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

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

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

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

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

    能。

    package hanqi;
    
    public class Vehicle {
        
        private int wheels;
        private int weight;
        public int getWheels() {
            return wheels;
        }
        public void setWheels(int wheels) {
            this.wheels = wheels;
        }
        public int getWeight() {
            return weight;
        }
        public void setWeight(int weight) {
            this.weight = weight;
        }
        
        //构造
        public Vehicle(int wheels,int weight)
        {
            this.weight=weight;
            this.wheels=wheels;
        }
        
    }
    package hanqi;
    
    public class Car extends Vehicle{
        
        private int loader;
    
        public int getLoader() {
            return loader;
        }
    
        public void setLoader(int loader) {
            this.loader = loader;
        }
        
        //构造
        public Car(int wheels, int weight,int loader)
        {
            super(wheels,weight);
            this.loader=loader;
        }
    
    }
    package hanqi;
    
    public class Truck extends Car{
        private int payload;
    
        public int getPayload() {
            return payload;
        }
    
        public void setPayload(int payload) {
            this.payload = payload;
        }
        
        //构造
        public Truck(int wheels, int weight, int loader, int payload)
        {
            super(wheels,weight,loader);
            this.payload=payload;
        }
    
    }
    package hanqi;
    
    public class TestVehicle {
    
        public static void main(String[] args) {
            
            Vehicle a = new Vehicle(4,3);
            System.out.println("a有:"+a.getWheels()+"个轮子	:"+a.getWeight()+"吨重");
        
            
            Car  b= new Car(4,3,4);
            System.out.println("b有:"+b.getWheels()+"个轮子	:"+b.getWeight()+"吨重	可以坐"+b.getLoader()+"个人");
        
            Truck  c= new Truck(6,10,5,10);
            System.out.println("c有:"+c.getWheels()+"个轮子	:"+c.getWeight()+"吨重	可以坐"+c.getLoader()+"个人	载重"+c.getPayload()+"吨");
            
    }
    }

  • 相关阅读:
    Netty学习路线总结
    Intellij IDEA 快捷键整理-鬼畜版(全键盘开发指南)
    httpClient
    [翻译]Javaslang 介绍
    Docker学习<一>--初体验Windows环境下安装
    Spring @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
    IntelliJ IDEA 2016.2激活方法
    解决mac升级后,出现的 xcrun: error: invalid active developer path, missing xcrun 错误
    Logstash 安装与配置
    【JVM学习笔记】双亲委托机制存在的意义
  • 原文地址:https://www.cnblogs.com/zs6666/p/5906558.html
Copyright © 2011-2022 走看看