大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學(xué)習(xí)攻略 Java學(xué)習(xí) Java成員變量初始化

Java成員變量初始化

更新時間:2022-08-30 10:30:55 來源:動力節(jié)點 瀏覽2828次

成員變量:定義在一個Java類中,屬于一個類;可以通過public、private、protected、static進(jìn)行修改;存儲在堆中;新對象必須在被靜態(tài)修改之前使用;可以調(diào)用當(dāng)前類的所有方法;如果類有子類,也可以調(diào)用子類;并且不能分配。如果沒有指定原始數(shù)據(jù)類型返回相應(yīng)的值,則非原始數(shù)據(jù)類型返回 null。

定義:

public class Test{
  public int num;  
}

初始化方法:

1.如果只定義了一個成員變量沒有賦值,編譯器會自動添加相應(yīng)的默認(rèn)值。如:

public class Test{
    //Basic data types
    private boolean flag;   //Default value: false
	private int _int;       //Default value: 0
	private byte _byte;     //Default value: 0
	private char _char;     //Default value: '0000', but when printed in Eclipse, it will be the blank character "box", because the code of the blank character is 0x20, and the control character below 0x20 is invisible.
	private short _short;   //Default value: 0
	private float _float;   //Default value: 0.0
	private long _long;     //Default value: 0
	private double _double; //Default value: 0.0	
	//Reference type defaults to null
	private String string;
}

2. 直接賦值

public class Test {
   private int num = 1;
   private String name = "xiaomin";
   public Person person = new Person();
}

3. 按方法初始化

public class Test{
  private int i = f();  
  private int j = g(i);
  private int f() {
	return 10;
  }
  private int g(int i){
    return i*2
  }  
}

但是編譯器在初始化對象的時候是按順序執(zhí)行的,所以不能通過下面的方式進(jìn)行初始化:

public class Test{
  private int j = g(i);
  private int i = f();  
  private int f() {
	return 10;
  }
  private int g(int i){
    return i*2
  }  
}

4.通過構(gòu)造函數(shù)初始化

public class Test{
  int num ;  
  public Test(){
    num = 10;
  }
}

在進(jìn)入構(gòu)造函數(shù)之前,編譯器默認(rèn)初始化num為0,進(jìn)入構(gòu)造函數(shù)后賦值為10。

初始化順序:

編譯類時,首先初始化成員變量,成員變量的初始化順序由定義成員變量的順序決定。

package com.extendstest;
public class OrderOfInitialization {
	public static void main(String[] args) {
		Card card = new Card();
		card.f();
	}
}
class Tag {
	Tag(int num) {
		System.out.println("Tag" + num);
	}
}
class Card {
	Tag tag1 = new Tag(1); //Define tag1 variable
	Card() {
		System.out.println("Card()");
		tag3 = new Tag(33); //tag3 is reinitialized
	}
	Tag tag2 = new Tag(2); //Define tag2 variables
	void f() {
		System.out.println("f()");
	}
	Tag tag3 = new Tag(3); //Define tag3 variables
}

在 main 方法中,創(chuàng)建了一個卡片對象。編譯器編譯 Card 類時,成員變量 tag1、tag2、tag3 在構(gòu)造函數(shù) Card() 之前初始化。因此,上述代碼的輸出如下:

Tag1
Tag2
Tag3
Card()
Tag33
f()

靜態(tài)變量的初始化順序:靜態(tài)成員變量優(yōu)先于非靜態(tài)成員變量。

public class StaticInitialization {
	public static void main(String[] args) {
		System.out.println("Creating new Cupboard() in main");
		new Cupboard();
		System.out.println("Creating new Cupboard() in main");
		new Cupboard();
		t2.f2(1);
		t3.f3(1);
	}
	static Table t2 = new Table(); 
	static Cupboard t3 = new Cupboard(); 
}
class Bowl{
	Bowl(int i){
		System.out.println("Bowl"+i);
	}
	void f(int i){
		System.out.println("f"+i);
	}
}
class Table{
	static Bowl b1 = new Bowl(1); 
	Table(){
		System.out.println("Table()");
		b2.f(1);
	}
	void f2(int i){
		System.out.println("f2("+i+")");
	}
	static Bowl b2 = new Bowl(2); 	
}
class Cupboard{
	Bowl b3 = new Bowl(3);
	static Bowl b4 = new Bowl(4); 
	Cupboard(){
		System.out.println("Cupboard()");
		b4.f(2);
	}
	void f3(int i){
		System.out.println("f3("+i+")");
	}
	static Bowl b5 = new Bowl(5); 
}

