A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 清香白莲 中级黑马   /  2015-4-6 19:29  /  534 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

继承超类和子类利用继承,可以基于已存在的类构造一个新类。继承已存在的类就是复用这些类的方法和域,在此基础上,还可以添加一些新的方法和域。
已存在的类称为超类,新类称为子类。子类比超类拥有的功能更加丰富。
Java中的继承属于公有继承,子类无法直接访问超类的私有域。构建子类对象时,可以通过super实现对超类构造器的调用。
  1. public class HelloJava
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
  6.                 boss.setBonus(5000);
  7.                
  8.                 Employee[] staff = new Employee[3];
  9.                
  10.                 staff[0] = boss;
  11.                 staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
  12.                 staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15);
  13.                
  14.                 for (Employee e : staff)
  15.                         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
  16.         }
  17. }
复制代码
  1. import java.util.Date;
  2. import java.util.GregorianCalendar;

  3. public class Employee
  4. {
  5.         private String name;
  6.         private double salary;
  7.         private Date hireDay;
  8.        
  9.         public Employee(String n, double s, int year, int month, int day)
  10.         {
  11.                 name = n;
  12.                 salary = s;
  13.                 GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
  14.                 hireDay = calendar.getTime();
  15.         }
  16.        
  17.         public String getName()
  18.         {
  19.                 return name;
  20.         }
  21.        
  22.         public double getSalary()
  23.         {
  24.                 return salary;
  25.         }
  26.        
  27.         public Date getHireDay()
  28.         {
  29.                 return hireDay;
  30.         }
  31.        
  32.         public void raiseSalary(double byPercent)
  33.         {
  34.                 double raise = salary * byPercent / 100;
  35.                 salary += raise;
  36.         }
  37. }
复制代码
  1. public class Manager extends Employee
  2. {
  3.         private double bonus;
  4.        
  5.         public Manager(String n, double s,int year, int month, int day)
  6.         {
  7.                 super(n, s, year, month, day);
  8.                 bonus = 0;
  9.         }
  10.        
  11.         public double getSalary()
  12.         {
  13.                 double baseSalary = super.getSalary();
  14.                 return baseSalary + bonus;
  15.         }
  16.        
  17.         public void setBonus(double b)
  18.         {
  19.                 bonus = b;
  20.         }
  21. }
复制代码
多态
一个对象变量指示多种实际类型的现象被称为多态。
超类对象变量可以引用超类对象,也可以引用子类对象。
动态绑定在运行时能够自动地选择调用哪个方法的现象称为动态绑定。
对于private方法,static方法,final方法或者构造器,编译器可以准确地知道应该调用哪个方法,这种调用方式称为静态绑定。
与此对应的是,调用的方法依赖于隐式参数的实际类型,并且在运行时实现动态绑定,此时,虚拟机一定调用与所引用的对象的实际类型最合适的那个类的方法。
阻止继承:final类和方法如果某个类被声明为final,表示这个类不会有子类。
如果超类中的特定方法被声明为final,那么子类将不能重写这个方法。
final类中的所有方法自动地成为final方法。
对象类型转换只能在继承层次内进行类型转换。
可以将子类对象的引用赋值给超类变量。
在将超类转换成子类之前,应该使用instanceof进行检查。
抽象类如果类中至少有一个抽象方法,那么类本身必须被声明为抽象的。
抽象类中可以不含抽象方法。
抽象类不能通过new来实例化。
  1. public class HelloJava
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Person[] people = new Person[2];
  6.                
  7.                 people[0] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
  8.                 people[1] = new Student("Maria Morris", "computer science");
  9.                
  10.                 for (Person p : people)
  11.                         System.out.println(p.getName() + ", " + p.getDescription());
  12.         }
  13. }
复制代码
  1. public abstract class Person
  2. {
  3.         public abstract String getDescription();
  4.         private String name;
  5.        
  6.         public Person(String n)
  7.         {
  8.                 name = n;
  9.         }
  10.        
  11.         public String getName()
  12.         {
  13.                 return name;
  14.         }
  15. }
复制代码
  1. import java.util.Date;
  2. import java.util.GregorianCalendar;

  3. public class Employee extends Person
  4. {
  5.         private double salary;
  6.         private Date hireDay;
  7.        
  8.         public Employee(String n, double s, int year, int month, int day)
  9.         {
  10.                 super(n);
  11.                 salary = s;
  12.                 GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
  13.                 hireDay = calendar.getTime();
  14.         }
  15.        
  16.         public double getSalary()
  17.         {
  18.                 return salary;
  19.         }
  20.        
  21.         public Date getHireDay()
  22.         {
  23.                 return hireDay;
  24.         }
  25.        
  26.         public String getDescription()
  27.         {
  28.                 return String.format("an employee with a salary of $%.2f", salary);
  29.         }
  30.        
  31.         public void raiseSalary(double byPercent)
  32.         {
  33.                 double raise = salary * byPercent / 100;
  34.                 salary += raise;
  35.         }
  36. }
