建立父类Telphone
package jk;
public abstract class Telphone {
public abstract void Call();
public abstract void Send();
}
建立接口IPlayGame
package jk;
public interface IPlayGame {
public void playGame();
}
建立子类elderPhone
package jk;
public class elderPhone extends Telphone{
public void Call() {
System.out.println("键盘电话!");
}
public void Send() {
System.out.println("键盘短信!");
}
}
建立子类smartPhone,并在此类中实现接口IPlayGame
package jk;
public class smartPhone extends Telphone implements IPlayGame{
public void Call() {
System.out.println("语音电话!");
}
public void Send() {
System.out.println("语音短信!");
}
public void playGame() {
// TODO Auto-generated method stub
System.out.println("智能手机具有了玩游戏的功能!");
}
}
建立类Psp
package jk;
public class Psp implements IPlayGame{
public void playGame() {
// TODO Auto-generated method stub
System.out.println("Psp具有了玩游戏的功能!");
}
}
测试
package jk;
public class Test {
public static void main(String[] args) {
// TODO 自动生成的方法存根
//接口的引用指向一个实现了接口的对象
IPlayGame ip1=new smartPhone();
ip1.playGame();
IPlayGame ip2=new Psp();
ip2.playGame();
}
}