public class Person{
protected String name="d";
public Person() {
System.out.println("Person");
}
}
/*
super注意点:
1.super调用父类的构造方法,必须在构造方法的第一个
2.super必须只能出现在子类的方法或者构造方法中(构造器)
3.super和this不能同时调用构造方法
super VS this:
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的应用
前提:
this:没有继承也可以使用
super:只能在继条件下才可使用
构造方法:
this();本类的构造
super();父类的构造
*/
public class Student extends Person{
private String name="du";
public Student() {
//隐藏代码:调用了父类的无参构造
super();//调用父类的构造器,必须要在子类构造器的第一行
System.out.println("Student");
}
public void text(String name){
System.out.println(name);//duh
System.out.println(this.name);//du
System.out.println(super.name);//d
}
}
public class Application {
public static void main(String[] args) {
Student student = new Student();
//Person
//Student
student.text("duh");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】