Java第九天面向对象练习

  • Post author:
  • Post category: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();
    }
}

转载于:https://my.oschina.net/u/4110331/blog/3046738