day10-作业-7月26日接口

7.1 已知电脑有三个USB接口,分别去实例化一个鼠标,一个键盘,一个U盘来连接到电脑 上。 输出每一个USB接口连接的设备信息
public class Computer {
    public static void main(String[] args) {
        new UsbDrive("学习资料10G", "西数").conncetionShow();
        
        connection(new Usb() {
            @Override
            public void conncetionShow() {
                System.out.println("键盘已连接");
            }
        });
        connection(()-> System.out.println("鼠标已连接"));
    }
    public static void connection(Usb usb){
        usb.conncetionShow();
    }
}
class UsbDrive implements Usb{
    private String message;
    private String name;

    public UsbDrive(String message, String name) {
        this.message = message;
        this.name = name;
    }

    @Override
    public void conncetionShow() {
        System.out.println(this.name+"牌U盘读取的数据为"+message);
    }
}
7.2 设计一个家政服务规范(接口): 洗衣服, 扫地, 买菜, 做饭 设计一个保姆类, 保姆需要遵循这 些规范 需求:在测试类中,实例化一个保姆的对象,然后让保姆买菜,做饭,扫地
public class Nanny implements BuyVegetables,Cook,SweepTheFloor,WashClothes{
    public static void main(String[] args) {
        Nanny nanny = new Nanny();
        nanny.BuyVegetables();
        nanny.Cook();
        nanny.clean();
        nanny.wash();
    }
    public void BuyVegetables() {
        System.out.println("买菜");
    }

    @Override
    public void Cook() {
        System.out.println("做饭");
    }

    @Override
    public void clean() {
        System.out.println("扫地");
    }

    @Override
    public void wash() {
        System.out.println("洗衣服");
    }
}
public interface BuyVegetables {
    void BuyVegetables();
}
public interface Cook {
    void Cook();
}
public interface SweepTheFloor {
    void clean();
}
public interface WashClothes {
    void wash();
}
7.3 设计一个动物类,属性: 姓名,性别 设计一个猫类,设计一个狗类, 猫和狗都继承自动物类 需求:在测试类中设计一个方法,这个方法需要一个参数,参数的类型可以是猫类,也 可以是狗类 -->多态(动物类 ) 在方法体内,将传入的猫或者狗的属性输出即可 输出到底是猫还是狗
abstract public class Animal {
    String name;
    String sex;

    public Animal(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    public Animal() {
    }

    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;
    }
}
public class Cat extends Animal {
    public Cat(String name, String sex) {
        super(name, sex);
    }

    public Cat() {
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                "} ";
    }
}
public class Dog extends Animal{
    public Dog(String name, String sex) {
        super(name, sex);
    }

    public Dog() {
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                "} ";
    }
}
public class Test {
    public static void main(String[] args) {
        Cat cat = new Cat("波斯猫", "雌");
        Dog dog = new Dog("法斗", "雄");
        isDogOrCat(cat);
        isDogOrCat(dog);
    }
    public static void isDogOrCat(Animal animal){
        if(animal instanceof Cat){
            System.out.println(animal+"是猫类");
        }else if (animal instanceof Dog){
            System.out.println(animal+"是狗类");
        }
    }
}

7.4 设计一个数据传输接口:Type-C标准 设计一个防水的接口:防水 设计一个手机类,实现这个数据传输接口,采用Type-C;同时实现了防水的接口
public class Phone implements TypeC,Waterproof {
    String name;

    public Phone(String name) {
        this.name = name;
    }

    @Override
    public void implTypeC() {
        System.out.println(this.name+"牌手机采取TypeC接口数据传输");
    }

    @Override
    public void implWaterProof() {
        System.out.println(this.name+"牌手机防水标准10级");
    }

    public static void main(String[] args) {
        Phone mi = new Phone("小米");
        mi.implTypeC();
        mi.implWaterProof();
    }
}
public interface TypeC {
    void implTypeC();
}
public interface Waterproof {
    void implWaterProof();
}
7.5(难)利用接口做参数,写个计算器,能完成加减乘除运算
7.5.1定义一个接口 Compute,含有一个计算方法 int compute(int a, int b)
7.5.2设计一个类 UseCompute,含有方法 public void useCom(Compute com, int one, int two) ,此方法能够用传递过来的对象调用 compute 方法完成运算,并输出计算的结 果。
7.5.3设计一个主类 Test,调用 UseCompute 中的方法 useCom 来完成加减乘除运算。
public interface Compute {
    double compute(double o1,double o2);
}
public class Add implements Compute{
    @Override
    public double compute(double o1, double o2) {
        return o1+o2;
    }
}
public class Div implements Compute{
    @Override
    public double compute(double o1, double o2) {
        return o2!=0?o1/o2:0;
    }
}
public class Mul implements Compute {
    @Override
    public double compute(double o1, double o2) {
        return o1*o2;
    }
}
public class Sub implements Compute {
    @Override
    public double compute(double o1, double o2) {
        return o1-o2;
    }
}
public class UseCompute {
    public static void main(String[] args) {
        useCom(new Add(),1,2);
        useCom(new Sub(),1,2);
        useCom(new Mul(),1,2);
        useCom(new Div(),1,0);
    }
    public static void useCom(Compute com, int one, int two){
        System.out.println(com.compute(one,two));
    }
}
7.6 (难)模拟 Arrays.sort 方法,实现如下方法,实现对一个Person数组的排序,并用匿名 内部类在调用方法的时候,作为第二个参数的实参。
public interface MyComparator {
    void sort(Person[] p);
}
public class Person {
    private String name;
    private int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public Person() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class TestSort {
    public static void main(String[] args) {
        MyComparator comparator = getComparator();
        Person[] persons = initPersons();
        comparator.sort(persons);
        for (Person person : persons) {
            System.out.println(person);
        }
    }

    private static Person[] initPersons() {
        return new Person[]{
                    new Person("一",1),
                    new Person("五",5),
                    new Person("四",4),
                    new Person("二",2),
                    new Person("三",3),
            };
    }

    public static MyComparator getComparator(){ 
        MyComparator comparator = new MyComparator() {
            @Override
            public void sort(Person[] p) {
                Person temp = new Person();
                for (int i = 0; i < p.length; i++) {
                    for (int j = 0; j < p.length-i-1 ; j++) {
                        if(p[j].getAge()>p[j+1].getAge()){
                            temp.setName(p[j].getName());
                            temp.setAge(p[j].getAge());
                            p[j].setName(p[j+1].getName());
                            p[j].setAge(p[j+1].getAge());
                            p[j+1].setAge(temp.getAge());
                            p[j+1].setName(temp.getName());
                        }
                    }
                }
            }
        };
        return comparator;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值