创建Laptop类
?代码如下:
public class Laptop extends Computer implements NetCard,USB {
private String name;
??? public String getName() { ??????? return name; ??? }
??? public void setName(String name) { ??????? this.name = name; ??? }
??? @Override ??? public void compute() { ??????? System.out.println("笔记本电脑["+ name +"]正在计算中......");
??? }
??? @Override ??? public void compute(String item) { ??????? System.out.println("笔记本电脑["+ name +"]正在计算["+ item +"].");
??? }
??? @Override ??? public void connectNetwork() { ??????? System.out.println("笔记本电脑[" + name +"]正在连接网络....");
??? }
??? @Override ??? public void connectUSB() { ??????? System.out.println("笔记本电脑["+ name +"]正在连接USB接口......");
??? } }
创建测试TextLaptop
代码如下:?
public class TestLaptop { ??? public static void main(String[] args)throws IOException { ??????? String name,item; ??????? BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
??????? System.out.println("笔记本电脑名:"); ??????? name = br.readLine(); ??????? System.out.println("计算机的项目名:"); ??????? item = br.readLine();
??????? //基于无参构造方法创建对象 ??????? Laptop laptop = new Laptop(); ??????? //设置对象属性 ??????? laptop.setName(name); ??????? //调用对象方法 ??????? laptop.connectUSB(); ??????? laptop.connectNetwork(); ??????? laptop.compute(); ??????? laptop.compute(item);
??? } }
创建NetCard
?
public interface NetCard { ??? void connectNetwork();//抽象方法,默认是public权限
??? default void speak() {//缺省方法(从JDK8开始) ??????? System.out.println("网络用于连接网络....");
??? }
??? static void tell() {// ??? } }
创建USB
?
?
|