复制代码
  1. public class Student extends Person
  2. {
  3.         private String major;
  4.        
  5.         public Student(String n, String m)
  6.         {
  7.                 super(n);
  8.                 major = m;
  9.         }
  10.        
  11.         public String getDescription()
  12.         {
  13.                 return "a student majoring in " + major;
  14.         }
  15. }
复制代码
访问权限
private—仅对本类可见。
public—对所有类可见。
protected—对本包和所有子类可见。
默认—对本包可见。
Object:所有类的超类Object类是Java中所有类的始祖,每个类都是由它扩展而来。
在Java中,只有基本类型不是对象,所有的数组类型,都扩展于Object类。
Object类中的equals方法用于检测一个对象是否等于另外一个对象。
Object类中的hashCode方法用于返回对象的散列码。
Object类中的toString方法用于返回表示对象值的字符串。

  1. public class HelloJava
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
  6.                 Employee alice2 = alice1;
  7.                 Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
  8.                 Employee bob = new Employee("Bob Brandson", 50000, 1989, 10, 1);
  9.                
  10.                 System.out.println("alice1 == alice2: " + (alice1 == alice2));
  11.                 System.out.println("alice1 == alice3: " + (alice1 == alice3));
  12.                 System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));
  13.                 System.out.println("alice1.equals(bob): " + alice1.equals(bob));
  14.                 System.out.println("bob.toString(): " + bob);
  15.                
  16.                 Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
  17.                 Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
  18.                 boss.setBonus(5000);
  19.                
  20.                 System.out.println("boss.toString(): " + boss);
  21.                 System.out.println("carl.equals(boss): " + carl.equals(boss));
  22.                 System.out.println("alice1.hashCode(): " + alice1.hashCode());
  23.                 System.out.println("alice3.hashCode(): " + alice3.hashCode());
  24.                 System.out.println("bob.hashCode(): " + bob.hashCode());
  25.                 System.out.println("carl.hashCode(): " + carl.hashCode());
  26.         }
  27. }
复制代码
  1. import java.util.Date;
  2. import java.util.GregorianCalendar;
  3. import java.util.Objects;

  4. public class Employee
  5. {
  6.         private String name;
  7.         private double salary;
  8.         private Date hireDay;
  9.        
  10.         public Employee(String n, double s, int year, int month, int day)
  11.         {
  12.                 name = n;
  13.                 salary = s;
  14.                 GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
  15.                 hireDay = calendar.getTime();
  16.         }
  17.        
  18.         public String getName()
  19.         {
  20.                 return name;
  21.         }
  22.        
  23.         public double getSalary()
  24.         {
  25.                 return salary;
  26.         }
  27.        
  28.         public Date getHireDay()
  29.         {
  30.                 return hireDay;
  31.         }
  32.        
  33.         public void raiseSalary(double byPercent)
  34.         {
  35.                 double raise = salary * byPercent / 100;
  36.                 salary += raise;
  37.         }
  38.        
  39.         public boolean equals(Object otherObject)
  40.         {
  41.                 if (this == otherObject)
  42.                         return true;
  43.                
  44.                 if (otherObject == null)
  45.                         return false;
  46.                
  47.                 if (getClass() != otherObject.getClass())
  48.                         return false;
  49.                
  50.                 Employee other = (Employee) otherObject;
  51.                
  52.                 return Objects.equals(name, other.name) && salary == other.salary &&
  53.                                 Objects.equals(hireDay, other.hireDay);
  54.         }
  55.        
  56.         public int hashCode()
  57.         {
  58.                 return Objects.hash(name, salary, hireDay);
  59.         }
  60.        
  61.         public String toString()
  62.         {
  63.                 return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";
  64.         }
  65. }
复制代码
  1. public class Manager extends Employee
  2. {
  3.         private double bonus;
  4.        
  5.         public Manager(String n, double s, int year, int month, int day)
  6.         {
  7.                 super(n, s, year, month, day);
  8.                 bonus = 0;
  9.         }
  10.        
  11.         public double getSalary()
  12.         {
  13.                 double baseSalary = super.getSalary();
  14.                 return baseSalary + bonus;
  15.         }
  16.        
  17.         public void setBonus(double b)
  18.         {
  19.                 bonus = b;
  20.         }
  21.        
  22.         public boolean equals(Object otherObject)
  23.         {
  24.                 if (!super.equals(otherObject))
  25.                         return false;
  26.                
  27.                 Manager other = (Manager) otherObject;
  28.                
  29.                 return bonus == other.bonus;
  30.         }
  31.        
  32.         public int hashCode()
  33.         {
  34.                 return super.hashCode() + 17 * new Double(bonus).hashCode();
  35.         }
  36.        
  37.         public String toString()
  38.         {
  39.                 return super.toString() + "[bonus=" + bonus + "]";
  40.         }
  41. }
复制代码




2 个回复

倒序浏览
学完继承有如下猜想:
创建子类对象时,会预先创建超类对象,而且子类对象与超类对象的内存首地址相同。
也就是说,从堆内存角度,子类对象包含了超类对象。
回复 使用道具 举报
不错 嘿嘿 谢谢啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马