package day01;
public class Employee {
String name;
int num;
public Employee() {
System.out.println("wuc fu Employee");
}
public Employee(String name,int num){
System.out.println("youc fu Employee");
this.name=name;
this.num=num;
}
public String getName() {
return name;
}
public int getNum() {
return num;
}
public void setName(String name) {
this.name = name;
}
public void setNum(int num) {
this.num = num;
}
public void m(){
System.out.println("我想你了"+this.name);
}
}
package day01;
public class Teacher extends Employee{//一个子类只能有一个父亲,父亲有多个儿子
public Teacher() {
System.out.println("wuc zi Teacher");
}
public Teacher(String name,int num){
super(name,num);//super可以调用父类有参方法
System.out.println("youc zi Teacher");
/*this.name=name;
this.num=num;*/
}
@Override //检测是否重写正确
public void m(){
System.out.println("我是老师");
System.out.println("我想你了"+this.name);
System.out.println("我想你了"+super.name);//super是指向父类
}
}
package day01;
public class It extends Employee{
public It() {
System.out.println("wuc zi IT");
}
public It(String name,int num){
super(name,num);//super可以调用父类有参方法 这样就不用自动调用无参的了
System.out.println("youc zi IT");
/* this.name=name;
this.num=num;*/
}
@Override //检测是否重写正确
public void m(){
System.out.println("我是程序员");
System.out.println("我想你了"+this.name);
System.out.println("我想你了"+super.name);//super指向父类
}
}
package day01;
public class Case13 {
public static void main(String[] args) {
It a=new It("Lucken",18); //子类 //先调用父类无参构造方法,再调用子类构造方法
Teacher b=new Teacher("luying",22); //子类
/* a.name="Lucken";
b.name="luying";
a.num=18;
b.num=22;*/
a.m(); //子类特有方法 可以用父类(先用子类,子类没有用父类)
//(子类返回值范围要小于父类,子类的权限要大于等于父类)
b.m(); //子类特有方法 可以用父类
Employee c=new Employee(); //父类不能用子类方法
c.name="jae";
c.num=21;
c.m();
}
}