代码如下:
public class MobilPhone { public String brand; public MobilPhone(){ this.brand="诺基亚"; } public MobilPhone(String bra){ this.brand=bra; } public String buy(){ return "没发工资, 买一个"+brand+"牌子的手机吧"; } public String Buy(String reason){ return reason + ",快买一个" + brand + "牌子的手机吧!"; } }
public class MobilPhoneTest { public static void main(String[] args){ MobilPhone mp = new MobilPhone(); mp.brand="苹果"; //发工资了,修改品牌属性 String detail=mp.Buy("发工资啦"); //发工资了,调用带参构造方法 System.out.println(detail); } }