智能建站公司,东营seo整站优化,深圳市seo网络推广平台,趣丁号友情链接this关键字
含义#xff1a;this代表当前对象
this使用位置
this在实例初始化相关的代码块和构造器中#xff1a;表示正在创建的那个实例对象#xff0c;即正在new谁#xff0c;this就代表谁this在非静态实例方法中#xff1a;表示调用该方法的对象#xff0c;即谁在调…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(实参列表)只从直接父类找不会再往上追溯