面向对象代码
package com.hanqi; public class Phone { private String pinPai;//品牌 public String getPinPai() { return pinPai; } public void setPinPai(String pinPai) { this.pinPai=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 com.hanqi; public class test { public static void main(String[] args) { //构建一个che的实例,对象 che xiali=new che(); //xiali.pinpai="夏利"; xiali.setPinPai(""); xiali.yanse="红色"; xiali.youxiangrongliang=40; //xiali.DangQianYouLiang=40; xiali.jiayou(40);//加油 xiali.fadong(); System.out.println("行驶100公里剩余油量="+xiali.getDangQianYouLiang(100)+"升"); Phone iphone6=new Phone(); iphone6.setColor("土豪金"); iphone6.call("10086"); Phone huawei=new Phone("华为","123"); System.out.println("品牌="+huawei.getPinPai()+ "号码="+huawei.getHaoma()); } }