|
什么是object指向?
. 1. 類與實(shí)例
1.1. 類
類 Car{
int number;//制造編號(hào) String name;//名稱 String maker;//廠家 String color;//顏色 }
類 Car{
int number;//制造編號(hào) String name;//名稱 String maker;//廠家 String color;//顏色
/** * 行走 **/ void run(){ //行走處理
}
/** * 轉(zhuǎn)換方向 **/ void turn(int angle){ //轉(zhuǎn)向處理
} }
1.2. 實(shí)例化
類名 實(shí)例名=new 類名;
Car car = new Car();
實(shí)例名.變量名 實(shí)例名.方法名
import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException;
/** * 車sample programme */ public 類 CarApp extends MIDlet {
/** * 開(kāi)始處理 */ protected void startApp() throws MIDletStateChangeException {
//實(shí)例化 Car car = new Car();
//設(shè)定實(shí)例變量 car.number = 6684; car.name = ""; car.maker = "NEC"; car.color = "blue";
//調(diào)用方法 car.run(); car.turn(); }
protected void pauseApp() {}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {} }
類 Car{
int number;//制造編號(hào) String name;//名稱 String maker;//廠家 String color;//顏色
/** * 行走 **/ void run(){ //行走處理 System.out.println(name + " is running!"); }
/** * 轉(zhuǎn)換方向 **/ void turn(){ //轉(zhuǎn)向處理 System.out.println(name + " is turning."); } }
2. 封裝2.1. public interface
2.2. 構(gòu)造器
/** * 構(gòu)造器 */ public 類名(){ //初始化處理
}
/** * 構(gòu)造器 */ public Car(int number,String name,String maker,String color){ this.number = number; this.name = name; this.maker = maker; this.color = color; }
2.3. accessory method
/******************************************* * accessory method *******************************************/
/** * 取得顏色 */ public String getColor() { return color; }
/** * 取得廠家 */ public String getMaker() { return maker; }
/** * 取得名稱 */ public String getName() { return name; }
/** * 取得制造編號(hào) */ public int getNumber() { return number; }
/** * 設(shè)定顏色 */ public void setColor(String string) { color = string; }
import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException;
/** * 車sample pragramme */ public 類 CarApp extends MIDlet {
/** * 開(kāi)始處理 */ protected void startApp() throws MIDletStateChangeException {
//實(shí)例化 Car car = new Car(6684,"java","NEC","blue");
//調(diào)用方法 car.run(); car.turn();
//涂顏色 System.out.println(car.getName() + "‘s color is " + car.getColor()); car.setColor("Red");//涂顏色 System.out.println(car.getName() + "‘s color is " + car.getColor()); }
protected void pauseApp() {}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {} }
類 Car{
private int number;//制造編號(hào) private String name;//名稱 private String maker;//廠家 private String color;//顏色
/** * 構(gòu)造器 */ public Car(int number,String name,String maker,String color){ this.number = number; this.name = name; this.maker = maker; this.color = color; }
/** * 行走 **/ public void run(){ //行走處理 System.out.println(name + " is running!"); }
/** * 轉(zhuǎn)換方向 **/ public void turn(){ //轉(zhuǎn)向處理 System.out.println(name + " is turning."); }
/******************************************* * accessory method *******************************************/
/** * 取得顏色 */ public String getColor() { return color; }
/** * 取得廠家 */ public String getMaker() { return maker; }
/** * 取得名稱 */ public String getName() { return name; }
/** * 取得制造編號(hào) */ public int getNumber() { return number; }
/** * 設(shè)定顏色 */ public void setColor(String string) { color = string; }
}
3. 繼承
類 Trailer{
private int number;//制造編號(hào) private String color;//顏色 private String maker;//廠家 private String name;//名稱 private String cargo;//負(fù)荷
/** * 構(gòu)造器 */ public Trailer(int number,String name,String maker,String color,String cargo){ this.number = number; this.name = name; this.maker = maker; this.color = color; this.cargo = cargo; }
/** * 行走 **/ public void run(){ //行走處理 System.out.println(name + " is running!"); }
/** * 轉(zhuǎn)換方向 **/ public void turn(){ //轉(zhuǎn)向處理 System.out.println(name + " is turning."); }
/******************************************* * accessory method *******************************************/
/** * 取得顏色 */ public String getColor() { return color; }
/** * 取得廠家 */ public String getMaker() { return maker; }
/** * 取得名稱 */ public String getName() { return name; }
/** * 取得制造編號(hào) */ public int getNumber() { return number; }
/** * 設(shè)定顏色 */ public void setColor(String string) { color = string; }
/** * 取得負(fù)荷 */ public String getCargo(){ return cargo; }
/** * 設(shè)定負(fù)荷 */ public void setCargo(String cargo){ this.cargo = cargo; } }
類 Trailer extends Car{
private String cargo;//負(fù)荷
/** * 構(gòu)造器 */ public Trailer(int number,String name,String maker,String color,String cargo){ super(number,name,maker,color);//調(diào)用繼承之類的構(gòu)造器. this.cargo = cargo; }
/******************************************* * accessory method *******************************************/
/** * 取得負(fù)荷 */ public String getCargo(){ return cargo; }
/** * 積荷を設(shè)定する設(shè)定負(fù)荷 */ public void setCargo(String cargo){ this.cargo = cargo; } }
import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException;
/** * 車sample programme */ public 類 CarApp extends MIDlet {
/** * 開(kāi)始處理 */ protected void startApp() throws MIDletStateChangeException {
//實(shí)例化 Trailer trailer = new Trailer(6684,"java-Trailer","NEC","blue","N820");
//調(diào)用方法 trailer.run(); trailer.turn();
//改變負(fù)荷 trailer.setCargo("N800"); System.out.println(trailer.getName() + "‘s cargo is " + trailer.getCargo()); }
protected void pauseApp() {}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {} }
類 Trailer extends Car{
private String cargo;//負(fù)荷
/** * 構(gòu)造器 */ public Trailer(int number,String name,String maker,String color,String cargo){ super(number,name,maker,color); this.cargo = cargo; }
/******************************************* * accessory method *******************************************/
/** * 取得負(fù)荷 */ public String getCargo(){ return cargo; }
/** * 設(shè)定負(fù)荷 */ public void setCargo(String cargo){ this.cargo = cargo; } }
類 Car{
private int number;//制造編號(hào) private String name;//名稱 private String maker;//廠家 private String color;//顏色
/** * 構(gòu)造者 */ public Car(int number,String name,String maker,String color){ this.number = number; this.name = name; this.maker = maker; this.color = color; }
/** * 行走 **/ public void run(){ //行走處理 System.out.println(name + " is running!"); }
/** * 轉(zhuǎn)換方向 **/ public void turn(){ //轉(zhuǎn)向處理 System.out.println(name + " is turning."); }
/******************************************* * accessory method *******************************************/
/** * 取得顏色 */ public String getColor() { return color; }
/** * 取得廠家 */ public String getMaker() { return maker; }
/** * 取得名稱 */ public String getName() { return name; }
/** * 取得制造編號(hào) */ public int getNumber() { return number; }
/** * 設(shè)定顏色 */ public void setColor(String string) { color = string; }
}
4. 下一講
|
|
|