作业-继承7

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了作业-继承7相关的知识,希望对你有一定的参考价值。

编写一个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的主要内容,如果未能解决你的问题,请参考以下文章

动态SQL基础概念复习(Javaweb作业5)

VSCode自定义代码片段7——CSS动画

VSCode自定义代码片段7——CSS动画

HTML5期末大作业:餐饮美食网站设计——咖啡(10页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 咖啡网页设计 美食餐饮网页设计...(代码片段

第十次作业

作业10-异常