两个类之间存在三种关系:
class Manager extends Employee{
......
}
//重写getSalary方法
public double getSalary(){
double baseSalary = super.getSalary();//调用了超类的getSalary方法
return baseSalary + bonus;
}
子类重写了getSalary()方法,也调用了超类的同名方法。
class Employee{
......
public final String getName(){
return name;
}
......
}
final class Manager extends Employee{
......
}
如上例中,Manager类不可以再被继承了,而getName方法也不可被子类重写。
Manager man = (Manager)staff[1];
package com.xujin;
public class Test {
public static void main(String[] args) {
Employee[] staff = new Employee[3];
staff[0] = new Employee("Bob", 1000);
staff[1] = new Manager("Jim", 5000, 1000);
staff[2] = new Boss("Alice", 7000, 1000, 10000);
for(Employee e : staff)
System.out.println("class name:" + e.getClass().getName() + "\tid:" + e.getId() +
"\tname:" + e.getName() + "\tsalary:" + e.getSalary());
Manager man = (Manager)staff[1];
Boss boss = (Boss)staff[2];
System.out.println(man.getBonus());//类型转换后就可以使用实际类型的全部功能
System.out.println(boss.getAward());
//ClassCastException异常,不允许进行继承链上的从上到下的转换
//Boss myBoss = (Boss)staff[0];
//把instaceof运算符和类型转换组合起来,避免异常
if(staff[0] instanceof Boss){
System.out.println("staff[0] is a instace of Boss");
Boss myBoss = (Boss) staff[0];
}
else System.out.println("staff[0] isn't a instace of Boss");
if(staff[2] instanceof Boss){
System.out.println("staff[2] is a instace of Boss");
}
else System.out.println("staff[2] isn't a instace of Boss");
}
}
class Employee{
public Employee(String name){
this.name = name;
id = nextId;
nextId++;
}
public Employee(String name, double salary){
this(name);//调用另一构造器
this.salary = salary;
}
//定义访问器方法
public final String getName(){
return name;
}
public double getSalary(){
return salary;
}
public final int getId(){
return id;
}
//定义更改器方法
public final void setName(String name){
this.name = name;
}
public final void setSalary(double salary){
this.salary = salary;
}
public final void raiseSalary(double percent){
this.salary *= (1 + percent);
}
//定义变量
private String name = "";//实例域初始化
private double salary;
private int id;
private static int nextId = 1;
}
class Manager extends Employee{
public Manager(String name, double salary, double bonus){
super(name, salary);//super在构造器中的使用,可以调用超类的构造器
setBonus(bonus);
}
public double getBonus(){
return bonus;
}
//重写getSalary方法
public double getSalary(){
double baseSalary = super.getSalary();//调用了超类的getSalary方法
return baseSalary + bonus;
}
public void setBonus(double bonus){
this.bonus = bonus;
}
private double bonus;
}
final class Boss extends Manager{
public Boss(String name, double salary, double bonus, double award){
super(name, salary, bonus);
this.award = award;
}
//重写getSalary方法
public double getSalary(){
double baseSalary = super.getSalary();//调用了超类的getSalary方法
return baseSalary + award;
}
public double getAward(){
return award;
}
private double award;
}
Person p = new Student("joun", 17, 6000);
这里p是一个person类型的变量,但是它引用了Student类型的实例。
package com.xujin;
public class Test {
public static void main(String[] args) {
Person[] people = new Person[2];
people[0] = new Employee("Bod", 34, 5000);
people[1] = new Student("Joun", 17, 6000);
for(Person p: people){
System.out.print(
"Name:" + p.getName() +
"\tAge:" + p.getAge() +
"\tDescription:" + p.getDescription() + "\t");
if(p instanceof Employee){
System.out.println(((Employee) p).getFee());
}
else if(p instanceof Student)
System.out.println(((Student) p).getFee());
}
}
}
abstract class Person{
public Person(String name, int age){
this.name = name;
this.age = age;
}
public abstract String getDescription();
public final String getName(){
return this.name;
}
public final void setName(String name){
this.name = name;
}
public final int getAge(){
return this.age;
}
public final void setAge(int age){
this.age = age;
}
private String name;
private int age;
}
class Employee extends Person{
public Employee(String name, int age, double fee){
super(name, age);
id = nextId;
nextId++;
this.fee = fee;
}
//定义Person抽象类的抽象方法
public String getDescription(){
return "This is an employee. class name:" + this.getClass().getName();
}
//定义访问器方法
public double getFee(){
return fee * 2;
}
public final int getId(){
return id;
}
//定义更改器方法
public final void setFee(double salary){
this.fee = fee;
}
//定义变量
private double fee;
private int id;
private static int nextId = 1;
}
class Student extends Person{
public Student(String name, int age, double fee){
super(name, age);
this.fee = fee;
}
public String getDescription(){
return "This is a student. class name:" + this.getClass().getName();
}
public double getFee(){
return this.fee;
}
public void setFee(double fee){
this.fee = fee;
}
private double fee;
}