//先附上一段代码作为例子,该例子出自Java核心技术书
package inheritanceTest;
import java.util.Date;
import java.util.GregorianCalendar;
public class ManagerTest
{
public static void main(String[] args)
{
Manager boss = new Manager("zack",80000,1994,6,16);
boss.setBonus(5000);
Employee[] staff = new Employee[3];
staff[0] = boss;
staff[1] = new Employee("tom",50000,1992,5,5);
staff[2] = new Employee("jack",40000,1990,3,15);
for(Employee e : staff)
{
System.out.println("name="+e.getName()+",salary="+e.getSalary());//动态绑定!!!
}
}
}
//Employee类
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 calender = new GregorianCalendar(year,month-1,day);
hireDay = calender.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;
}
}
//Manager类
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;
}
//重写Employee类中的getSalary方法
public double getSalary()
{
double baseSalary = super.getSalary();
return baseSalary + bonus;
}
public void setBonus(double b)
{
bonus = b;
}
}
Employee为子类 Manager为父类,即Manager继承了Employee类
Employee类中的getName,getHireDay方法为public属性,因此Manager类自动继承父类中的这些方法,无需显示定义;同时也从父类中继承了name,salary,hireDay这3个域
若父类中的一些方法子类不适用时,则子类可以重写(覆盖)父类中的方法,例子中的getSalary()方法
注意:使用super调用构造器的语句必须是子类构造器的第一条语句
本例中的e是Employee类的对象,e.getSalary()可以确定是执行父类中的getSalary()方法还是子类中的getSalary()方法,所以e既可以引用Employee类型的对象也可以引用Manager类型的对象
多态:一个对象变量可以引用多种实际类型的现象称为多态(polymorphism)
动态绑定:在运行时自动选择调用哪个方法的现象称为动态绑定(dynamic binding)
其他概念:
1.抽象类:
抽象类可以包含抽象方法,具体数据,具体方法
抽象类不能被实例化
但是可以定义抽象类的对象变量,但它只能引用非抽象子类的对象
e.g:Person p = new Student();
示例代码如下:
package personTest;
import java.util.Date;
import java.util.GregorianCalendar;
public class Test
{
public static void main(String[] args)
{
Person[] people = new Person[2];
//Person test = new Person("ss");抽象类不能被实例化
people[0] = new Employee("tom",50000,1993,1,3);
people[1] = new Student("jack","computer science");
for(Person p : people)
{
System.out.println(p.getName()+","+p.getDescription()); //动态绑定!!!
}
}
}
//抽象Person类
abstract class Person
{
private String name;
public Person(String n)
{
name = n;
}
public String getName()
{
return name;
}
public abstract String getDescription();
}
//具体Employee实现类
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 void raiseSalary(double byPercent)
{
double raise = salary*byPercent/100;
salary += raise;
}
//具体实现抽象类中的方法
@Override
public String getDescription()
{
return String.format("an employee with a salary of $%.2f", salary);
}
}
//具体Student实现类
class Student extends Person
{
private String major;
public Student(String n,String m)
{
super(n);
major = m;
}
@Override
public String getDescription()
{
return "a student majoring in "+major;
}
}
因为Person是抽象类,所以不能对Person类进行实例化,所以变量p不会引用Person对象,引用的是Employee和Student具体的子类对象
但是如果在抽象类中去掉getDescription()抽象方法,在具体的子类中定义的话,就不能通过Person类型的变量p调用getDescription()方法了,因为getDescription()方法不在Person类中了,即没有用到动态绑定的机制。
2.访问修饰符总结:
private---仅对本类可见
public---对所有类可见
protected---对本包和所有子类可见
默认---对本包可见
3.ArrayList的使用
package arraylistTest;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
public class ArrayListTest
{
public static void main(String[] args)
{
ArrayList<Employee> staff = new ArrayList<Employee>();
staff.add(new Employee("tom",5000,1993,3,4));
staff.add(new Employee("jack",4000,1992,3,5));
for(Employee e : staff)
e.raiseSalary(5);
for(Employee e : staff)
System.out.println("name="+e.getName()+",salary="+e.getSalary()+",hireDay="+e.getHireDay());
}
}
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;
}
}
4.反射---待学习