Java面向对象练习(3) (2024.7.11)

手机类

java 复制代码
package Phone20240711;

public abstract class Phone {
    private String brand;
    private String price;

    public Phone(){}

    public Phone(String brand, String price) {
        this.brand = brand;
        this.price = price;
    }

    public String getBrand() {
        return brand;
    }

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

    public String getPrice() {
        return price;
    }

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

    public abstract void call();
    public abstract void sendMessages();
}

旧手机类

java 复制代码
package Phone20240711;

public class OldPhone extends Phone{
    public OldPhone() {}

    public OldPhone(String brand, String price) {
        super(brand, price);
    }

    @Override
    public void call() {
        System.out.println("旧手机" + this.getBrand() + "在打电话");
    }

    @Override
    public void sendMessages() {
        System.out.println("新手机" + this.getBrand() + "在打电话");
    }
}

新手机类

java 复制代码
package Phone20240711;

public class NewPhone extends Phone implements PlayGame{
    public NewPhone() {}

    public NewPhone(String brand, String price) {
        super(brand, price);
    }

    @Override
    public void call() {
        System.out.println("新手机" + this.getBrand() + "在打电话");
    }

    @Override
    public void sendMessages() {
        System.out.println("新手机" + this.getBrand() + "在发短信");
    }

    @Override
    public void playGame() {
        System.out.println("新手机" + this.getBrand() + "在玩游戏");
    }
}

游戏接口

java 复制代码
package Phone20240711;

public interface PlayGame {
    public abstract void playGame();
}

测试

java 复制代码
package Phone20240711;
import java.util.Scanner;
public class PhoneTest {
    public static void main(String[] args) {
        NewPhone np = creatNewPhone();
        OldPhone op = creatOldPhone();
        np.call();
        np.sendMessages();
        np.playGame();
        op.call();
        op.sendMessages();
    }

    public static NewPhone creatNewPhone() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入新手机的品牌");
        String brand = sc.next();
        System.out.println("请输入新手机的价格");
        String price = sc.next();
        return new NewPhone(brand, price);
    }
    public static OldPhone creatOldPhone() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入旧手机的品牌");
        String brand = sc.next();
        System.out.println("请输入旧手机的价格");
        String price = sc.next();
        return new OldPhone(brand, price);
    }
}
相关推荐
草履虫建模1 小时前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
qq_297574674 小时前
【实战教程】SpringBoot 实现多文件批量下载并打包为 ZIP 压缩包
java·spring boot·后端
老毛肚4 小时前
MyBatis插件原理及Spring集成
java·spring·mybatis
学嵌入式的小杨同学4 小时前
【Linux 封神之路】信号编程全解析:从信号基础到 MP3 播放器实战(含核心 API 与避坑指南)
java·linux·c语言·开发语言·vscode·vim·ux
lang201509284 小时前
JSR-340 :高性能Web开发新标准
java·前端·servlet
Re.不晚4 小时前
Java入门17——异常
java·开发语言
缘空如是4 小时前
基础工具包之JSON 工厂类
java·json·json切换
追逐梦想的张小年5 小时前
JUC编程04
java·idea
好家伙VCC5 小时前
### WebRTC技术:实时通信的革新与实现####webRTC(Web Real-TimeComm
java·前端·python·webrtc
南极星10055 小时前
蓝桥杯JAVA--启蒙之路(十)class版本 模块
java·开发语言