publicclassperson{
String name ="Person";publicvoidprint(){
System.out.println("person print");}publicclassboyextendsperson{
String name ="boy";publicvoidprint(){
System.out.println("boy print");}publicvoidPrintname(String name){
System.out.println(name);//输出传入name
System.out.println(this.name);//输出本类定义的name属性
System.out.println(super.name);//输出父类的name属性}publicvoidtest(){print();//输出本类的print方法this.print();//和上面一样super.print();//输出父类的print方法}
调用Printname("xiao ming")后输出:
xiao ming
boy
Person
调用test()后输出:
boy print
boy print
person print
super和this关键字this相当于自身类的一个对象,super相当于父类的一个对象this和super均需要放到构造器的第一行,所以this和super不能同时使用this没有继承也可以使用,super只能在继承后的子类中调用子类的构造函数中默认调用super()即父类的无参构造,弱父类中自己定义了有参构造,那么系统默认的午餐构造将会失效,子类中构造函数要使用super(参数)的形式写出来,否则会报错。代码public class person { String name =