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);
    }
}
相关推荐
空の鱼4 小时前
java开发,IDEA转战VSCODE配置(mac)
java·vscode
P7进阶路4 小时前
Tomcat异常日志中文乱码怎么解决
java·tomcat·firefox
小丁爱养花5 小时前
Spring MVC:HTTP 请求的参数传递2.0
java·后端·spring
CodeClimb5 小时前
【华为OD-E卷 - 第k个排列 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
等一场春雨5 小时前
Java设计模式 九 桥接模式 (Bridge Pattern)
java·设计模式·桥接模式
带刺的坐椅5 小时前
[Java] Solon 框架的三大核心组件之一插件扩展体系
java·ioc·solon·plugin·aop·handler
不惑_6 小时前
深度学习 · 手撕 DeepLearning4J ,用Java实现手写数字识别 (附UI效果展示)
java·深度学习·ui
费曼乐园6 小时前
Kafka中bin目录下面kafka-run-class.sh脚本中的JAVA_HOME
java·kafka
feilieren7 小时前
SpringBoot 搭建 SSE
java·spring boot·spring
阿岳3167 小时前
Java导出通过Word模板导出docx文件并通过QQ邮箱发送
java·开发语言