Introduction to Java (8) - Keywords this and super
Table of contents
Introduction to Java (8) - Keywords this and super
this keyword
* Usage and meaning:
* this. Property name - calling object:
- Use the this keyword in the constructor to assign a value to the property. This means that the current object has variables, and the end without this is the value passed by the parameter
* this. Method name ( Actual parameters) - calling object:
- Java allows one member method of an object to directly call another member method, and the this prefix can be omitted
- this reference cannot be used in static modified methods [Java syntax stipulates that static members cannot directly access non-static members ]
- If you pass a reference to an object to a static method modified by static, you need to create its own object. Through the reference of this object, call non-static methods and access non-static data members
* this (actual parameter) - access the constructor:
- Used to access the constructor of this class
- Can only be used in the constructor and is the first statement
- This can appear in instance methods and constructors
- Cannot appear in static methods
- Used to distinguish local variables and member variables , this cannot be omitted
super keyword
* Usage and meaning:
* super. Property name - access the properties of the parent class
* super. Method name (actual parameter) - access the method of the parent class
* super (actual parameter) - access the constructor of the parent class
- means passing The constructor method of the subclass calls the constructor method of the parent class
- simulates a real-world scenario: an existing parent has a child
- can only be used in the re-constructor method, and is the first statement
- can appear in instance methods and constructor methods
- cannot Used in static methods
- when there is one in the parent and one in the child, and you want to access the "parent's characteristics" in the child, you cannot omit super
- the function of super (actual parameter) is to initialize the parent class characteristics of the current object, not Create a new object
- super represents the inheritance of the "current object" characteristics
Summarize
- If neither this() nor super() appears in the constructor, then there will be a super() by default; to inherit the parameterless constructor of the parent class
- Since both this() and super() can only appear in the re-constructor method The first statement, so both cannot appear in the same constructor at the same time
- no matter how it is called, the constructor of the parent class will be used
Sample code
class Teacher{
String name="张三";
public Teacher(String name){
System.out.println("this.name-->"+this.name);//张三
this.name=name;//this不能省略,用来区分局部变量和成员变量
System.out.println("name-->"+name);//lisi
}
public void run1(String r) {
System.out.println("这里是"+r);
}
public void run2() {
this.run1("run2");//调用本类的方法
//this可以省略
}
public Teacher() {
this("王五");//无参数构造方法调用本类中有参数的构造方法
}
}
class Sudents extends Teacher{
public Sudents() {//默认存在super(),所以调用父类的无参数构造方法
}
public Sudents(String name) {
super("学生A");//调用父类的有参数构造方法
System.out.println(super.name);//因为原来父亲的属性被更改,所以输出父类的属性为更改后的
}
public void s() {
System.out.println(super.name);//输出父类的属性
super.run1("子类的普通方法");
}
}
public class ThisAndSuper {
public static void main(String[] agrs) {
Teacher t=new Teacher("lisi");//创建对象的有参数构造方法调用
System.out.println("------------------------");
t.run1("run1");
t.run2();
System.out.println("------------------------");
Teacher tt=new Teacher();//创建对象的无参数构造方法调用
System.out.println("------------------------");
Sudents s=new Sudents();
new Sudents("老师A");
s.s();
}
}