package test;
import java.util.Scanner;
/*1、子类实例化的过程中父类的构造器先被调用,然后再调用子类的构造器,在子类构造器的内部默认的调用super();
* 2、如果父类带有有参数的构造器,并且没有显式的定义父类默认的构造器,默认
* 构造器被富凯,导致子类的构造器无法通过super()来做调用,就会编译报错,需要显式的定义父类的
* 默认的构造器。
* 2、不能在子类的构造器中调用两个super();
*/
class Teacher{
String name;
int age;
public Teacher() {
System.out.println("父类的构造器被调用了");
}
public Teacher(String name) {
this.name = name;
}
public void sleep() {
System.out.println(name+"在睡觉");
}
}
class JavaTeacher extends Teacher{
boolean isGlass;
public JavaTeacher() {
//隐藏调用父类的构造器
//super();写与不写效果相同
super("肖战");//调用第二个有参数的构造器,不调用默认构造器
System.out.println("子类的构造器被调用了");
}
public void teachJava() {
System.out.print(name + "在教java");
}
}
public class Demo1{
public static void main(String[] args) {
JavaTeacher jt = new JavaTeacher();
jt.teachJava();
}
}
///--------属性-----------///
/*class Teacher{
String name = "李逵";
int age;
public void sleep() {
System.out.println(name+"在睡觉");
}
}
class JavaTeacher extends Teacher{
boolean isGlass;
String name = "李鬼";
public void teachJava() {
//如果父子类有重名属性 this就是子类的name
System.out.println(this.name+"教java");
//super是父类的name
System.out.println(super.name+"教java");
}
}
public class Demo1{
public static void main(String[] args) {
JavaTeacher jt = new JavaTeacher();
jt.teachJava();
}
}*/