PackageCom.hanqi;//Package Name Public classCar {//public (publicly accessible) class (Class) Car (class name 1th letter uppercase)//Property member Variables//the color of the car PrivateString Yanse; PublicString Getyanse () {//There are return values, no parameters returnYanse; } Public voidSetyanse (String yanse) {//no return value, with parametersYanse =Yanse; } //the brand of the car PrivateString Pinpai;//becomes private//Read Properties PublicString Getpinpai ()//method getter method with return value, without parameters (value used) to read outside { returnPinpai;//reading above is equivalent to encapsulation } //Write Property No return value there is a parameter setter method (assigned value) Public voidSetpinpai (String pinpai) {//Validation if(Pinpai.equals (""))//determine if strings are equal{SYSTEM.OUT.PRINTLN ("Brand cannot be empty");} Else{Pinpai=pinpai;}//Assignment} } //capacity of the tank DoubleYouxiangrongliang; //Current Oil content DoubleDangqianyouliang; //No return value no parameter Public voidFadong () {System.out.println ("The car was launched."); //change the state of a car } //There are parameters with return values Public DoubleGetdangqianyouliang (DoubleXingshi) { returndangqianyouliang-(xingshi*10/100); } //method member function member method//1. No return value 2. No Parameters//void no return value//(parameter) Public voidJiayou (DoubleYouliang)//no return value, with parameters { //come ondangqianyouliang+=Youliang; System.out.println ("This time to" +yanse+pinpai+ "plus" +youliang+ "lift Oil");} }
PackageCom.hanqi;ImportCom.hanqi.Car; Public classTest1 {/** * @paramargs*/ Public Static voidMain (string[] args) {//TODO Auto-generated method stubs//build an instance of car, objectCar Xiali =NewCar (); Xiali.setpinpai (Xiali); Xiali.getpinpai (); System.out.println ("The brand of the car =" +Xiali.getpinpai ()); Xiali.setyanse (Red); Xiali.getyanse (); //above the Pinpai and Yans//xiali.jiayou (+);//come onXiali. Youxiangrongliang=40; //Xiali. dangqianyouliang=30;Xiali.jiayou (40);//Xiali.fadong (); System.out.println ("Travel 100 km remaining oil =" +xiali.getdangqianyouliang (100)); }}
The method type of the class--whether there are arguments, whether there are return values