学习7.3视频,根据视频改写usb应用程序如下。请将程序空缺位置补充完整:
//定义出一个USB的标准
interface USB { // 操作标准
public void install();
public void work();
}
//在电脑上应用此接口
class Computer {
public void plugin(USB usb) { // 接收USB接口实例
第一空 ; // 调用install()接口方法
第二空 ; // 调用work()接口方法
}
}
//定义USB设备手机
class Phone implements USB { // 实现USB接口
public void install() {
System.out.println("安装手机驱动程序。") ;
}
public void work() {
System.out.println("手机与电脑进行工作。") ;
}
}
//定义USB设备打印机
class Print implements USB { // 实现USB接口
public void install() {
System.out.println("安装打印机驱动程序。") ;
}
public void work() {
System.out.println("进行文件打印。") ;
}
}
//连接USB设备
public class TestDemo {
public static void main(String args[]) {
Computer c = 第三空 ; //实例化Computer()
c.plugin(new 第四空 ) ; // 调用Phone设备相关操作;
c.plugin(new 第五空 ) ; // 调用Print相关操作 ;
}
}
//定义出一个USB的标准
interface USB { // 操作标准
public void install();
public void work();
}
//在电脑上应用此接口
class Computer {
public void plugin(USB usb) { // 接收USB接口实例
第一空 ; // 调用install()接口方法
第二空 ; // 调用work()接口方法
}
}
//定义USB设备手机
class Phone implements USB { // 实现USB接口
public void install() {
System.out.println("安装手机驱动程序。") ;
}
public void work() {
System.out.println("手机与电脑进行工作。") ;
}
}
//定义USB设备打印机
class Print implements USB { // 实现USB接口
public void install() {
System.out.println("安装打印机驱动程序。") ;
}
public void work() {
System.out.println("进行文件打印。") ;
}
}
//连接USB设备
public class TestDemo {
public static void main(String args[]) {
Computer c = 第三空 ; //实例化Computer()
c.plugin(new 第四空 ) ; // 调用Phone设备相关操作;
c.plugin(new 第五空 ) ; // 调用Print相关操作 ;
}
}
举一反三
- 中国大学MOOC: 下列代码中构造方法的返回类型是()public class Village { Village () { System .out .println(“hiding in Village”) ; } public static void main( String args [ ]) { Village c =new Village ( ) ;}class Village { public static void main( String args [ ]) { Village c =new Village ( ) ; } Village () { System .out .println(“hiding in Village”) ; } }
- 下列程序运行结果是 public class Demo { public static void main(String[] args) { Object obj=new Father(){ public void show(){ System.out.println("helloworld"); } }; obj.show(); } } class Father{ public void show(){ System.out.println("hello father"); } }
- public class Example { public static void main(String[] args) { new Father () { public void show() { System.out.println("helloworld"); } }.show(); } } class Father { public void show() { System.out.println("hellofather"); } }
- 下列程序运行结果是( ) public class Demo { public static void main(String[] args) { Demo demo = new Demo(); demo.show(new Car() { public void run() { System.out.println("demo run"); } }); } public void show(Car c) { c.run(); } }abstract class Car { public void run() { System.out.println("car run..."); } }
- 请阅读下面的程序,写出最终的结果: interface Inter { public void show(); } abstract class AbstractInter implements Inter { public void show() { System.out.println("AbstractInter show()"); } } class InterImpl extends AbstractInter { public void show() { System.out.println("InterImpl show()"); } } public class InterImplTest { public static void main(String[] args) { InterImpl i = new InterImpl(); i. show(); } }