目录
this:
1.局部变量调用成员变量this.变量名
2.构造调用构造,不可无参、含参同时进行this.()/this("你好",12)
3.方法调用方法(本类里调用)this.方法();
super:
1.调用父类的成员变量 super.变量名
2.调用父类的构造,不可无参、含参同时进行super()/super("你好",12)
3.方法的调用super.方法名();
父类的创建:
blic class Teacher {
String name = "America";//定义属性赋值
int a=600;
public Teacher() {
System.out.println("父类无参");
}
public Teacher(String n) {
this.name = n;
System.out.println("父类有参" + n);
}
public void play() {
System.out.println("语文");
}
}
子类的创建:继承父类
package text;
public class Student extends Teacher {
int a = 10;//成员变量
public Student() {//1.创建无参
super("66");
System.out.println("无参");
}
public void listen() {//方法二
this.print();//本类的普通方法之间的调用
System.out.println("Listening.");
}
public Student(String name) {//1.创建含参
this();//本类的无参构造之间的调用
//super("China");
System.out.println("有参");
}
public void print() {//方法一
super.play();//调用父类的普通方法
int a=20;//局部变量
System.out.println("姓名");
System.out.println(this.a);
System.out.println(super.a);//获取父类的成员变量
//this.listen();
}
public static void main(String[] args) {
Student s = new Student();
Student s2 = new Student("白");
s.print();
//System.out.println("name的值为:" + s.name);
}
}