Java構造器與傳值學習總結
本文主要學習Java構造器與傳值,供大家參考,具體內容如下
構造器
構造器介紹
構造器是Java學習中一個很重要的概念,每個類的對象在使用關鍵字new實例化的時候,Java虛擬機都會給這個實例化的對象自動調用一個無參構造器,但是當我們自己寫有構造器時,Java虛擬機將不再為我們調用這個無參構造器,而是我們要遵循我們自己寫的構造器的參數(shù)規(guī)則來進行實例化。構造器不需要返回值類型,同時它也可以提供很多方法,最主要的功能是初始化類的變量成員。一個類可以有多個不同的構造器,但是這些構造器的參數(shù)列表的數(shù)量、類型、或者順序上的差異,而不是同類型不同變量名的差異。
構造器代碼演示
無參構造器
public class ConstructDisplay {
?? ?public static void main(String[] args) {
?? ??? ?ConstructDisplay display=new ConstructDisplay();
?? ??? ?//我們沒有自己定義類的構造器
?? ??? ?//所以Java虛擬機為我們自動調用了無參構造器
?? ?}
}自定義構造器
public class ConstructDisplay {
?? ?String name;
?? ?int age;
?? ?
?? ?public ConstructDisplay(String name) {
?? ?//this.name指的是調用這個構造器對象的name屬性
?? ?//而等號右邊的name才是
?? ?//public ConstructDisplay(String name)中的形參name
?? ??? ?this.name=name;
?? ?}
?? ?
?? ?public ConstructDisplay(int age) {
?? ??? ?this.age=age;
?? ?}
?? ?
?? ?public static void main(String[] args) {
?? ??? ?ConstructDisplay display=new ConstructDisplay("1");
? ? ? ? ConstructDisplay display2=new ConstructDisplay(1);
?? ?}
}在上面的代碼中,在我們沒有定義無參構造器這種情況下,我們將不能在實例化對象時再調用無參構造器。
this與super
this關鍵字
當一個對象被創(chuàng)建好之后,Java虛擬機就會為這個對象分配一個引用自身的指針this,Java中為了解決變量命名的沖突與不確定性等問題,引入關鍵字this代表其所在方法的當前對象。
在代碼中使用this關鍵字
構造器中的this
public class Tree {
?? ??
?? ? ?String name;
?? ? ?int num;
?? ? ?int age;
? ? ? ?? ?
?? ? ?public Tree(String name) {
?? ??? ? ?System.out.print("樹的品種是"+name+",");
?? ? ?}
?? ? ?
??? ? ?public Tree(int age) {
?? ??? ? ?this("蘋果樹");
?? ??? ? ? //調用Tree的其他構造器
?? ??? ? ?System.out.println("它的樹齡為"+age+"年.");
?? ? ?}
?? ? ?
?? ? ? public Tree() {
?? ? ? ? this(10);
?? ? ? ? //調用Tree的其他構造器
?? ? ? ? System.out.println(".......");
?? ?}
? ? ? ?? ?
? ? ? ? public static void main(String[] args) {
?? ??? ??? ?Tree tree=new Tree();
?? ??? ?}
}
//run:
//樹的品種是蘋果樹,它的樹齡為10年.
//.......這里需要注意的是,在構造器中使用this調用其他構造器時要把調用的代碼放在該構造器的第一句。構造器中賦值this的用法在上面介紹構造器時就使用了這個寫法,所以就不在這里重復了。
構造方法中的this
public class Tree {
?? ??
?? ? ?String name;
?? ? ?int age;
? ? ??
?? ? ?
?? ? ?public void setAge(Tree t) {
?? ??? ? ?t.age=10;
?? ? ?}
?? ? ?
?? ? ?public void setName(String name) {
?? ??? ? ?this.name=name;
?? ??? ? ?//將當前調用setName的對象作為實參傳給setAge的形參t
?? ??? ? ?setAge(this);
?? ? ?}
? ? ? ?? ?
? ? ? ? public static void main(String[] args) {
?? ??? ??? ?Tree tree=new Tree();
?? ??? ??? ?tree.setName("蘋果樹");
?? ??? ??? ?System.out.println(tree.name+"的樹齡為"+tree.age);
?? ??? ?}
}
//run:蘋果樹的樹齡為10在代碼中使用super關鍵字
構造器與方法中的super
class BananaTree extends Tree{
?? ?public BananaTree(String name, int age) {
?? ?//第一個super調用父類的構造器
?? ??? ?super(name, age);
? ? //第二個super調用父類的方法setName
?? ??? ?super.setName("香蕉樹");
?? ?}
?? ?
}
public class Tree {
?? ??
?? ? ?String name;
?? ? ?int age;
? ? ??
?? ? ?public Tree(String name,int age) {
?? ??? ?this.name=name;
?? ??? ?this.age=age;
?? ?}
?? ? ?
?? ? ?public void setName(String changeName) {
?? ??? ? ?this.name=changeName;
?? ? ?}
?? ? ? ??? ?
? ? ? ? public static void main(String[] args) {
?? ??? ? ? BananaTree tree=new BananaTree("蘋果樹", 10);
?? ??? ? ? System.out.println(tree.name+"的樹齡為"+tree.age);
?? ??? ?}
}super關鍵字僅在子類中使用,它用來調用調用父類的屬性、構造器或者方法,所以super常與繼承一起出現(xiàn)。
傳值與傳引用
傳值與傳引用的區(qū)別
傳值就是將當前變量的值傳遞給另外一個與當前變量不相關聯(lián)的另一個變量,另一個變量的操作不會對當前的變量本身造成影響;而傳引用就是將本身傳遞給另一個變量指向,另一個變量對這個變量操作也會影響到當前變量。
傳值與傳應用代碼示例
傳值
public class Tree {
?? ??
?? ? ?String name;
?? ? ?int age;
? ? ??
?? ? ?public Tree(String name,int age) {
?? ??? ?this.name=name;
?? ??? ?this.age=age;
?? ?}
?? ? ?
?? ? ?public void setName(String name) {
?? ??? ? ?name="香蕉樹";
?? ? ?}
?? ? ? ? ??? ?
? ? ? public static void main(String[] args) {
?? ??? ? ? Tree tree=new Tree("蘋果樹",10);
?? ??? ? ? tree.setName(tree.name);
?? ??? ? ? System.out.println(tree.name+"的樹齡為"+tree.age);
?? ??? ?}
}傳引用
public class Tree {
?? ??
?? ? ?String name;
?? ? ?int age;
? ? ??
?? ? ?public Tree(String name,int age) {
?? ??? ?this.name=name;
?? ??? ?this.age=age;
?? ?}
?? ? ?
?? ? ?public void setName(Tree otherTree) {
?? ??? ? ?otherTree.name="香蕉樹";
?? ? ?}
?? ? ? ? ??? ?
? ? ? public static void main(String[] args) {
?? ??? ? ? Tree tree=new Tree("蘋果樹",10);
?? ??? ? ? tree.setName(tree);
?? ??? ? ? System.out.println(tree.name+"的樹齡為"+tree.age);
?? ??? ?}
}哪些情況下是傳值,哪些情況下是傳引用,我認為不是一篇簡單的博客就可以解釋的明白的,更重要的還是多打代碼,自己就能總結出什么情況是傳值,什么情況下是傳引用了。經驗都是自己慢慢總結出來噠,我也只是在這里跟小伙伴們提出這樣一個概念這樣子啦。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
使用MyBatis 動態(tài)update數(shù)據(jù)
使用mybatis寫sql,需要動態(tài)更新對象數(shù)據(jù),每次需要更新的字段不同,為了防止null空異常,就需要用動態(tài)sql了,下面給大家分享一段代碼關于mybatis動態(tài)update,需要的朋友參考下2016-11-11

