/**
* @author AdMin
*父类
*/
public abstract class Phones {
public abstract void call();
}
/**
* @author AdMin
*苹果手机类
*/
public class IPhone extends Phones {
@Override
public void call(){
System.out.println("苹果手机打电话");
}
}
/**
* @author AdMin
*安卓手机类
*/
public class APhone extends Phones {
@Override
public void call(){
System.out.println("安卓手机打电话");
}
}
/**
* @author AdMin
*
*/
public class WPhone extends Phones{
@Override
public void call() {
System.out.println("Windows Phone手机打电话");
}
}
public class PhonesTest {
/**
* @param args
*
* 测试类
*
*/
public static void main(String[] args) {
//方法1
// APhone ap1 = new APhone();
// IPhone ph1 =new IPhone();
// WPhone wp1=new WPhone();
// wp1.call();
// ph1.call();
// ap1.call();
// //方法二
// Phones ph =new IPhone();
// Phones ph1 = new APhone();
// Phones ph2 =new WPhone();
//
// ph.call();
// ph1.call();
// ph2.call();
//方法三
//首先生成一个子类也就是IPhone的实例
IPhone ip = new IPhone();
//使用向上转型,将子类的对象赋值给父类的引用
Phones ph =ip;
APhone ap =new APhone();
APhone ph1 =ap;
WPhone wp =new WPhone();
Phones ph2= wp;
//调用子类重写的父类方法
ip.call();
ap.call();
wp.call();
}
}
Java学习--重写父类call方法
最新推荐文章于 2024-11-27 21:38:39 发布