This article brings you relevant knowledge aboutjava, which mainly introduces the related issues about this and super in keywords, as well as some of their differences. Let’s take a look at them together. I hope everyone has to help.
## Recommended study: "java video tutorial"
## 1. Use of the "this" keyword
this2. Functionkeyword is difficult to understand. Its function is very close to its meaning, which means "current".
thiskeyword can be used to modify and call: properties, methods, and constructors.
3. Usethis
Keywords can only be used inside methods.It is used inside a method, that is, a reference to the object to which this method belongs;
It is used inside a constructor, indicating the object being initialized by the constructor.
this. Specific: We can use
3.1 Modify attributes and methodsthis
to distinguish properties and local variables. For example:this.name = name;
thisCode demonstration:is understood as: the current object or The object currently being created
In the method of the class, we can use
this.property
orthis.method
to call the current object properties or methods. However, usually, we choose to omitthis.
. In special cases, if the formal parameter of the method has the same name as the attribute of the class, we must explicitly usethis.variable
to indicate that the variable is an attribute, not a formal parameter.When using
this
to access properties and methods, if they are not found in this class, they will be searched from the parent class.
class Person{ // 定义Person类 private String name ; private int age ; public Person(String name,int age){ this.name = name ; this.age = age ; } public void getInfo(){ System.out.println("姓名:" + name) ; this.speak(); } public void speak(){ System.out.println(“年龄:” + this.age); } }3.2 Calling the constructor In the constructor of the class, we can use
this.propertyIn the constructor of the class, we can explicitly useor
this.method
, call the properties or methods of the object currently being created. However, usually, we choose to omitthis.
. In special cases, if the formal parameters of the constructor have the same name as the attributes of the class, we must explicitly usethis.variable
to indicate that the variable is an attribute, not a formal parameter.
this (formal parameter list)The constructor cannot call its own constructor through theto call other constructors specified in this class.
this (formal parameter list)If there are n constructors in a class, thenmethod.
there are at most n - 1this (formal parameter list)this (formal parameter list)# is used in the constructor
## Regulations:
must be declared in the first lineof the
Inside the constructor,current constructor.
can only declare at most onethis (formal parameter list), which is used to call other constructors.
Except for the constructor, the compiler prohibits calling the constructor in any other method.
Code Demonstration:
class Person{ // 定义Person类 private String name ; private int age ; public Person(){ // 无参构造器 System.out.println("新对象实例化") ; } public Person(String name){ this(); // 调用本类中的无参构造器 this.name = name ; } public Person(String name,int age){ this(name) ; // 调用有一个参数的构造器 this.age = age; } public String getInfo(){ return "姓名:" + name + ",年龄:" + age ; } }3.3 Return the current object
public class Leaf { int i = 0; Leaf increment(){ i++; return this; } void print(){ System.out.println("i = "+i); } public static void main(String args[]){ Leaf x = new Leaf(); x.increment().increment().increment().print();//i = 3 }}2. Use of "super" keyword
is understood as: parent class(2) Use super in a Java class to call the specified operation in the parent class:
superto explicitly call the structure of the parent class in the subclass.can be used to access the ## defined in the parent class #Attributes
member methods
.supercan be used to call
defined in the parent class.
supercan be used to call the parent class'sconstructor
in the subclass constructor.Especially when a member with the same name appears in a child or parent class, you can use
superto indicate that the member in the parent class is being called. The tracing ofsuper
is not limited to the direct parent class. The usage ofsuper
is similar to
this
.this
represents the reference of this class object, andsuper
represents the memory space of the parent class. logo.2. Use
我们可以在子类的方法或构造器中。通过使用"
super.属性
“或”super.方法
“的方式,显式的调用父类中声明的属性或方法。但是,通常情况下,我们习惯省略”super.
"。
当子类和父类中定义了同名的属性时,我们要想在子类中调用父类中声明的属性,则必须显式的使用"
super.属性
"的方式,表明调用的是父类中声明的属性。
当子类重写了父类中的方法以后,我们想在子类的方法中调用父类中被重写的方法时,则必须显式的使用"
super.方法
"的方式,表明调用的是父类中被重写的方法。
(1)子类中所有的构造器默认都会访问父类中空参数的构造器。
(2)当父类中没有空参数的构造器时,子类的构造器必须通过this(参数列表)或者super(参数列表)语句指定调用本类或者父类中相应的构造器,否则编译出错。同时,只能”二选一”,不能同时出现,且必须放在构造器的首行。
(3)在类的多个构造器中,至少有一个类的构造器中使用了"super(形参列表)",调用父类中的构造器。
代码演示:
public class Person { private String name; private int age; private Date birthDate; public Person(String name, int age, Date d) { this.name = name; this.age = age; this.birthDate = d; } public Person(String name, int age) { this(name, age, null); } public Person(String name, Date d) { this(name, 30, d); } public Person(String name) { this(name, 30); } }public class Student extends Person { private String school; public Student(String name, int age, String s) { super(name, age); school = s; } public Student(String name, String s) { super(name); school = s; }// 编译出错: no super(),系统将调用父类无参数的构造器。 public Student(String s) { school = s; } }
No. | 区别点 | this | super |
---|---|---|---|
1 | 访问属性 | 访问本类中的属性,如果本类没有此属性则从父类中继续查找 | 直接访问父类中的属性 |
2 | 调用方法 | 访问本类中的方法,如果本类没有此方法则从父类中继续查找 | 直接访问父类中的方法 |
3 | 调用构造器 | 调用本类构造器,必须放在构造器的首行 | 调用父类构造器,必须放在子类构造器的首行 |
(1)从结果上来看:(继承性)
子类继承父类以后,就获取了父类中声明的属性或方法。
创建子类的对象,在堆空间中,就会加载所有父类中声明的属性。
(2)从过程上来看:
当我们通过子类的构造器创建子类对象时,我们一定会直接或间接的调用其父类的构造器,进而调用父类的父类的构造器,…直到调用了java.lang.Object
类中空参的构造器为止。正因为加载过所有的父类的结构,所以才可以看到内存中有父类中的结构,子类对象才可以考虑进行调用。
(3)明确:虽然创建子类对象时,调用了父类的构造器,但是自始至终就创建过一个对象,即为
new
的子类对象。
推荐学习:《java视频教程》
The above is the detailed content of Detailed analysis of Java's this and super keywords. For more information, please follow other related articles on the PHP Chinese website!