码迷,mamicode.com
首页 > 其他好文 > 详细

作业-继承7

时间:2016-09-23 21:22:18      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数

wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数

loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个

类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功

能。

package T092206;

public class Vehicle {
    private int wheels;
    private double weight;
    public int getWheels() {
        return wheels;
    }
    public void setWheels(int wheels) {
        this.wheels = wheels;
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    Vehicle(){
        
    }
    Vehicle(int wheels,double weight){
        this.wheels=wheels;
        this.weight=weight;
    }
    public void getInfo(){
        System.out.println("车轮的个数是:"+wheels+" 车重:"+weight);
    }
}



package T092206;

public class Car extends Vehicle {
    private int loader;
    public int getLoader() {
        return loader;
    }
    public void setLoader(int loader) {
        this.loader = loader;
    }
    Car(){
        
    }
    Car(int wheels,double weight,int loader){
        super(wheels,weight);
        this.loader=loader;
    }
    public void loader(){
        if(loader<=6){
            System.out.println("这辆车能载"+loader+"人");
        }
        else{
            System.out.println("超员了");
        }
    }
}




package T092206;

public class Truck extends Car {
    Truck(){
        super();
    }
    private double payload;
    public double getPayload() {
        return payload;
    }
    public void setPayload(double payload) {
        this.payload = payload;
    }
    public void payload(){
        if(payload<=1000){
            System.out.println("这辆车的载重是"+payload);
        }
        else{
            System.out.println("超重了");
        }
    }
}



package T092206;

public class Test {
    public static void main(String[] args){
        Car c=new Car(4,1000,4);
        c.getInfo();
        c.loader();
        Truck t =new Truck();
        t.setLoader(3);
        t.setPayload(2000);
        t.setWeight(3000);
        t.setWheels(6);
        t.getInfo();
        t.loader();
        t.payload();    
    }
}

技术分享

作业-继承7

标签:

原文地址:http://www.cnblogs.com/wangguoning/p/5901639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!