-
已知电脑有三个USB接口,分别去实例化一个鼠标,一个键盘,一个U盘来连接到电脑上。 输出每一个USB接口连接的设备信息
interface USB{ String getInfo(); } class Mouse implements USB{ @Override public String getInfo() { return "鼠标"; } } class KeyBroad implements USB{ @Override public String getInfo() { return "键盘"; } } class UPan implements USB{ @Override public String getInfo() { return "U盘"; } } class Computer{ private USB usb1; private USB usb2; private USB usb3; public void showUSB(){ System.out.println(usb1.getInfo()); System.out.println(usb2.getInfo()); System.out.println(usb3.getInfo()); } public Computer(){} public Computer(USB usb1, USB usb2, USB usb3) { this.usb1 = usb1; this.usb2 = usb2; this.usb3 = usb3; } } public class Work1 { public static void main(String[] args) { USB usb1 = new Mouse(); USB usb2 = new KeyBroad(); USB usb3 = new UPan(); Computer computer = new Computer(usb1,usb2,usb3); computer.showUSB(); } }
-
设计一个家政服务规范(接口): 洗衣服, 扫地, 买菜, 做饭 设计一个保姆类, 保姆需要遵循这些规范 需求:在测试类中,实例化一个保姆的对象,然后让保姆买菜,做饭,扫地
public class Work2 { public static void main(String[] args) { Nanny nanny = new Nanny(); nanny.wash(); nanny.clean(); nanny.buy(); nanny.cook(); } } interface skill{ void wash(); void clean(); void buy(); void cook(); } class Nanny implements skill{ @Override public void wash() { System.out.println("洗衣服"); } @Override public void clean() { System.out.println("扫地"); } @Override public void buy() { System.out.println("买菜"); } @Override public void cook() { System.out.println("做饭"); } }
-
设计一个动物类,属性: 姓名,性别
设计一个猫类,设计一个狗类, 猫和狗都继承自动物类
需求:在测试类中设计一个方法,这个方法需要一个参数,参数的类型可以是猫类,也
可以是狗类 -->多态(动物类 )
在方法体内,将传入的猫或者狗的属性输出即可 输出到底是猫还是狗
class Animal{ private String name; private String sex; public Animal(){} public Animal(String name, String sex) { this.name = name; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } class Cat extends Animal{ public Cat(){} public Cat(String name, String sex) { super(name, sex); } } class Dog extends Animal{ public Dog(){} public Dog(String name, String sex) { super(name, sex); } } public class Work3 { public static void main(String[] args) { Cat cat = new Cat("小花","女"); Dog dog = new Dog("二哈","男"); show(cat); show(dog); } public static void show(Animal animal){ if(animal instanceof Dog){ System.out.println("该动物是狗"); System.out.println(animal.getName()); System.out.println(animal.getSex()); } if(animal instanceof Cat){ System.out.println("该动物是猫"); System.out.println(animal.getName()); System.out.println(animal.getSex()); } } }
-
设计一个数据传输接口:Type-C标准
设计一个防水的接口:防水
设计一个手机类,实现这个数据传输接口,采用Type-C;同时实现了防水的接口
interface TypeC{ void typec(); } interface WaterProof{ void waterproof(); } class Phone implements TypeC,WaterProof{ private String name; public Phone(){} public Phone(String name) { this.name = name; } @Override public void typec() { System.out.println("具有Type-C接口"); } @Override public void waterproof() { System.out.println("防水"); } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class Work4 { public static void main(String[] args) { Phone phone = new Phone(); phone.typec(); phone.waterproof(); } }
-
(难)利用接口做参数,写个计算器,能完成加减乘除运算
5.1定义一个接口 Compute,含有一个计算方法 int compute(int a, int b)
5.2设计一个类 UseCompute,含有方法 public void useCom(Compute com, int one, int two) ,此方法能够用传递过来的对象调用 compute 方法完成运算,并输出计算的结果。
5.3设计一个主类 Test,调用 UseCompute 中的方法 useCom 来完成加减乘除运算。
interface Compute{ int compute(int a,int b); } class Add implements Compute{ @Override public int compute(int a, int b) { return a+b; } } class Sub implements Compute{ @Override public int compute(int a, int b) { return a-b; } } class Mul implements Compute{ @Override public int compute(int a, int b) { return a*b; } } class Div implements Compute{ @Override public int compute(int a, int b) { return a/b; } } class UseCompute{ public void useCom(Compute com, int one, int two){ System.out.println(com.compute(one,two)); } } public class Work5 { public static void main(String[] args) { UseCompute useCompute = new UseCompute(); Add s1 = new Add(); Sub s2 = new Sub(); Mul s3 = new Mul(); Div s4 = new Div(); System.out.print("和:");useCompute.useCom(s1,1,2); System.out.print("差:");useCompute.useCom(s2,3,4); System.out.print("积:");useCompute.useCom(s3,5,6); System.out.print("商:");useCompute.useCom(s4,7,8); } }
-
(难)模拟 Arrays.sort 方法,实现如下方法,实现对一个Person数组的排序,并用匿名内部类在调用方法的时候,作为第二个参数的实参。
MyArrays.sort(Person[] array, MyComparator comparator);
import java.util.Arrays; class Person{ public String name; public int age; public Person(){} public Person(String name,int age){ this.name = name; this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } } interface MyComparator{ int compare(Person p1,Person p2); } public class Work6 { public static void main(String[] args) { Person[] person = { new Person("七星球",7), new Person("六星球",6), new Person("五星球",5), new Person("四星球",4), new Person("三星球",3), new Person("二星球",2), new Person("一星球",1), }; //匿名内部类实现 sort(person, new MyComparator() { @Override public int compare(Person p1, Person p2) { return p1.age-p2.age; } }); System.out.println(Arrays.toString(person)); } public static void sort(Person[] array,MyComparator comparator){ for (int i = 0; i < array.length - 1; i++) { int minIndex = i; for (int j = i+1; j < array.length; j++) { if(comparator.compare(array[minIndex],array[j] )>0){ minIndex = j; } } if(minIndex != i){ Person temp = array[i]; array[i] = array[minIndex]; array[minIndex] = temp; } } } }
接口和内部类习题
最新推荐文章于 2021-08-18 11:34:43 发布