package OrangeWater.oop.demo04;
public class Student extends Person{
private String name = "lining";
public void test(String name){
System.out.println(name);//test传入的name
System.out.println(this.name);//当前类的name,也就是lining,this是指向当前类name的指针
System.out.println(super.name);//super是指向父类name的指针
}
}
package OrangeWater.oop.demo04;
public class Person {
protected String name = "李宁";
}
package OrangeWater.oop;
import OrangeWater.oop.demo04.Person;
import OrangeWater.oop.demo04.Student;
/*
1. 提高程序的安全性,保护数据
2. 隐藏代码的实现戏界
3. 统一接口
4. 系统维护增加效率
*/
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test("王伟");
}
}
结果
王伟
lining
李宁
super注意点:
- super调用父类的构造方法,必须在构造方法的第一个
- super必须只能出现在子类的方法或者构造方法中!
- super和this不能同时调用构造方法!
this:
代表的对象不同:
~~~~~~~~
this:本身调用者这个对象
~~~~~~~~
super:代表父类对象的应用
前提
~~~~~~~~
this:没有继承也可以使用
~~~~~~~~
super:只能在继承条件才可以使用
构造方法
~~~~~~~~
this():本类的构造
~~~~~~~~
super():父类的构造!