网站建设电话销售开场白百度知道个人中心
this关键字
- 含义:this代表当前对象
this使用位置
- this在实例初始化相关的代码块和构造器中:表示正在创建的那个实例对象,即正在new谁,this就代表谁
- this在非静态实例方法中:表示调用该方法的对象,即谁在调用,this就代表谁。
- this不能出现在静态代码块和静态方法中
this的三种用法
this.成员变量名
- 方法的局部变量与当前对象的成员变量重名时,就可以在成员变量前面加this区分成员变量和局部变量,没有重名问题,就可以省略this.
- this.成员变量会先从本类声明的成员变量列表中查找,如果未找到,会去从父类继承的在子类中仍然可见的成员变量列表中查找
class Fu {public String name ="父类的名字";public int age =22;
}class Zi extends Fu {public int age = 18;public void test() {int age = 20;//当方法的局部变量与当前对象的成员变量重名时,就可以在成员变量前面加this.System.out.println(this.age); //18/*this.成员变量会先从本类声明的成员变量列表中查找,如果未找到,会去从父类继承的在子类中仍然可见的成员变量列表中查找*/System.out.println(this.name); //父类的名字}
}
public class Demo01{public static void main(String[] args) {Zi zi = new Zi();zi.test();}
}
this.成员方法
- 调用当前对象的成员方法时,都可以加"this.",也可以省略,实际开发中都省略
- 当前对象的成员方法,先从本类声明的成员方法列表中查找,如果未找到,会去从父类继承的在子类中仍然可见的成员方法列表中查找
class Fu {public void demo() {System.out.println("我是父类可见成员方法");}
}class Zi extends Fu {public void test() {//调用当前对象的成员方法时,都可以加"this.",也可以省略,实际开发中都省略this.show(); //张三this.demo(); //我是父类可见成员方法}public void show() {System.out.println("张三");}
}public class Demo01 {public static void main(String[] args) {Zi zi = new Zi();zi.test();}
}
this()或this(实参列表)
- 只能调用本类的其他构造器
- 必须在构造器的首行
- 如果一个类中声明了n个构造器,则最多有 n - 1个构造器中使用了"this(【实参列表】)",否则会发生递归调用死循环
class Zi {private String name;private int age;private char sex;public Zi() {}public Zi(String name, int age) {this.name = name;this.age = age;}public Zi(String name, int age, char sex) {//只能调用本类的其他构造器且必须在构造器的首行this(name, age); this.sex = sex;}}
super关键字
含义:super代表当前对象中从父类的引用的
super使用的前提
- 通过super引用父类的xx,都是在子类中仍然可见的
- 不能在静态代码块和静态方法中使用super
super的三种用法
super.成员变量
在子类中访问父类的成员变量,特别是当子类的成员变量与父类的成员变量重名时。
class Father{int a = 10;
}
class Son extends Father{int a = 20;public void test(int a){//在子类中访问父类的成员变量,特别是当子类的成员变量与父类的成员变量重名时。System.out.println(super.a);//10//访问本类的成员变量System.out.println(this.a);//20//访问本方法的局部变量System.out.println(a);//30}
}
public class Test{public static void main(String[] args){Son s = new Son();s.test(30);}
}
super.成员方法
在子类中调用父类的成员方法,特别是当子类重写了父类的成员方法时
class Father{public void method(){System.out.println("父类方法");}
}
class Son extends Father{public void test(){//调用子类方法method();//子类方法super.method();//父类方法}public void method(){System.out.println("子类方法");}
}public class Test{public static void main(String[] args){Son s = new Son();s.test();}
}
super()或super(实参列表)
在子类的构造器首行,用于表示调用父类的哪个实例初始化方法。super() 和 this() 都必须是在构造方法的第一行,所以不能同时出现。
class Father {private String name;public Father(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}class Son extends Father {public Son( String name) {super(name);//调用父类中的构造方法}public void show(){System.out.println("名字:" + super.getName());}
}public class Test {public static void main(String[] args) {Son s = new Son("张三");s.show(); //名字:张三}
}
就近原则和追根溯源原则之找变量
没有super和this
- 在构造器、代码块、方法中如果出现使用某个变量,先查看是否是当前块声明的局部变量,
- 如果不是局部变量,先从当前执行代码的本类去找成员变量
- 如果从当前执行代码的本类中没有找到,会往上找父类的(非private,跨包还不能是缺省的)
存在this
-
通过this找变量时,先从当前执行代码的本类中的成员变量开始找,没有的会往上找父类的(非private,跨包还不能是缺省的)。
存在super
- 通过super找成员变量,直接从当前执行代码所在类的父类找
- super()或super(实参列表)只能从直接父类找
- 通过super只能访问父类在子类中可见的(非private,跨包还不能是缺省的)
注意:super和this都不能出现在静态方法和静态代码块中,因为super和this都是存在与对象中的
就近原则和追根溯源原则之找方法
没有super和this
-
先从当前对象(调用方法的对象)的本类找,如果没有,再从父类继承的可见的方法列表中查找,再没有,继续往上追溯
存在this
-
先从当前对象(调用方法的对象)的本类找,如果没有,再从父类继承的可见的方法列表中查找,再没有,继续往上追溯
存在super
-
直接从当前对象(调用方法的对象)的父类继承的可见的方法列表中查找,再没有,继续往上追溯
找构造器
- this()或this(实参列表):只从本类中,不会再往上追溯
- super()或super(实参列表):只从直接父类找,不会再往上追溯