继承超类和子类利用继承,可以基于已存在的类构造一个新类。继承已存在的类就是复用这些类的方法和域,在此基础上,还可以添加一些新的方法和域。
已存在的类称为超类,新类称为子类。子类比超类拥有的功能更加丰富。
Java中的继承属于公有继承,子类无法直接访问超类的私有域。构建子类对象时,可以通过super实现对超类构造器的调用。
- public class HelloJava
- {
- 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());
- }
- }
复制代码- import java.util.Date;
- import java.util.GregorianCalendar;
- 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;
- }
- }
复制代码 多态
一个对象变量指示多种实际类型的现象被称为多态。
超类对象变量可以引用超类对象,也可以引用子类对象。
动态绑定在运行时能够自动地选择调用哪个方法的现象称为动态绑定。
对于private方法,static方法,final方法或者构造器,编译器可以准确地知道应该调用哪个方法,这种调用方式称为静态绑定。
与此对应的是,调用的方法依赖于隐式参数的实际类型,并且在运行时实现动态绑定,此时,虚拟机一定调用与所引用的对象的实际类型最合适的那个类的方法。
阻止继承:final类和方法如果某个类被声明为final,表示这个类不会有子类。
如果超类中的特定方法被声明为final,那么子类将不能重写这个方法。
final类中的所有方法自动地成为final方法。
对象类型转换只能在继承层次内进行类型转换。
可以将子类对象的引用赋值给超类变量。
在将超类转换成子类之前,应该使用instanceof进行检查。
抽象类如果类中至少有一个抽象方法,那么类本身必须被声明为抽象的。
抽象类中可以不含抽象方法。
抽象类不能通过new来实例化。
- public class HelloJava
- {
- public static void main(String[] args)
- {
- Person[] people = new Person[2];
-
- people[0] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
- people[1] = new Student("Maria Morris", "computer science");
-
- for (Person p : people)
- System.out.println(p.getName() + ", " + p.getDescription());
- }
- }
复制代码- public abstract class Person
- {
- public abstract String getDescription();
- private String name;
-
- public Person(String n)
- {
- name = n;
- }
-
- public String getName()
- {
- return name;
- }
- }
复制代码- import java.util.Date;
- import java.util.GregorianCalendar;
- 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)
- {
- super(n);
- major = m;
- }
-
- public String getDescription()
- {
- return "a student majoring in " + major;
- }
- }
复制代码 访问权限
private—仅对本类可见。
public—对所有类可见。
protected—对本包和所有子类可见。
默认—对本包可见。
Object:所有类的超类Object类是Java中所有类的始祖,每个类都是由它扩展而来。
在Java中,只有基本类型不是对象,所有的数组类型,都扩展于Object类。
Object类中的equals方法用于检测一个对象是否等于另外一个对象。
Object类中的hashCode方法用于返回对象的散列码。
Object类中的toString方法用于返回表示对象值的字符串。
- public class HelloJava
- {
- public static void main(String[] args)
- {
- Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
- Employee alice2 = alice1;
- Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
- Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);
-
- System.out.println("alice1 == alice2: " + (alice1 == alice2));
- System.out.println("alice1 == alice3: " + (alice1 == alice3));
- System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));
- System.out.println("alice1.equals(bob): " + alice1.equals(bob));
- System.out.println("bob.toString(): " + bob);
-
- Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
- Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
- boss.setBonus(5000);
-
- System.out.println("boss.toString(): " + boss);
- System.out.println("carl.equals(boss): " + carl.equals(boss));
- System.out.println("alice1.hashCode(): " + alice1.hashCode());
- System.out.println("alice3.hashCode(): " + alice3.hashCode());
- System.out.println("bob.hashCode(): " + bob.hashCode());
- System.out.println("carl.hashCode(): " + carl.hashCode());
- }
- }
复制代码- import java.util.Date;
- import java.util.GregorianCalendar;
- import java.util.Objects;
- 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 boolean equals(Object otherObject)
- {
- if (this == otherObject)
- return true;
-
- if (otherObject == null)
- return false;
-
- if (getClass() != otherObject.getClass())
- return false;
-
- Employee other = (Employee) otherObject;
-
- return Objects.equals(name, other.name) && salary == other.salary &&
- Objects.equals(hireDay, other.hireDay);
- }
-
- public int hashCode()
- {
- return Objects.hash(name, salary, hireDay);
- }
-
- public String toString()
- {
- return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
- }
- }
复制代码- 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 boolean equals(Object otherObject)
- {
- if (!super.equals(otherObject))
- return false;
-
- Manager other = (Manager) otherObject;
-
- return bonus == other.bonus;
- }
-
- public int hashCode()
- {
- return super.hashCode() + 17 * new Double(bonus).hashCode();
- }
-
- public String toString()
- {
- return super.toString() + "[bonus=" + bonus + "]";
- }
- }
复制代码
|
|