因為靜態(tài)初始化只在Class對象第一次創(chuàng)建時發(fā)生一次,所以上面的代碼執(zhí)行如下:

Bowl1
Bowl2
Table()
f1
Bowl4
Bowl5
Bowl3
Cupboard()
f2
Creating new Cupboard() in main
Bowl3
Cupboard()
f2
Creating new Cupboard() in main
Bowl3
Cupboard()
f2
f2(1)
f3(1)

還有以下靜態(tài)塊初始化方法:

class Cup{
	Cup(int i){
		System.out.println("Cup("+i+")");
	}
	void f(int i){
		System.out.println("f("+i+")");
	}
}
class Cups{
	static Cup c1;
	static Cup c2;
	static {  //Static block initialization
		c1 = new Cup(1);
		c2 = new Cup(2);
	}
	Cups(){
		System.out.println("Cups()");
	}
}
public class StaticInitialization {
	public static void main(String[] args) {
		System.out.println("Inside main()");
		Cups.c1.f(99);
	}
	static Cups x = new Cups();
	static Cups y = new Cups();
}

實施結(jié)果:

Cup(1)
Cup(2)
Cups()
Cups()
Inside main()
f(99)

另一個非靜態(tài)實例初始化,沒有 static 關(guān)鍵字

package com.extendstest;
class Mug{
	Mug(int i){
		System.out.println("Mug("+i+")");
	}
	void f(int i ){
		System.out.println("f("+i+")");
	}
}
public class Mugs {
	Mug c1;
	Mug c2;
	{     //Non-static initialization, which is called many times when an object is created
		c1 = new Mug(1);
		c2 = new Mug(2);
		System.out.println("c1&&c2inin....");
	}	
	Mugs(){
		System.out.println("Mugs()");
	}
	public static void main(String[] args) {
		System.out.println("inin..main");		
		Mugs x = new Mugs();
        Mugs y = new Mugs();
	}
}

實施結(jié)果:

inin..main
Mug(1)
Mug(2)
c1&&c2inin....
Mugs()
Mug(1)
Mug(2)
c1&&c2inin....
Mugs()

 

提交申請后,顧問老師會電話與您溝通安排學(xué)習(xí)

免費課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 真实的国产乱xxxx在线播放 | 激情久久久久久久久久 | 国产成人综合亚洲 | 欧美日韩亚洲国产一区二区三区 | 狠狠色狠狠综合久久 | 一级毛片美国一级j毛片不卡 | 欧美日韩国产成人综合在线 | 免费精品久久久久久中文字幕 | 欧美在线视频在线观看 | 97欧美精品一区二区三区 | 国99久9在线 | 免费 | 女色综合 | 日本亚洲黄色 | 国产精品一级香蕉一区 | 四虎4hutv永久在线影院 | 精品91自产拍在线观看99re | 天天干天天干天天干天天干天天干 | 天天艹在线 | 亚洲色中文字幕在线播放 | 俄罗斯一级在线播放 | 天天摸天天草 | 夜鲁夜鲁夜鲁在线观看福利 | 九九精品国产兔费观看久久 | 国产香蕉尹人综合在线 | 青草伊人久久 | 国产精品久久久久久久久久久不卡 | 青青成人 | 久久国产加勒比精品无码 | 国产1区2区3区在线观看 | 日产一二三四五六七区麻豆 | 日日操日日 | 99热久久国产精品免费观看 | 四虎精品免费永久在线 | 人人看人人鲁狠狠高清 | 四虎永久免费观看 | 国产成人女人视频在线观看 | 国产美女免费观看 | 久久久日本精品一区二区三区 | 亚洲水蜜桃久久综合网站 | 国产美女激情 | 成人日韩视频 |