1. 编写一个Java应用程序,设计一个汽车类Vehicle,包含的成员属性有:车轮个数wheels和车重weight。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1. 编写一个Java应用程序,设计一个汽车类Vehicle,包含的成员属性有:车轮个数wheels和车重weight。相关的知识,希望对你有一定的参考价值。
1. 编写一个Java应用程序,设计一个汽车类Vehicle,包含的成员属性有:车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含属性载人数passenger_load。卡车Truck是Vehicle的子类,其中包含载人数passenger_load和载重量payload。要求每个类都有相关数据的输出方法。
2. 运行程序,理解成员变量的继承与隐藏。
classVehicleprivateintwheels;privatefloatweight;protectedVehicle(intwheels,floatweight)this。wheels=wheels;this。weight=weight。是普通的除号,即10/2=5。
编写java程序的注意事项:
大小写敏感:Java是大小写敏感的,这就意味着标识符Hello与hello是不同的。
类名:对于所有的类来说,类名的首字母应该大写。如果类名由若干单词组成,那么每个单词的首字母应该大写,例如 MyFirstJavaClass。
方法名:所有的方法名都应该以小写字母开头。如果方法名含有若干单词,则后面的每个单词首字母大写,例如myFirstJavaClass。
参考技术A /**编译运行均已通过
*直接复制黏贴即可
*文件名为:Test.java
*/
class Vehicle
private int wheels;
private float weight;
protected Vehicle(int wheels, float weight)
this.wheels = wheels;
this.weight = weight;
public int getWheels()
return wheels;
public float getWeight()
return weight;
public void print()
System.out.println("汽车:");
System.out.println("共有"+this.getWheels()+"个轮子");
System.out.println("重量为"+this.getWeight()+"吨");
class Car extends Vehicle
private int passenger_load;
public Car(int wheels, float weight, int passenger_load)
super(wheels, weight);
this.passenger_load = passenger_load;
public int getPassenger_load()
return passenger_load;
public void print()
System.out.println("小车:");
System.out.println("共有"+this.getWheels()+"个轮子");
System.out.println("重量为"+this.getWeight()+"吨");
System.out.println("载人数为"+this.getPassenger_load()+"人");
class Truck extends Vehicle
private int passenger_load;
private float payload;
public Truck(int wheels, float weight, int passenger_load, float payload)
super(wheels, weight);
this.passenger_load = passenger_load;
this.payload = payload;
public int getPassenger_load()
return passenger_load;
public float getPayload()
return payload;
public void print()
System.out.println("卡车:");
System.out.println("共有"+this.getWheels()+"个轮子");
System.out.println("重量为"+this.getWeight()+"吨");
System.out.println("载人数为"+this.getPassenger_load()+"人");
System.out.println("载重量为"+this.getPayload()+"吨");
public class Test
public static void main(String args[])
Vehicle car = new Car(4, 3, 4);
Vehicle truck = new Truck(6, 6, 2, 10);
System.out.println("*****************************");
car.print();
System.out.println("*****************************");
truck.print();
/*
*output:
*****************************
小车:
共有4个轮子
重量为3.0吨
载人数为4人
*****************************
卡车:
共有6个轮子
重量为6.0吨
载人数为2人
载重量为10.0吨
*/本回答被提问者采纳
Java基础-继承-编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loade
#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()+"个轮子\t:"+a.getWeight()+"吨重"); Car b= new Car(4,3,4); System.out.println("b有:"+b.getWheels()+"个轮子\t:"+b.getWeight()+"吨重\t可以坐"+b.getLoader()+"个人"); Truck c= new Truck(6,10,5,10); System.out.println("c有:"+c.getWheels()+"个轮子\t:"+c.getWeight()+"吨重\t可以坐"+c.getLoader()+"个人\t载重"+c.getPayload()+"吨");
}
}
以上是关于1. 编写一个Java应用程序,设计一个汽车类Vehicle,包含的成员属性有:车轮个数wheels和车重weight。的主要内容,如果未能解决你的问题,请参考以下文章
Java程序设计,创建一个汽车类CarModel.java,