1、父类
package com.wyq.study;
public class Employer {
private String name;
int age;
public static final int workNo = 10;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
// public void setAge(int age){
// this.age = age;
// }
// public int getAge(){
// return age;
// }
public Employer(){
super();
System.out.println("无参构造"+this.name+"\t"+this.age+"\t"+workNo);
}
public Employer(String name,int age){
super();
this.name = name;
this.age = age;
System.out.println("employer的带参构造"+this.name+"\t"+age+"\t"+workNo);
}
public final void waitt(String work){
System.out.println("我的工作单位是"+work);
}
}
2、子类
package com.wyq.study;
public class Employee extends Employer{
private String department;
public void setDepartment(String department){
this.department = department;
}
public String getDepartment(){
return department;
}
public Employee(){
super();
System.out.println("这里是子类的无参构造");
}
public Employee(String name,int age,String department){
super(name,age);
this.department = department;
System.out.println("这里是无参构造"+super.workNo+"\t"+super.getName()+"\t"+super.getClass()+"\t"+this.age+"\t"+super.workNo);
}
public void show(){
System.out.println(super.getName()+"\t"+super.age+super.workNo);
}
}
3、测试类
package com.wyq.study;
public class TestEmp {
public static void main(String[] args) throws InterruptedException {
Employee e = new Employee("李四",12,"财务部");
e.show();
System.out.println(e+"\t"+e.age+"\t"+e.getName()+"\t"+e.getDepartment()+"\t"+e.workNo);
Employer e1 = new Employee("李四",12,"财务部");
e.waitt("jsldfj");
e1.waitt("阿里巴巴集团");
}
}
4、输出结果
employer的带参构造李四 12 10
这里是无参构造10 李四 class com.wyq.study.Employee 12 10
李四 1210
com.wyq.study.Employee@30de3c87 12 李四 财务部 10
employer的带参构造李四 12 10
这里是无参构造10 李四 class com.wyq.study.Employee 12 10
我的工作单位是jsldfj
我的工作单位是阿里巴巴集团
5、总结
1)final可以修饰属性,被final修饰的属性一般为常量,其值不能被更改
2)final可以修饰方法,被final修饰的方法,不能被子类重写,但是子类可以调用
3)final也可以修饰类,被final修饰的类不能有子类,比如像math类
4)final和abstract是互斥的关系