//手机类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();
}
}