面向对象----构造方法(以手机为案例)
package phone; public class phone { private String pinPai;//品牌 public String getPinPai() { return pinPai; } private String haoma;//号码 public String getHaoma() { return haoma; } private String color;//颜色 public String getColor() { return color; } public void setColor(String color) { this.color = color; } //构造方法:构造对象的时候调用的方法 public phone() { System.out.println("调用默认的构造方法"); } //新的构造方法 public phone(String pinPai,String haoMa){ this.pinPai=pinPai; this.haoma=haoma; } public int call(String haom){ System.out.println("拨打的电话号码为"+haom); int thsj =3; return thsj; } }
package phone; public class Test1 { public class phone { phone iphone6 = new phone(); iphone6.setColor=("土豪金"); iphone6.call("10086"); // 新的构造方法 phone huawei =new phone("华为""1234"); System.out.println("品牌 ="+ huawei.getPinPai()+"号码="+hauwei.getHaoma()); } }