import java.util.Scanner;
//创建一个接口类iPhone这个接口有方法但是没有方法体
interface iphone {
public void kaiji();
public void guanji();public void chongdian();public void tingge();public void useTypeC();
}
class TypeC implements iphone {
public void chongdian() {
}
public void tingge() {}public void kaiji() {}public void guanji() {}public void useTypeC() {System.out.println("选择TypeC功能");Scanner SC = new Scanner(System.in);//键盘输入String b = SC.nextLine();录入键盘输入的值保存chongdianqi cd = new chongdianqi();//创建一个充电器的对象CDerji tg = new erji();//创建一个erji对象 tgif (b.equals("充电")) {cd.chongdian();//这里如果要引用chongdianqi和erji里面的函数必须要先声明一个对象,然后通过对象去调用函数,才能引用} else if (b.equals("听歌")) {tg.tingge();} else {System.out.println("选择错误");}}}
//定义两个设备继承TypeC
class erji extends TypeC {
public void tingge() {
System.out.println(“正在听歌”);
}
}
//定义一个充电器类继承TypeC
class chongdianqi extends TypeC {
public void chongdian() {System.out.println("正在充电");}}
class kaiguan implements iphone{
public void kaiji(){
System.out.println(“开机”);
}
public void guanji(){System.out.println("关机");}public void chongdian(){}public void tingge(){}public void useTypeC(){}
}
class shebei {
public static void main(String[] args) {
TypeC B=new TypeC();//定义一个对象
iphone a = new kaiguan();//new一个对象 注意 不能用interface的对象去new 搞不出来的
a.kaiji();
B.useTypeC();//用TypeC()的对象调用函数
a.guanji();
}}
改良版本
public class charge extends TypeC {
public void charge(){
System.out.println(“正在充电”);
}
}
public class music extends TypeC{
public void music(){
System.out.println(“正在听歌”);
}
}
public class phone {
public void open(){
System.out.println(“开机”);
}
public void useTypeC(){
}public void close(){System.out.println("关机");}
}
class qidong {
public static void main(String[] args) {
phone a1 = new TypeC();
a1.open();
a1.useTypeC();
a1.close();
}
}
import java.util.Scanner;
public class TypeC extends phone {
public void useTypeC(){
System.out.println(“请选择功能充电还是听歌”);
Scanner TypeCuser = new Scanner(System.in);
String userName = TypeCuser.nextLine();
charge a =new charge();
music b = new music();
if(userName.equals("充电")){a.charge();}else if (userName.equals("听歌")){b.music();}}
}