Java第九天面向對象練習

/**11.構造方法的重載,定義一個名爲Vehicle交通工具的基類,該類中應包含String類型的成員屬性brand商標和color顏色還應包含成員方法run行駛在控制檯顯示「我已經開動了」和showlnfo顯示信息:在控制檯顯示商標和顏色,並編寫構造方法初始化其成員屬性。編寫Car小汽車類繼承於Vehicle類增長int型成員屬性seats座位,還應增長成員方法showCar在控制檯顯示小汽車的信息並編寫構造方法。編寫Truck卡車類繼承於Vehicle類增長float型成員load載重,還應增長成員方法showTruck在控制檯顯示卡車的信息並編寫構造方法。在main方法中測試以上各種。
*/
//Vehicle交通工具的基類
class Vehicle{
    String brand;
    String color;
    public void run(){
        System.out.println("我已經開動了");
    }
    public void showlnfo(){
        System.out.println("商標:"+brand+"顏色:"+color);
    }
    public Vehicle(){
        
    }
    public Vehicle(String brand,String color){
        this.brand=brand;
        this.color=color;
    }
}
//Car小汽車類繼承於Vehicle類
class Car extends Vehicle{
    int seats;
    public Car(){
        
    }
    public Car(String brand,String color,int seats){
        super(brand,color);
        this.seats=seats;
    }
    public void showCar(){
        showlnfo();
        System.out.println("座位:"+seats);
    }
}
//Truck卡車類繼承於Vehicle類
class Truck extends Vehicle{
    float load;
    public Truck(){
        
    }
    public Truck(String brand,String color,float load){
        super(brand,color);
        this.load=load;
    }
    public void showTruck(){
        showlnfo();
        System.out.println("載重:"+load);
    }
}
public class Test11{
    public static void main(String[] args){
        Vehicle v=new Vehicle("飛鴿","白色");
        v.run();
        v.showlnfo();
        Car c=new Car("寶馬","紅色",6);
        c.run();
        c.showCar();
        Truck t=new Truck("東方紅","藍色",7);
        t.run();
        t.showTruck();
    }
}網絡

/**12.構造方法與重載,定義一個網絡用戶類要處理的信息有用戶ID、用戶密碼、email地址。在創建類的實例時,把以上三個信息都做爲構造函數的參數輸入,其中用戶ID和用戶密碼是必須的,默認的email地址是用戶ID加上字符串「@gameschool.com」
*/
class WebUser{
    int id;
    String password;
    String email;
    public WebUser(int id,String password){
        this.id=id;
        this.password=password;
        //email默認值
        email=id+"@gameschool.com";
    }
    public WebUser(int id,String password,String email){
        this.id=id;
        this.password=password;
        this.email=email;
    }
    public void info(){
        System.out.println("id:"+id+"    密碼"+password+"     email:"+email);
    }
}
public class Test12{
    public static void main(String[] args){
        WebUser w=new WebUser(123,"monica888888");
        w.info();
        WebUser u=new WebUser(133,"monica888888","2833@qq.com");
        u.info();
    }
}函數

相關文章
相關標籤/搜索