public class Vehicle {
public void way(){
System.out.println("我是父类 这是交通方法");
}
}
import com.tb.bean.Vehicle;
public class Air extends Vehicle {
@Override
public void way() {
System.out.println("我是飞机 我正在天上飞");
}
}
import com.tb.bean.Vehicle;
public class Bus extends Vehicle{
@Override
public void way() {
// TODO Auto-generated method stub
System.out.println("我是汽车 我正在地上跑");
}
}
import com.tb.bean.Vehicle;
import com.tb.bean.child.Air;
import com.tb.bean.child.Bus;
public class Main {
public static void main(String[] args) throws
InstantiationException, IllegalAccessException {
//字段永远是父类的 但是对于方法 它是运行时调用 在内存中是从对象的方法栈中弹出 首先弹出来的是子类的方法
//对于重写的方法 它首先弹出来的肯定是子类的
//但是由于先前在创建对象的时候就已经做好了字典 经过一对比
//发现和字典中的完全一样 它就默认是父类的了
Vehicle bus = new Bus();
bus.way();
Vehicle vehicle= new Vehicle();
System.out.println("vehicle-->"+vehicle.getClass().getName());
System.out.println("bus-->"+bus.getClass().getName());
//可以看出对象还是自己 只不过用父类的引用指向了我自己 由于继承的单方向性 这样的结果肯定会导致
//父类的引用不能直接调用父类中没有的成员
//也就是说 子类现有一些特性成员 在上向转型过程中是加了一层纱遮住了 不能让父类的引用使用
Vehicle air = new Air();
air.way();
}
}