亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺析JAVA中的內(nèi)存結(jié)構(gòu)、重載、this與繼承

 更新時間:2021年03月15日 11:05:20   作者:521125LYC  
這篇文章主要介紹了 JAVA中的內(nèi)存結(jié)構(gòu)、重載、this與繼承的相關(guān)資料,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一.對象在JVM的內(nèi)存結(jié)構(gòu)

JAVA內(nèi)存管理由JVM來管理。
1)堆,所有new出來的對象(包括成員變量)
2)棧,所有局部變量(包括方法的參數(shù))
3)方法區(qū),class字節(jié)碼文件(包括方法,靜態(tài)數(shù)據(jù))

1.引用變量指向null時,會發(fā)生空指針異常

public class student {
	int age;
	String name;
	public student(int age,String name){
		this.age=age;
		this.name=name;
	}
	public static void main(String[] args) {
		//(類)引用類型 引用變量	創(chuàng)建對象(new出來的類中的構(gòu)造方法)
		student s= new student(18,"劉永超");
		s=null;
		System.out.println(s);
		System.out.println(s.age);//發(fā)生異常,因為引用變量指向null
	}
}

結(jié)果:java.lang.NullPointerException 2.引用類型劃等號和基本類型劃等號的區(qū)別: 一.引用類型劃等號:

1…指向同一個對象

2.通過一個引用的修改對象中的數(shù)據(jù)會影響另一個對象中的數(shù)據(jù)

二.基本類型劃等號:

1.賦值。

2.對一個變量的修改不會影響到另一個變量(例如int類型)

引用變量畫“”和基本類型畫”“區(qū)別:

引用類型畫”==“:

1.判斷兩個引用變量(引用地址)是否指向同一對象

2.基本類型畫等號:判斷兩邊的值是否相等

代碼演示:

public class student2 {
	int age;
	String name;
	public student2(int a,String s){
		age=a;
		name=s;
	}
	
	public static void main(String[] args) {
		student2 s=new student2(17,"liu");
		student2 ss=s;
		s.age=20;
		ss.age=28;
		System.out.println(s.age);
		//s=ss 判斷兩個引用對象(引用變量)是否指指向同一對象
		System.out.println(s==ss);
	}
}

結(jié)果為 s.age=28 true;

成員變量與局部變量的生命周期:

  • 成員變量:創(chuàng)建對象開始到被GC垃圾回收器處理掉為止。
  • 局部變量從調(diào)用方法到方法就結(jié)束。

 二.方法的重載

1).發(fā)生在同一類,方法名相同,參數(shù)列表類型和數(shù)量不同
2).方法的重載,和返回值沒有關(guān)系
3).編譯器在在編譯時根據(jù)方法的簽名自動綁定調(diào)用的方法

注:方法的簽名:方法名+參數(shù)列表

代碼的實現(xiàn):

public class overloadDemo {
	public void test(){
		System.out.println("lala");
	}
	
	public void test(int a){
		System.out.println("heihei");
	}

	public void test(String s){
		System.out.println("xixi");
	}

	public void test(int a,String s){
		System.out.println("caocao");
	}

//	public int test(){不是方法的重載,和返回值沒有關(guān)系
//		return 1;
//	}
	
	//構(gòu)造方法
	public overloadDemo (){
		System.out.println("無參構(gòu)造");
	}
	
	public overloadDemo(int a){
		System.out.println("有參構(gòu)造");
	}
	
	public static void main(String[] args) {
		overloadDemo load=new overloadDemo();
		load.test(5);
		load.test(8, "liu");
		
	}
}

如上代碼,load.test(5)將調(diào)用public void test(int a){};

load.test(8,“l(fā)iu”)將調(diào)用public void test(int a,String s){}。

三.this的使用:

​ 1.this關(guān)鍵字在方法中,用于指向調(diào)用該方法的當前對象,簡單的說,那個對象調(diào)用方法,this指的就是那個對象,嚴格來講,在方法中需要通過this關(guān)鍵字指明當前對象。
2.在構(gòu)造方法中,用來初始化成員變量的參數(shù)一般和成員變量取相同的名字,這樣有利于代碼的可讀性,但次數(shù)必須通過this關(guān)鍵字來區(qū)別成員變量和參數(shù)(在這里不能省略this)
3.this就是指當前對象。

this的用法:

