继承

来源:互联网 发布:可视化编程工具 编辑:程序博客网 时间:2024/06/05 14:53

extends

这里写图片描述

super

这里写图片描述

子类构造器

这里写图片描述

多态

public class Employee {   private String name;   private double salary;   private Date hireDay;   public Employee(String n, double s, int year, int month, int day)   {      name = n;      salary = s;      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);      hireDay = calendar.getTime();   }   public String getName()   {      return name;   }   public double getSalary()   {      return salary;   }   public Date getHireDay()   {      return hireDay;   }   public void raiseSalary(double byPercent)   {      double raise = salary * byPercent / 100;      salary += raise;   }}public class Manager extends Employee {   private double bonus;   public Manager(String n, double s, int year, int month, int day)   {      super(n, s, year, month, day);      bonus = 0;   }   public double getSalary()   {      double baseSalary = super.getSalary();      return baseSalary + bonus;   }   public void setBonus(double b)   {      bonus = b;   }}public class ManagerTest {   public static void main(String[] args) {      Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);      boss.setBonus(5000);      Employee[] staff = new Employee[3];      staff[0] = boss;      staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);      staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15);      for (Employee e : staff)         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());   }}

这里写图片描述

继承层次
这里写图片描述

理解方法调用
这里写图片描述
这里写图片描述

经典的练习题

public class A {    public String show(D obj) {        return ("A and D");    }    public String show(A obj) {        return ("A and A");    }}public class B extends A{    public String show(B obj) {        return ("B and B");    }    public String show(A obj) {        return ("B and A");    }}public class C extends B{}public class D extends B{}public class Test {    public static void main(String[] args) {        A a1 = new A();        A a2 = new B();        B b = new B();        C c = new C();        D d = new D();        System.out.println("1--" + a1.show(b)); //b向上转型为A 调用的是A类的show(A obj)  A and A        System.out.println("2--" + a1.show(c)); //c向上转型为A 调用的是A类的show(A obj)  A and A        System.out.println("3--" + a1.show(d)); //调用的是A类方法的show(D obj)          A and D        System.out.println("4--" + a2.show(b)); //b向上转型为A 调用的是B类的show(A obj)  B and A         System.out.println("5--" + a2.show(c)); //b向上转型为A 调用的是B类的show(A obj)  B and A        System.out.println("6--" + a2.show(d)); //调用的是A类方法的show(D obj)          A and D        System.out.println("7--" + b.show(b)); //调用的是B类的方法show(B obj)           B and B        System.out.println("8--" + b.show(c)); //调用的是B类的方法show(B obj)           B and B        System.out.println("9--" + b.show(d)); //调用的是A类的方法show(D obj)           A and D    }}
    这里a2是引用变量,为A类型,它引用的是B对象,因此按照上面那句话的意思是说有B来决定调用谁的方法,所以a2.show(b)应该要调用B中的show(B obj),产生的结果应该是“B and B”,但是为什么会与前面的运行结果产生差异呢?这里我们忽略了后面那句话“但是这儿被调用的方法必须是在超类中定义过的”,那么show(B obj)在A类中存在吗?根本就不存在!所以这句话在这里不适用?那么难道是这句话错误了?非也!其实这句话还隐含这这句话:它仍然要按照继承链中调用方法的优先级来确认。所以它才会在A类中找到show(A obj),同时由于B重写了该方法所以才会调用B类中的方法,否则就会调用A类中的方法。    所以多态机制遵循的原则概括为:当超类对象引用变量引用子类对象时,被引用对象的类型而不是引用变量的类型决定了调用谁的成员方法,但是这个被调用的方法必须是在超类中定义过的,也就是说被子类覆盖的方法,但是它仍然要根据继承链中方法调用的优先级来确认方法,该优先级为:this.show(O)、super.show(O)、this.show((super)O)、super.show((super)O)。

强制类型转换
这里写图片描述

抽象类

在使用抽象类时需要注意几点:
1、抽象类不能被实例化,实例化的工作应该交由它的子类来完成,它只需要有一个引用即可。
2、抽象方法必须由子类来进行重写。
3、只要包含一个抽象方法的抽象类,该方法必须要定义成抽象类,不管是否还包含有其他方法。
4、抽象类中可以包含具体的方法,当然也可以不包含抽象方法。
5、子类中的抽象方法不能与父类的抽象方法同名。
6、abstract不能与final并列修饰同一个类。
7、abstract 不能与private、static、final或native并列修饰同一个方法。

这里写图片描述

public abstract class Person {   public abstract String getDescription();   private String name;   public Person(String n)   {      name = n;   }   public String getName()   {      return name;   }}public class Employee extends Person {   private double salary;   private Date hireDay;   public Employee(String n, double s, int year, int month, int day)   {      super(n);      salary = s;      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);      hireDay = calendar.getTime();   }   public double getSalary()   {      return salary;   }   public Date getHireDay()   {      return hireDay;   }   public String getDescription()   {      return String.format("an employee with a salary of $%.2f", salary);   }   public void raiseSalary(double byPercent)   {      double raise = salary * byPercent / 100;      salary += raise;   }}public class Student extends Person {   private String major;   public Student(String n, String m)   {      // pass n to superclass constructor      super(n);      major = m;   }   public String getDescription()   {      return "a student majoring in " + major;   }public class PersonTest {   public static void main(String[] args) {      Person[] people = new Person[2];      // fill the people array with Student and Employee objects      people[0] = new Employee("Harry Hacker", 50000, 1989, 10, 1);      people[1] = new Student("Maria Morris", "computer science");      // print out names and descriptions of all Person objects      for (Person p : people)         System.out.println(p.getName() + ", " + p.getDescription());   }}

这里写图片描述

受保护的访问
这里写图片描述