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);
    }
}
相关推荐
何似在人间5755 分钟前
多级缓存模型设计
java·jvm·redis·缓存
多云的夏天7 分钟前
ubuntu24.04-MyEclipse的项目导入到 IDEA中
java·intellij-idea·myeclipse
Fanxt_Ja19 分钟前
【数据结构】红黑树超详解 ---一篇通关红黑树原理(含源码解析+动态构建红黑树)
java·数据结构·算法·红黑树
Aphelios38024 分钟前
TaskFlow开发日记 #1 - 原生JS实现智能Todo组件
java·开发语言·前端·javascript·ecmascript·todo
weixin_448771721 小时前
使用xml模板导出excel
xml·java·excel
烁3471 小时前
每日一题(小白)模拟娱乐篇27
java·数据结构·算法·娱乐
魔道不误砍柴功2 小时前
2025年Java无服务器架构实战:AWS Lambda与Spring Cloud Function深度整合
java·架构·serverless
smileNicky2 小时前
SpringBoot系列之集成Redisson实现布隆过滤器
java·spring boot·redis·布隆过滤器
隔壁小查2 小时前
【后端开发】初识Spring IoC与SpringDI、图书管理系统
java·spring·okhttp
程序员沉梦听雨3 小时前
外观模式详解
java·设计模式·外观模式