java-面向对象中级-继承(PC信息案列)

面向对象中级-继承(PC信息案列)

编写Computer类包含CPU、内存、硬盘等属性,getDetails方法用于返回Computer的详细信息
编写pc子类,继承Computer类,添加特有属性{品牌brand
编写Notepad子类,继承Computer类,添加特有属性{颜色color
编写Test类,在main方法中创建PC和Notepad对象,分别给对象中特有的属性赋值
以及从computer类继承属性赋值,并使用方法并打印输出
父类Computer

package com.hspedu.extend.exercise;
//Computer类包含CPU、内存、硬盘等属性,
// getDetails方法用于返回Computer的详细信息
public class Computer {
    private String cpu;
    private int memory;
    private int disk;

    public Computer(String cpu, int memory, int disk) {
        this.cpu = cpu;
        this.memory = memory;
        this.disk = disk;
    }
    //返回详细信息
    public String getDetails() {
        return "cpu=" + cpu + "memory=" + memory + "disk=" + disk;
    }
    public String getCpu() {
        return cpu;
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public int getMemory() {
        return memory;
    }

    public void setMemory(int memory) {
        this.memory = memory;
    }

    public int getDisk() {
        return disk;
    }

    public void setDisk(int disk) {
        this.disk = disk;
    }
}

子类PC

package com.hspedu.extend.exercise;

public class PC extends Computer{
    private String brand;

    public PC(String cpu, int memory, int disk, String brand) {
        super(cpu, memory, disk);
        this.brand = brand;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }
    public void printInfo(){
        System.out.println("pc信息:"+getDetails() + "brands=" + brand);
    }
}

子类Notepad

package com.hspedu.extend.exercise;

public class Notepad extends Computer{
    private String color;

    public Notepad(String cpu, int memory, int disk, String color) {
        super(cpu, memory, disk);
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void printInfo(){
        System.out.println("NotePad信息:"+getDetails() + "color=" + color);
    }
}

主类Test(Exercies03)

package com.hspedu.extend.exercise;

public class Exercies03 {
    /**
     * 编写Computer类包含CPU、内存、硬盘等属性,getDetails方法用于返回Computer的详细信息
     * 编写pc子类,继承Computer类,添加特有属性{品牌brand
     * 编写Notepad子类,继承Computer类,添加特有属性{颜色color
     * 编写Test类,在main方法中创建PC和Notepad对象,分别给对象中特有的属性赋值
     * 以及从computer类继承属性赋值,并使用方法并打印输出
     */
    public static void main(String[] args) {
        PC pc = new PC("intel",8,500,"AMB");
        pc.printInfo();
        Notepad notepad = new Notepad("麒麟",16,1000,"red");
        notepad.printInfo();
    }
}

运行结果
在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值