day09--super和this关键字的作用
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package com.inmind.extends_this_super05;
|
||||
/*
|
||||
super关键字的作用:
|
||||
1.super,可以在子类的成员方法中,访问父类的成员变量,格式:super.父类成员变量名
|
||||
2.super,可以在子类,的成员方法中,访问父类的成员方法,格式:super.父类成员方法名
|
||||
3.super,可以在子类的构造方法中,访问父类的构造方法,但是只能在第一行,格式:super(参数列表)
|
||||
|
||||
super:表示父类的空间
|
||||
|
||||
|
||||
this关键字的作用:
|
||||
1.this,可以区分当前类中的局部变量和成员变量
|
||||
2.this,可以在本类中调用其他的成员方法
|
||||
3.this,可以在本类的构造方法中,调用其他的重载的构造方法
|
||||
关于第三点的注意事项:
|
||||
a.this必须在第一行,不能自己调用自己
|
||||
b.this和super在调用构造方法时,都必须在第一行,所以在同一个构造方法中,不能同时出现的
|
||||
c.this可以调用其他的重载的构造方法,不能相互调用
|
||||
*/
|
||||
public class Demo08 {
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.inmind.extends_this_super05;
|
||||
|
||||
public class Fu {
|
||||
int numFu = 10;
|
||||
|
||||
public Fu(){}
|
||||
|
||||
public Fu(int numFu){
|
||||
this.numFu = numFu;
|
||||
}
|
||||
|
||||
public void methodFu(){
|
||||
System.out.println("父类的methodFu方法");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.inmind.extends_this_super05;
|
||||
|
||||
public class Zi extends Fu{
|
||||
int numZi = 20;
|
||||
String name;
|
||||
|
||||
public Zi(){
|
||||
//调用有参构造即可
|
||||
this(0);
|
||||
}
|
||||
public Zi(int numZi){
|
||||
this(numZi, null);
|
||||
}
|
||||
|
||||
public Zi(int numZi ,String name){
|
||||
super();
|
||||
this.numZi = numZi;
|
||||
this.name = name;
|
||||
System.out.println("必须要执行的初始化操作1");
|
||||
System.out.println("必须要执行的初始化操作2");
|
||||
System.out.println("必须要执行的初始化操作3");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void method() {
|
||||
System.out.println(super.numFu);
|
||||
super.methodFu();
|
||||
int numZi = 30;
|
||||
System.out.println(numZi);
|
||||
System.out.println(this.numZi);
|
||||
this.method1();//调用自己其他的成员方法
|
||||
|
||||
}
|
||||
|
||||
public void method1(){
|
||||
System.out.println("子类的method1方法");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user