Java类的继承与组合经典实例

电脑装配一显卡
Computer :电脑中有一个属性,属性类型就是显卡(组合关系)
Geforce 显卡的子类
Ati 显卡的子类
场景:程序员 xxx 去电脑专卖店购买了 xx 品牌的电脑,使用了一段时间之后(编程序、玩玩游
戏、看看电影等等),嫌电脑原装的集成显卡不够给力,
就去电脑专卖店购买了 Geforce 类型的独立显卡,并请店家安装上,使用了一段时
间之后(编程序、玩玩游戏、看看电影等等);听说 ati 类型的显卡
更好, 就去电脑专卖店购买了 Ati 类型的独立显卡,并请店家安装上,使用了一段
时间(编程序、玩玩游戏、看看电影等等)。
分析:
类:
程序员类:(测试类)
        特征:名字
        行为: 购买电脑
        使用电脑(编程序,玩游戏,看电影)
        换Geforce 类型的独立显卡
        换Ati 类型的独立显卡
电脑类:
        特征:品牌
        售价
        Geforce 类型独立显卡
        Ati 类型独立显卡
        行为:运行程序
        运行游戏
        播放视频
-----------------------------
Geforce 类型独立显卡类:
        特征:品牌
        售价
        分辨率
        行为:渲染图形
Ati 类型独立显卡类:
        特征:品牌
        售价
        分辨率
        行为:渲染图形
package com.qf.work;
class Computer{
    private VideoCard videoCard;
    private String  brand;
    private double  price;

    public Computer() {
    }

    public Computer(VideoCard videoCard, String brand, double price) {
        this.videoCard = videoCard;
        this.brand = brand;
        this.price = price;
    }

    public VideoCard getVideoCard() {
        return videoCard;
    }

    public void setVideoCard(VideoCard videoCard) {
        this.videoCard = videoCard;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "电脑配置:" +
                "(显卡类型:" + videoCard +'\t'+
                "品牌:" + brand + '\t' +
                "价格:" + price;
    }

    public void runProgram(){
        System.out.println("使用"+toString()+"运行程序");
    }
    public void runGame(){
        System.out.println("使用"+toString()+"运行游戏");
    }
    public void playVideo(){
        System.out.println("使用"+toString()+"播放视频");
    }
}
class VideoCard{
    private String  brand;
    private double price;
    private String resolution;

    public VideoCard() {
    }

    public VideoCard(String brand, double price, String resolution) {
        this.brand = brand;
        this.price = price;
        this.resolution = resolution;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getResolution() {
        return resolution;
    }

    public void setResolution(String resolution) {
        this.resolution = resolution;
    }

    @Override
    public String toString() {
        return "品牌:" + brand +'\t'+
                "价格:" + price +'\t'+
                "分辨率:" + resolution+')';
    }

    public void renderDraw(){
        System.out.println("渲染图形");
    }
}
class Geforce extends VideoCard{
    public Geforce() {
    }

    public Geforce(String brand, double price, String resolution) {
        super(brand, price, resolution);
    }

    @Override
    public String toString() {
        return super.toString();
    }
}
class Ati extends VideoCard{
    public Ati() {
    }

    public Ati(String brand, double price, String resolution) {
        super(brand, price, resolution);
    }

    @Override
    public String toString() {
        return super.toString();
    }

}
class Programmer{
    private String name;
    private Computer computer;

    public Programmer() {
    }

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

    public String getName() {
        return name;
    }

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

    public Computer getComputer() {
        return computer;
    }

    public void setComputer(Computer computer) {
        this.computer = computer;
    }

    @Override
    public String toString() {
        return "Programmer{" +
                "name='" + name + '\'' +
                ", computer=" + computer +
                '}';
    }

    public void buyComputer(Computer computer){
        this.computer=computer;
    }
    public void useComputer(){
        this.computer.runProgram();
        this.computer.runGame();
        this.computer.playVideo();
    }
    public void changeGeforce(VideoCard videoCard){
        System.out.println("换Geforce独立显卡");
        this.computer.setVideoCard(videoCard);
    }
    public void changeAti(VideoCard videoCard){
        System.out.println("换Ati独立显卡");
        this.computer.setVideoCard(videoCard);
    }
}
public class Homework5 {
    public static void main(String[] args) {
        Programmer programmer=new Programmer("张三");
        programmer.buyComputer(new Computer(new VideoCard("集成显卡",500,"800*600"),"华硕",5990));
        programmer.useComputer();
        System.out.println(programmer.getName()+"觉得显卡太菜了");
        programmer.changeGeforce(new Geforce("Geforce独立显卡",900,"1600*900"));
        programmer.useComputer();
        System.out.println(programmer.getName()+"觉得Ati的独立显卡好一点");
        programmer.changeAti(new Ati("Ati独立显卡",1500,"1920*1080"));
        programmer.useComputer();
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值