Core Java (十一) Java 继承,类,超类和子类

来源:互联网 发布:java if else同时执行 编辑:程序博客网 时间:2024/04/30 15:26

继承关系

两个类之间存在三种关系:

  1. 依赖,uses-a,如果一个类的方法操纵另一个对象,我们就说一个类依赖于另一个类。
  2. 聚合(关联),has-a,一个对象包含另外一个对象,聚合关系意味着类A的对象包含类B的对象。
  3. 继承,is-a,如果两个类之间存在明显的is-a(是)关系,例如每个经理都是雇员,那这两个类有继承关系。
例如:
class Manager extends Employee{    ......}
Manager继承了Employee类,继承可以重写超类的方法,也可以添加方法,即子类比超类拥有的功能更加丰富。


方法重写

当子类重写超类的方法时,也可以调用超类的同名方法,只需要使用super.method()。要注意,重写的方法不可低于超类方法的访问权限!
例如:
//重写getSalary方法public double getSalary(){double baseSalary = super.getSalary();//调用了超类的getSalary方法return baseSalary + bonus;}
子类重写了getSalary()方法,也调用了超类的同名方法。
另外,super()方法也可以在构造器中使用,以便调用超类的构造器方法。要注意的是super()方法必须是子类构造器的第一条语句。否则编译器会给出Constructor call must be the first statement in a constructor的错误提醒。例子见最后大例子。


多态和动态绑定

一个对象(例如下例中的e)能够引用多种实际类型的现象称为多态,在运行时能够自动的选择调用那个方法的想象成为动态绑定。Java不支持多重继承,仅支持单继承。
例如下面的大例子中,Emplyee类型的e可以引用Emplyee类型,Manager类型以及Boss类型的对象,当调用e.getSalary()的时候,编译器知道具体的e的实际类型,从而准确的调用该实际类型类的getSalary方法,如果不存在,那就调用其超类的该方法,这就是动态绑定。


final

阻止继承。
class Employee{......public final String getName(){return name;}......}final class Manager extends Employee{......}
如上例中,Manager类不可以再被继承了,而getName方法也不可被子类重写。


强制类型转换

进行强制类型转换的原因:在暂时忽视对象的实际类型之后,使用对象的全部功能。
仅可以在继承链上从上向下进行转换,如把实际类型是Manager的Employee类型的staff[1]转换成Manager类型。
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类型的实例。

抽象类中的方法有两种,一种是普通的,和一般类中的方法一样,另一种是抽象方法,起一个占位的作用,将来子类继承会实现这种方法。
类即使不含抽象方法,也可以将类声明为抽象方法。

抽象类的抽象方法可以用来实现多态性,例如下例中在抽象类中定义了一个getFee()方法,两个子类Employee和Student分别实现了它,但是用了不同的实现方法,当通过Person类型的p调用getFee()方法时,就会根据p的实际类型来调用确定的方法。

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;}

结果: