super学习
2022-10-02 16:27:38
super
super代表的是“当前对象(this)”的父类型特征
概念
1、super是一个关键字,全部小写。
2、super和this对比着学习。
this:
this能出现在实例方法和构造方法中。
this的语法是:”this.“,”this()“
this不能使用在静态方法中。
this.大部分情况下是可以省略的
⭐this.在区分局部变量和实例变量的时候不能省略。
public class setName(String name){ this.name=name; } //此时不能省略
this()只能出现在构造方法第一行,通过当前的构造方法去调用”本类“中其他的构造方法,目的是:代码复用
super:
super只能出现在实例方法和构造方法中。
super的语法是:”super.“,”super()“
super不能使用在静态方法中。
super.大部分情况下是可以省略的
⭐super.在???的时候不能省略。
父类和子类中有同名属性,或者说同样的方法,
想在子类中访问父类的,super.,不能省略。
super()只能出现在构造方法第一行,通过当前的构造方法去调用”父类“中其他的构造方法,目的是:创建子类对象的时候,先初始化父类特征
3、super()
表示通过子类的构造方法调用父类的构造方法
模拟现实生活中的这种场景:要想有儿子,需要先有父亲
4、重要结论:
当一个构造方法第一行:
既没有this()有没有super()的话,默认会有一个super();
表示通过当前子类的构造方法调用父类的无参数构造方法。
所以必须保证父类的无参数构造方法是存在的。(建议手动添加)
5、this()和super()不能共存,他们都只能出现在构造方法第一行
6、无论怎样,父类的构造方法是一定会执行的。(百分百的)
public class SuperTest{ public static void main(String [] args){ new C(); } } class A{ public A(){ Systemm.out.println("A的无参数构造执行1"); } } class B extends A{ public B(){ System.out.println("B的无参数构造执行2"); } public B(String name){ System.out.println("B的有参数构造执行(String)3"); } } class C extends B{ public C(){
this("zhangsan"); System.out.println("C的无参数构造执行4"); } public C(String name){
this(name,20); System.out.println("C的有参数构造执行(String)5"); } public C(String name,int age){
super(name); System.outt.println("C的有参数构造执行(String,int)6"); } }
执行结果:
A的无参数构造执行1
B的有参数构造执行(String)3
C的有参数构造执行(String,int)6
C的有参数构造执行(String)5
C的无参数构造执行4
在Java语言中不论是new什么对象,最后老祖宗的Object类的无参数构造方法一定会执行。(Object类的无参数构造方法是处于”栈顶部位“(最后调用,最先执行结束))
用法

利用super(String a,double b)实现对父类私有变量在子类的构造方法中的赋值。
🌼🌼🌼🌼🌼🌼
1、super关键字代表的就是”当前对象“的那部分父类型特征
2、注意:虽然调用构造方法,在构造方法执行过程中一连串调用了父类的构造方法,父类的构造方法又继续向下调用它的父类方法,但是实际上对象只调用了一个。
3、super(实参)初始化当前对象的父类型特征。并不是创建新对象
,实际上对象只创建了一个。
观察以下代码:super.name 、this.name、 nampublic class SuperTest public static void main(String[] args){
public class SuperTest{
public static void main(String[] args){
Vip v=new Vip("张三");
v.shopping();
}
}
class Customer{
String name;
public Customer(){}
public Customer(String name){
this.name==name;
}
}
class Vip extends Customer{
public Vip(){}
public Vip(String name){
super(name);
}
public void shopping(){
//this表示当前对象
System.out.println(this.name+"正在购物!");
//super表示的是当前对象的父类型特征。(super是this指向的那个对象中的一块空间)
System.out.println(super.name+"正在购物!");
//name前面如果什么也没有,自动在前面加上(引用.)<=>(this.name)
System.out.println(name+"正在购物!");
}
}
内存图如下
4、super使用时后面必须有个点
public calss Test0{ public void doSome(){ //输出:Test0@2f920f4 System.out.println(this); //输出“引用”的时候,会自动调用引用的toString()方法。 //System.out.println(this.toString); //编译错误:需要'.' // System.out.println(super); }
//this和super不能使用在static静态方法中。
/*
public static void doOther(){
System.out.println(this);
System.out.println(super.xxx);
*/
//静态方法,主方法 public static void main(String[] args){ Test0 st =new Test0(); st.doSome();
//main方法是静态的
//错误的
/*System.out.println(this);
System.out.println(super.xxx);
*/
} }
/*
通过👆这个测试得出的结论
super不是引用。super也不保存内存地址,super也不指向任何对象
super只是代表当前对象内部的那一块父类型的特征。
*/
public class Test00{ public static void main(String[] arfs){ Cat c=new Cat(); c.yidong(); } } class Animal{ public void move(){ System.out.println("Animal move"); } } class Cat extends Animal{ //对move进行重写 public void move(){ System.out.println("Cat move"); } //单独编写一个子类特有的方法 public void yidong(){ this.move(); move(); //super.不仅可以访问属性,也可以访问变量 super.move(); } }
👆代码输出结果:
/*
Cat move
Cat move
Animal move
*/
因此:在父和子中有同名的属性,或者说有相同的方法,如果此时想在子类中访问父中的数据,必须使用“super.”加以区分。
🐀🐂🐅🐇🐉🐍🐎🐏🐒🐓🐕🐖
💄super.属性名【访问父类的属性】
💄super.方法名(实参)【访问父类的方法】
💄super(实参)【调用父类的构造方法】
本文作者:TranquilTimber
本文链接:https://www.cnblogs.com/gbrr/p/16747903.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步