------- android培训、java培训、期待与您交流! ----------
super关键字和this关键字:
this:
1.存储是当前对象的引用;2.它始终指向的是本对象内部的成员(成员属性、成员方法、构造函数)
super:
1.存储的父类对象的引用;2.它指向父类对象中的成员(成员属性、成员方法、构造函数)
在子类中可以定义跟父类相同的成员属性,此时,在子类中,这个变量将覆盖
父类的同名变量。可以使用关键字super.变量名显示的去获取父类的被覆盖的同名
变量。
class Person
{
String schoolName = "aa";
}
class Student extends Person
{
String schoolName = "bb";
String name;
int age;
Student(){
super();//super访问父类的无参构造函数
fun();
}
void show(){
//使用this可以访问本对象的成员属性
System.out.println("我的名字:" + this.name +
",年龄:" + this.age );
System.out.println("打印同名变量:" + " 本类:" + this.schoolName +
" 父类的:" + super.schoolName);
}
void fun(){
//使用this可以访问本对象的成员函数
// this();
}
}
class Demo
{
public static void main(String[] args)
{
Student stu = new Student();
stu.fun();
}
}