1.this.成員變量—訪問當前對象成員變量
2.this.方法名—訪問當前對象的方法(一般不用)
3.this()---------調(diào)用構(gòu)造方法(必須寫在此構(gòu)造方法中的第一行)

代碼演示:

public class thisDemo {
	int age;
	public void test(){
		System.out.println(this.age);//此處可以省略this關(guān)鍵字
	}
	
	public void test(int age){
		System.out.println("這是帶有int類的test方法重載");
		this.test();//此處也可以省略this,編譯器會自己默認
	}
	
	public thisDemo(){
		//this(20);
		this(19,"liuyongcaho");//調(diào)用當前對象時int ,string類型的構(gòu)造方法
		this.age=18;
		System.out.println("這是一個無參構(gòu)造方法");
	}
	
	public thisDemo(int age){
		this();
		System.out.println(age);
	}
	
	public thisDemo(int age,String name){
		System.out.println(age+"   "+name);
	}
	
	
	public static void main(String[] args) {
		thisDemo pdd=new thisDemo();
		pdd.test(9);
	}
}

運行結(jié)果:

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-QA8e4G5v-1615567706657)(C:\Users\Thinkpad\AppData\Roaming\Typora\typora-user-images\image-20210312160858936.png)]

可以看出,在new對象時,會自行執(zhí)行它的無參構(gòu)造方法,執(zhí)行完后再執(zhí)行test(int a){}這個方法。

四:類的繼承

​ 1.作用:代碼復(fù)用,減少代碼重復(fù)
​ 2.通過extends來繼承
​ 3.超類(父類),派生類(子類)共有的屬性和行為
​ 4.派生類繼承超類后,派生類具有派生類+超類的共有屬性。
​ 5.一個超累可以擁有多個派生類,一個派生類只能有一個超類。
​ 6.繼承具有傳遞性
​ 7.java中規(guī)定,在派生類的構(gòu)造方法中必須先構(gòu)造超類的構(gòu)造方法(必須在派生類的第一行),在派生類中若沒有調(diào)用超類的方法,則編譯器默認提供super()來調(diào)用超類的無參構(gòu)造方法,若超類自己寫了構(gòu)造方法,在派生類中不在默認提供super();

super的含義及使用:

​ 1.super指代當前對象的超類對象
​ 2.super必須放在構(gòu)造方法中的第一行
​ 3.super.成員變量-----訪問超類成員變量
​ 4.super()—調(diào)用的時超類構(gòu)造方法。

代碼演示如下: 父類:

/*
 * 父類(超類)
 */
public class Person {
	int age;
	String name;
	String gender;
	
	public Person(int age,String name){
		this.age=age;
		this.name=name;
		System.out.println(this.age+"  "+this.name);
	}
	public void testperson(){
		System.out.println("我是父類");
	}
}

子類:

/*
 * 子類(派生類)
 * extends
 */
public class Teacher extends Person{
	String subject;
	public Teacher(){
		//super();這是父類的無參構(gòu)造方法
		super(19,"劉德華");
		System.out.println(this.age+" "+this.name);
	}
	
	//方法的重寫
	public void testperson(){
		System.out.println("這是子類teacher的testperson方法");
	}
	
	public static void main(String[] args) {
		 Person y=new Teacher();//向上造型
		 y.testperson();
	}
}

運行結(jié)果:

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-zydw2DJx-1615567706663)(C:\Users\Thinkpad\AppData\Roaming\Typora\typora-user-images\image-20210312165717602.png)]

結(jié)果分析:

子類在創(chuàng)建對象時會執(zhí)行子類的無參構(gòu)造方法,而子類的構(gòu)造方法中有父類的有參構(gòu)造方法再執(zhí)行子類中的testperson方法。

on方法");
}

public static void main(String[] args) {
	 Person y=new Teacher();//向上造型
	 y.testperson();
}

}

##### 運行結(jié)果:

[外鏈圖片轉(zhuǎn)存中...(img-zydw2DJx-1615567706663)]

##### 結(jié)果分析:

子類在創(chuàng)建對象時會執(zhí)行子類的無參構(gòu)造方法,而子類的構(gòu)造方法中有父類的有參構(gòu)造方法再執(zhí)行子類中的testperson方法。

#### 

到此這篇關(guān)于淺析JAVA中的內(nèi)存結(jié)構(gòu)、重載、this與繼承的文章就介紹到這了,更多相關(guān)java內(nèi)存結(jié)構(gòu)重載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論