20190218课堂作业<接口>

本文通过实例展示了面向对象编程(OOP)在手机和电脑类设计中的应用,包括继承、接口实现及多态性,突出了父类与子类间的关系以及接口在组件组装中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

练习1 手机

base:

public class Handset {
    public String brand;
    public String type;
    
    public void sendInfo(){
            System.out.println("发送短信.");}
    public void call(){
        System.out.println("拨打电话.");}
    public void info(){
        System.out.println("收取短信");}
    
    public void toStringInfo(){
        sendInfo();call();info();}}

 interface:

public interface Network {
    void networkConn();}
public interface PlayWiring {
    void play(String content);}
public interface TheakePictures {
    void takePictures();}

实现类_智能手机:

public class AptitudeHandset extends Handset implements TheakePictures,
        Network, PlayWiring {

    public void play(String content) {
        System.out.println("智能播放" + content + ".");
    }

    public void networkConn() {
        System.out.println("上网.");
    }

    public void takePictures() {
        System.out.println("拍照.");
    }

    public void toStringInfo() {
        System.out.println("智能手机能:");
        String content = "音频内容";
        super.toStringInfo();
        play(content);
        networkConn();
        takePictures();
    }

    public static void main(String[] args) {
        AptitudeHandset iphone4 = new AptitudeHandset();
        iphone4.toStringInfo();
    }
}

输出结果:

智能手机能:
发送短信.
拨打电话.
收取短信
智能播放音频内容.
上网.
拍照.

这里发现一个父类指向子类对象的问题:

父类 a = new 子类

那么对象a所调取的方法,是父类所含的方法,如果子类有对此方法进行重写的话,会调取子类的重写方法,但是,子类所特有的方法无法被调取(即父类没有此方法).它的精髓所在,这样就可以做到只关注父类所包含的方法,而利用子类进行优化,而子类可以多继承,所以换子类成本很低,不需要动到父类.举个例子:如练习1,手机是父类,它包含通话,发短信.智能手机,与普通手机是子类.所以 手机 a = new 智能手机 ,智能手机可以重写通话,重写成智能手机具备的特性,利用网络通话,未来智能手机可能利用手表通话,这个时候只要变动子类即可.

//-----------------------------------华丽丽的分割线--------------------------------------------------------------------------

实现类_普通手机:

public class CommonHandset extends Handset implements PlayWiring {

    public void play(String content) {
        System.out.print("播放" + content);
    }

    public void toStringInfo() {
        System.out.println("普通手机能:");
        super.toStringInfo();
        play("音频内容");
    }

    public static void main(String[] args) {
        CommonHandset phone = new CommonHandset();
        phone.toStringInfo();
    }
}

输出结果:

普通手机能:
发送短信.
拨打电话.
收取短信
播放音频内容

练习2 电脑

 接口类

public interface CPU {
    String CPUBrand();
    String CPUZhupin();
}
public interface EMS {
    String getEMSType();
    String getCapacity();
}
public interface HardDisk {
    String getCapacity();
}

实现类

public class CPUImpl implements CPU {

    public String CPUBrand() {
        return "英特尓";
    }

    public String CPUZhupin() {
        return "3.14Hz";
    }
}
public class EMSImpl implements EMS{

    public String getEMSType() {
        return "金士顿DDR5";
    }

    public String getCapacity() {
        return "8G";
    }
}
public class HardDiskImpl implements HardDisk{

    public String getCapacity() {
        return "1T";
    }
}

测试类

public class Computer {
    CPU CPU;
    EMS EMS;
    HardDisk HardDisk;
    
    public Computer (CPU cpu,EMS ems,HardDisk harddisk){
        this.CPU = cpu;
        this.EMS = ems;
        this.HardDisk = harddisk;
    }
    
    public void DIY(){
        System.out.println("DIY组装完成,配置如下:"+"CPU品牌:"+CPU.CPUBrand()+
                ",CPU主频:"+CPU.CPUZhupin()+",内存类型"+EMS.getEMSType()+
                ",内存容量:"+EMS.getCapacity()+",硬盘容量"+HardDisk.getCapacity());
    }
    
    public static void main(String[] args) {
        //各个配置的具体实现,父类指向子类对象,限制智能用接口的抽象方法
        CPU cpu = new CPUImpl(); 
        EMS ems = new EMSImpl();
        HardDisk harddisk = new HardDiskImpl();
                
        Computer computer = new Computer(cpu,ems,harddisk);
        computer.DIY();
    }
}

输出

DIY组装完成,配置如下:CPU品牌:英特尓,CPU主频:3.14Hz,内存类型金士顿DDR5,内存容量:8G,硬盘容量1T

 

转载于:https://www.cnblogs.com/yanyu19/p/10394357.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值