黑马程序员技术交流社区
标题:
每日一个逗比代码,加深知识点记忆~~
[打印本页]
作者:
刘登飞
时间:
2015-9-14 10:19
标题:
每日一个逗比代码,加深知识点记忆~~
class Demo5_Phone {
public static void main(String[] args) {
Phone p = new Phone("三星",2800); //创建对象
p.show(); //调用成员方法
Phone p1 = new Phone();
p1.setBrand("苹果");
p1.setPrice(6800);
System.out.println(p1.getBrand() +"手机只要"+ p1.getPrice()+"元"+",千万别卖肾!~");
}
}
class Phone
{
private String brand; //私有品牌
private int price; //私有价格
public Phone() {} //无参构造方法
public Phone(String brand,int price ) //有参构造方法
{
this.brand = brand;
this.price = price;
}
public void setBrand(String brand) //设置品牌
{
this.brand = brand;
}
public String getBrand() //获取品牌
{
return brand;
}
public void setPrice(int price) //设置价格
{
this.price = price;
}
public int getPrice() //获取价格
{
return price;
}
public void show() //定义show方法
{
System.out.println(brand +"手机只要"+ price+"元");
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2