Java分析以下需求并实现:手机类Phone 属性:(品牌brand, 价格price)、行为:(打电话call()、发短信sendMessage()、 玩游戏playGame())

//手机类Phone:
public class Phone {
    private String brand;
    private int price;

    //生成空参
    public Phone() {
    }

    //生成有参构造
    public Phone(String brand, int price) {
        this.brand = brand;
        this.price = price;
    }

    //打电话
    public void call() {
        System.out.println("正在使用价格为" + price + "元的" + brand + "品牌的手机打电话....");
    }

    //发送信息
    public void sendMessage() {

        System.out.println("正在使用价格为" + price + "元的" + brand + "品牌的手机发短信....");
    }

    //玩游戏
    public void playGame() {
        System.out.println("正在使用价格为" + price + "元的" + brand + "品牌的手机玩游戏....");
    }

    //setter和getter方法
    public String getBrand() {
        return brand;
    }

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

    public int getPrice() {
        return price;
    }

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


}
//测试类:
public class PhoneTest {
    public static void main(String[] args) {
        //setter方法赋值
        Phone phone = new Phone();
        phone.setBrand("小米");
        phone.setPrice(998);
        //构造方法赋值
        Phone phone1 = new Phone("小米", 998);
        //setter方法调用
        phone.call();
        phone.sendMessage();
        phone.playGame();
        System.out.println();
        //构造方法调用
        phone1.call();
        phone1.sendMessage();
        phone1.playGame();

    }
}
 

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值