方法的查找
实例方法查找
从对象创建时的类开始,沿类层次向上查找
Manager man = new Manager();
Employee emp1 = new Employee();
Employee emp2 = (Employee)man;
emp1.ComputePay(); //调用Employee类中的ComputePay()方法
man.ComputePay(); //调用的是Manager类中的ComputePay()方法
emp2.ComputePay(); //调用Manager类中的ComputePay()方法
类方法查找
static
Manager man = new Manager();
Employee emp1 = new Employee();
Employee emp2 = (Employee)man;
man.expenseAllowance(); //调用的是Manager类中的expenseAllowance方法
emp1.expenseAllowance(); //调用Employee类中的expenseAllowance方法
emp2.expenseAllowance(); //调用Employee类中的expenseAllowance方法
多态的概念
不同类型的对象可以响应相同的消息,各自对于这个消息的响应行为可以是不同的
超类对象和从相同的超类派生出来的多个子类的对象可以被当作同一种类型的对象对待
实现同一接口不同类型的对象,可被当做同一种类型的对象对待
可向这些不同的类型对象发送同样的消息,由于多态性,这些不同类的对象响应同一消息的行为可以有所差别
绑定
将一个方法调用表达式与方法体的代码结合
早绑定:程序运行之前执行绑定
编译的时候发生的绑定
晚绑定:“动态绑定”或 “运行期绑定”,是基于对象的类别,在程序运行时执行绑定
多态的例子——二次引用
package drive;
public class Vehicle {
public void drivedByFemaleDriver() {
}
public void drivedByMaleDriver() {
}
}
class Bus extends Vehicle{
public void drivedByFemaleDriver() {
System.out.println("drivedByFemaleDriver");
}
public void drivedByMaleDriver() {
System.out.println("drivedByMaleDriver");
}
}
class Car extends Vehicle{
public void drivedByFemaleDriver() {
System.out.println("drivedByFemaleDriver");
}
public void drivedByMaleDriver() {
System.out.println("drivedByMaleDriver");
}
}
package drive;
public abstract class Driver {
public abstract void driver(Vehicle ve);
}
class FemaleDriver extends Driver{
public FemaleDriver(){
}
@Override
public void driver(Vehicle ve) {
ve.drivedByFemaleDriver();
}
}
class MaleDriver extends Driver{
public MaleDriver(){
}
@Override
public void driver(Vehicle ve) {
ve.drivedByMaleDriver();
}
}
package drive;
public class DriverTest {
public static void main(String[] args) {
Driver a = new FemaleDriver();
Driver b = new MaleDriver();
Vehicle x = new Car();
Vehicle y = new Bus();
a.driver(x);
b.driver(y);
}
}
构造方法与多态性
构造子类对象时构造方法的调用顺序
首先调用超类的构造方法,这个步骤会不断重复下去,首先被执行的是最远超类的构造方法;
然后执行当前子类对象的构造方法体其他语句。
//子类不能直接存取父类中声明的私有数据成员,super.toString调用父类Ball的toString方法输出类Ball中声明的属性值
package ball;
public class Point {
private double xCoordinate;
private double yCoordinate;
public Point(){
}
public Point(double x,double y){
xCoordinate = x;
yCoordinate = y;
}
public String toString(){
return "("+Double.toString(xCoordinate)+","+Double.toString(yCoordinate)+")";
}
}
package ball;
public class Ball {
private Point center;
private double radius;
private String colour;
public Ball(){}
public Ball(double xValue,double yValue,double r){
center = new Point(xValue,yValue);
radius = r;
}
public Ball(double xValue,double yValue,double r,String c){
this(xValue,yValue,r);
colour = c;
}
public String toString(){
return "A ball with center "+center.toString()+",radius "+Double.toString(radius)+",colour "+colour;
}
}
package ball;
public class MovingBall extends Ball{
private double speed;
public MovingBall(){}
public MovingBall(double xValue,double yValue,double r,String c,double s){
super(xValue,yValue,r,c);
speed = s;
}
public String toString(){
return super.toString()+",speed "+Double.toString(speed);
}
}
//子类不能直接存取父类中声明的私有数据成员,super.toString调用父类Ball的toString方法输出类Ball中声明的属性值
package ball;
public class Tester {
public static void main(String[] args) {
MovingBall mb = new MovingBall(10,20,40,"green",25);
System.out.println(mb);
}
}