1. 编写一个Java应用程序,设计一个汽车类Vehicle,包含的成员属性有:车轮个数wheels和车重weight。
1.编写一个Java应用程序,设计一个汽车类Vehicle,包含的成员属性有:车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含属性载人...
1. 编写一个Java应用程序,设计一个汽车类Vehicle,包含的成员属性有:车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含属性载人数passenger_load。卡车Truck是Vehicle的子类,其中包含载人数passenger_load和载重量payload。要求每个类都有相关数据的输出方法。
2. 运行程序,理解成员变量的继承与隐藏。 展开
2. 运行程序,理解成员变量的继承与隐藏。 展开
2个回答
展开全部
classVehicle{privateintwheels;privatefloatweight;protectedVehicle(intwheels,floatweight){this。wheels=wheels;this。weight=weight。是普通的除号,即10/2=5。
编写java程序的注意事项:
大小写敏感:Java是大小写敏感的,这就意味着标识符Hello与hello是不同的。
类名:对于所有的类来说,类名的首字母应该大写。如果类名由若干单词组成,那么每个单词的首字母应该大写,例如 MyFirstJavaClass。
方法名:所有的方法名都应该以小写字母开头。如果方法名含有若干单词,则后面的每个单词首字母大写,例如myFirstJavaClass。
展开全部
/*
*编译运行均已通过
*直接复制黏贴即可
*文件名为: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吨
*/
*编译运行均已通过
*直接复制黏贴即可
*文件名为: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吨
*/
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询