首页 > 代码库 > 运用参数与返回类型

运用参数与返回类型

/*运用参数与返回类型
 * getter ,setter
 * 1.getter返回实例变量的值
 * 2.setter通过参数设置实例变量的值*/

 1 public class Yunyongcanshu { 2     String brand;//品牌 3     int numPickups;//拾音器 4     boolean rockUseIt;//摇滚 5      6     String getBrand(){ 7         return brand; 8     } 9     void  setBrand(String aBrand){10         brand = aBrand;11     }12     int getNumPickups(){13         return numPickups;14     }15     void setNumPickups(int num){16         numPickups = num;17     }18     boolean getRockUseIt(){19         return rockUseIt;20     }21     void setRockUseIt(boolean yesOrno){22         rockUseIt = yesOrno;23     }24     25     public static void main(String[] args){26         Yunyongcanshu guitar = new Yunyongcanshu();27         guitar.setBrand("纵向科技");28         guitar.setNumPickups(3);29         //guitar.setNumPickups(6);30         guitar.setRockUseIt(true);31         32         System.out.println("吉他牌子是:"+guitar.getBrand());33         System.out.println("吉他的pickup数目是:"+guitar.getNumPickups());34         System.out.println("是否属于摇滚明星的吉他:"+guitar.getRockUseIt());35     }36  37 }

技术分享

运用参数与返回类型