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);
    }
}
相关推荐
Daniel 大东1 小时前
BugJson因为json格式问题OOM怎么办
java·安全
Theodore_10225 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
冰帝海岸6 小时前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象6 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了7 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
小二·7 小时前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic7 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
懒洋洋大魔王7 小时前
RocketMQ的使⽤
java·rocketmq·java-rocketmq
武子康7 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神8 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式