zoukankan      html  css  js  c++  java
  • 29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。

    //Vehicle类
    package d922A;

    public class Vehicle {
    private int wheels;
    private double weight;
    Vehicle()
    {
    weight=1;wheels=4;
    }
    Vehicle(int lun, double zizhong)
    {
    wheels=lun;
    weight=zizhong;
    }
    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;
    }
    public void showData(){
    System.out.println("轮子"+wheels+"个,"+"自重"+weight+"t");
    }

    }
    // Car类
    package d922A;

    public class Car extends Vehicle {
    private int loader;
    Car()
    {
    loader=4;
    }
    Car(int zaikeshu)
    {
    loader=zaikeshu;
    }
    Car(int lun, double zizhong,int zaikeshu)
    {
    super(lun,zizhong);
    loader=zaikeshu;
    }
    public void showData(){
    System.out.println("轮子"+getWheels()+"个"+"自重"+getWeight()+"t");
    System.out.println("载客人数:"+loader+"人");
    }
    public int getLoader() {
    return loader;
    }
    public void setLoader(int loader) {
    this.loader = loader;
    }

    }
    // Truck类
    package d922A;

    public class Truck extends Car {
    private double payload;
    Truck()
    {
    payload=10;
    }
    Truck(double zaizhong)
    {
    payload=zaizhong;
    }
    Truck(int zaikeshu,double zaizhong)
    {
    super(zaikeshu);
    payload=zaizhong;
    }
    Truck(int lun, double zizhong,int zaikeshu,double zaizhong)
    {
    super(lun,zizhong,zaikeshu);
    payload=zaizhong;
    }
    public void showData(){
    System.out.println("轮子"+getWheels()+"个"+"自重"+getWeight()+"t");
    System.out.println("载客人数:"+getLoader()+"人");
    System.out.println("载重:"+payload+"t");
    }

    }
    //主类
    package d922A;

    public class Testvec {

    public static void main(String[] args) {
    	Truck a=new Truck();
    	a.showData();
    	a=new Truck(50);
    	a.showData();
    	a=new Truck(2,50);
    	a.showData();
    	a=new Truck(8,3,4,100);
    	a.showData();
    	
    
    }
    

    }
    ///运行结果
    轮子4个自重1.0t
    载客人数:4人
    载重:10.0t
    轮子4个自重1.0t
    载客人数:4人
    载重:50.0t
    轮子4个自重1.0t
    载客人数:2人
    载重:50.0t
    轮子8个自重3.0t
    载客人数:4人
    载重:100.0t

  • 相关阅读:
    .net 存储过程中的 output参数取值问题
    【从零开始学Servlet笔记】Servelet入门
    【从零开始学Servlet笔记】Web资源
    【从零开始学Servlet笔记】Http协议
    【从零开始学Mybatis笔记】SqlMapConfig.xml配置文件
    【从零开始学Mybatis笔记】Dao开发方法
    【从零开始学Mybatis笔记】Mybatis入门
    【从零开始学SpringMVC笔记】SpringMVC进阶
    【从零开始学SpringMVC笔记】SpringMVC与struts2不同
    【从零开始学SpringMVC笔记】参数绑定
  • 原文地址:https://www.cnblogs.com/nicebaby/p/5897284.html
Copyright © 2011-2022 走看看