詳述:Java成員變量和構造方法

Java成員變量

1.成員變量的定義

  • “直接”在類體中定義的變量;
    特點
  • 成員變量有默認值(整數 0; 浮點型 0.0 ;char " " ; boolean false; 其他類型 null)
  • 成員變量在整個類中都有效,但是一般寫在類的頭部。
    作用
  • 詳細描述對象信息
package userinfor;

public class UserInfo {
    
     int age;
     String name;
     String mobile;
     String address;
     char a;
     boolean b;
     double c;   

public static void main(String[] args) {
          UserInfo user1 = new UserInfo();
          System.out.println(user1.age);
          System.out.println(user1.name);
          System.out.println(user1.a);
          System.out.println(user1.b);
          System.out.println(user1.c);
}

在這裏插入圖片描述

從上面代碼和結果來看可以看出,成員變量是有默認值的,分別是與結果相對應,整型-0;String-null;char-空;boolean-false;浮點型-0.0。

2.成員變量的賦值方法

a. 直接複製法

package userinfor;

public class UserInfo {
    
     int age;
     String name;
     String mobile;
     String address;


	 public static void main(String[] args) {
          //int score = 12;
          UserInfo user1 = new UserInfo();
          user1.age = 12;
          user1.name = "某某";
          user1.mobile = "123456789";
          user1.address = "xxxxxxxxx";
          System.out.println(user1.age);
          System.out.println(user1.name);
          System.out.println(user1.mobile);
          System.out.println(user1.address);
         
          UserInfo user2 = new UserInfo();
          user2.age = 21;
          user2.name = "某某某";
          user2.mobile = "987654321";
          user2.address = "xxxxxxxxxxxxxx";
          System.out.println(user2.age);
          System.out.println(user2.name);
          System.out.println(user2.mobile);
          System.out.println(user2.address);
	}
}

在這裏插入圖片描述
當對象的數量多的時候,這樣的賦值方法可以說是非常的麻煩,而且導致了代碼非常的不簡潔,這是與Java編程思想相悖的。所以第二種賦值方法產生了。

b. 利用構造方法

Java構造方法

構造方法的定義

  • 方法是類名({參數列表()}){}
    特點
  • 構造方法沒有返回值部分 void 不能有;
  • 一個類中默認有無參構造方法,當定義了一個有參的構造方法,無參方法就不存在了
  • 構造方法不允許final和static修飾
  • 一個類中根據需要可以定義多個構造方法,這是”重載“體現
  • 爲了簡化代碼,類中構造方法可以相互調用,this(實參列表),但必須至於構造方法第一行,而且一個構造方法中不能有兩個this,否則會報錯,且調用是不分上下的,this(實參列表)既可以調用上面的構造方法,也可以調用下面的構造方法。
    在這裏插入圖片描述
package userinfor;


public class UserInfo {
    
     int age;
     String name;
     String mobile;
     String address;

     UserInfo(int a,String n, String m,String add){
          this(a,n);//調用變量a,n的構造方法
          mobile = m;
          address = add;
     }
    
     UserInfo(int a){
          age= a;
     }
     UserInfo(int a,String n){
          this(a);//調用a的構造方法
          name = n;
     }
     public static void main(String[] args) {
          UserInfo user1 = new UserInfo(12,"某某","123456789","xxxxxxxx");
        
          System.out.println(user1.age);
          System.out.println(user1.name);
          System.out.println(user1.mobile);
          System.out.println(user1.address);
         
         
          UserInfo user2 = new UserInfo(21,"某某某","987654321","xxxxxxxxxxxxxx");
          System.out.println(user2.age);
          System.out.println(user2.name);
          System.out.println(user2.mobile);
          System.out.println(user2.address);
     }
    
    
}

在這裏插入圖片描述
在這裏插入圖片描述

以上代碼,結果跟第一種方法是一模一樣的,但是從代碼的簡化方面來看,明顯簡化了,大家可以在以後的編程中採用第二種方法去給對象賦值。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章