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);
    }
}
相关推荐
钱多多_qdd5 分钟前
spring cache源码解析(四)——从@EnableCaching开始来阅读源码
java·spring boot·spring
waicsdn_haha7 分钟前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
Q_192849990617 分钟前
基于Spring Boot的摄影器材租赁回收系统
java·spring boot·后端
Code_流苏20 分钟前
VSCode搭建Java开发环境 2024保姆级安装教程(Java环境搭建+VSCode安装+运行测试+背景图设置)
java·ide·vscode·搭建·java开发环境
禁默1 小时前
深入浅出:AWT的基本组件及其应用
java·开发语言·界面编程
Cachel wood1 小时前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架
Code哈哈笑1 小时前
【Java 学习】深度剖析Java多态:从向上转型到向下转型,解锁动态绑定的奥秘,让代码更优雅灵活
java·开发语言·学习
gb42152871 小时前
springboot中Jackson库和jsonpath库的区别和联系。
java·spring boot·后端
程序猿进阶1 小时前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot