super and this
生活随笔
收集整理的這篇文章主要介紹了
super and this
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
super
指向父類的一個指針, 引用父類中的屬性,方法或者構(gòu)造函數(shù)
public class Father {
String name ;
Father(String myName){
name = myName;
System.out.println("name from father: "+name);
}
protected void outPut(){
System.out.println("use super to get father's method");
}
}
public class SonClass extends Father{
SonClass(){
super("sandy");//調(diào)用父類的構(gòu)造函數(shù)Father(String myName)
super.outPut();//調(diào)用父類的方法
System.out.println(super.name);//調(diào)用父類的屬性
}
public static void main(String [] args){
SonClass sClass = new SonClass();
}
}
this:指向本實例的一個指針,調(diào)用本實例中的構(gòu)造函數(shù)或者屬性。應該為構(gòu)造函數(shù)中的第一條語句
public class SonClass {
String name;
SonClass(){
//super("sandy");
// super.outPut();
System.out.println("test");
}
SonClass(String name){
this();//調(diào)用本類的構(gòu)造函數(shù)SonClass();
this.name = name;//給本類的屬性name賦值
System.out.println(name);
}
public static void main(String [] args){
SonClass sClass = new SonClass("wendy");
}
}
總結(jié)
以上是生活随笔為你收集整理的super and this的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: docker国内镜像地址
- 下一篇: VIM下Express jade空格问题