Java,编写一个类Test04_2.java,实例化2个Phone类,分别自定义它们的品牌和价格,然后进行打电话操作,输出品牌和价格
时间: 2024-05-22 21:16:10 浏览: 62
public class Test04_2 {
public static void main(String[] args) {
Phone phone1 = new Phone("Apple", 6999.0);
Phone phone2 = new Phone("Samsung", 4999.0);
phone1.call();
System.out.println("品牌:" + phone1.getBrand());
System.out.println("价格:" + phone1.getPrice());
phone2.call();
System.out.println("品牌:" + phone2.getBrand());
System.out.println("价格:" + phone2.getPrice());
}
}
class Phone {
private String brand;
private double price;
public Phone(String brand, double price) {
this.brand = brand;
this.price = price;
}
public void call() {
System.out.println("打电话中...");
}
public String getBrand() {
return brand;
}
public double getPrice() {
return price;
}
}
阅读全文