public class Phone { // 属性 private String brand; private double price; private String color; // 构造方法 public Phone() { } public Phone(String brand, double price, String color) { this.brand = brand; this.price = price; this.color = color; } // set/get方法 public void setBrand(String brand) { this.brand = brand; } public String getBrand() { return brand; } public void setPrice(double price) { this.price = price; } public double getPrice() { return price; } public void setColor(String color) { this.color = color; } public String getColor() { return color; } // 功能方法 public void call() { System.out.println("正在使用价格为" + price + "元" + color + "的" + brand + "手机打电话...."); } public void sendMessage() { System.out.println("正在使用价格为" + price + "元" + color + "的" + brand + "手机发短信...."); } } 测试类代码:
时间: 2023-05-28 15:04:40 浏览: 145
Phone.class
public class PhoneTest {
public static void main(String[] args) {
Phone iphone = new Phone("iPhone", 6999.0, "白色");
iphone.call();
iphone.sendMessage();
Phone huawei = new Phone();
huawei.setBrand("华为");
huawei.setPrice(3999.0);
huawei.setColor("黑色");
huawei.call();
huawei.sendMessage();
}
}
阅读全文