一、题目描述
编写一个制造各种车辆的程序。包含三个类,具体要求如下:
(1)基类Vehicle,包含轮子数和汽车自身重量两个属性,一个两参数的构造方法,一个显示汽车信息的方法;
(2)小轿车类Car,增加载客数属性,重写构造方法和显示车辆信息的成员方法;
(3)卡车类Truck,增加载客数和载货量属性,重写构造方法和显示车辆信息的成员方法;
(4)主程序类,要求输入各种车辆的信息,并在控制台输出各种车辆信息。
二、代码实现
import java.util.Scanner;
class Vehicle{
int num;
int weight;
void set(int num,int weight) {
this.num=num;
this.weight=weight;
}
void show() {
System.out.println(num+" "+weight);
}
}
class Car extends Vehicle{
int person;
void set(int num,int weight,int person) {
this.num=num;
this.weight=weight;
this.person=person;
}
void show(){
System.out.println("轮子数:"+num+"重量:"+weight+"乘客数:"+person);
}
}
class Truck extends Vehicle{
int znum;
int person;
void set(int num,int weight,int person,int znum) {
this.num=num;
this.weight=weight;
this.person=person;
this.znum=znum;
}
void show() {
System.out.println("轮子数:"+num+"重量:"+weight+"乘客数:"+person+"载货重量:"+znum);
}
}
public class Test {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Car car=new Car();
Truck truck=new Truck();
car.num=in.nextInt();
car.weight=in.nextInt();
car.person=in.nextInt();
truck.num=in.nextInt();
truck.weight=in.nextInt();
truck.person=in.nextInt();
truck.znum=in.nextInt();
car.show();
truck.show();
in.close();
}
三、结果展示
四、推荐
给大家推荐三款蓝牙耳机,下面的链接可以直接购买:
1、https://item.taobao.com/item.htm?ft=t&id=643733003968
2、https://item.taobao.com/item.htm?ft=t&id=642849670304
3、https://item.taobao.com/item.htm?ft=t&id=639886208479
由于是新开的淘宝店铺,购买时联系客服,下单即可享受红包,红包 不大也是一片心意哟。虽然是新店,但是质量是有保证的哦!