package lino2;
//测试类
public class PhoneTest {
public static void main(String[] args) {
//创建手机类对象
//方式一:无参构造+setXXX(形参) 创建对象+赋值
phone p1=new phone();
p1.setBrand("小米");
p1.setPrice(777);
p1.setColor("黑");
System.out.println(p1.getBrand()+"---"+p1.getColor()+"---"+p1.getPrice());
p1.playGame();
p1.callPhone("张三");
p1.sendMsg();
System.out.println("--------");
//方式2:有参构造赋值
phone p2=new phone("红米",999,"蓝");
System.out.println(p2.getBrand()+"---"+p2.getColor()+"---"+p2.getPrice());
p2.playGame();
p2.callPhone("李四");
p2.sendMsg();
}
}
package lino2;
public class phone {
//成员变量私有化
private String brand;//品牌
private int price;//价格
private String color;//颜色
//无参构造: alt+shift+s--->c
public phone() {
}
//有参构造: alt+shift+s-->o
public phone(String brand, int price, String color) {
this.brand = brand;
this.price = price;
this.color = color;
}
//这些 setXXX()/getXXX() alt+shift+s->r
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 String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void callPhone(String name) {
System.out.println("给"+name+"打电话");
}
public void sendMsg(){
System.out.println("手机可以发短信");
}
public void playGame(){
System.out.println("手机可以玩游戏");
}
}