栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 {
    
    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();
    }
}

运行结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/708908.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号