class vehicle // 定义汽车类
{
protected:
int wheels// 车轮数
float weight// 重量
public:
vehicle(int wheels,float weight)
int get_wheels()
float get_weight()
float wheel_load()
void show()
}
class car:public vehicle // 定义小车类
{
int passenger_load// 载人数
public:
car(int wheels,float weight,int passengers=4)
int get_passengers()
void show()
}
class truck:public vehicle // 定义卡车类
{
int passenger_load// 载人数
float payload// 载重量
public:
truck(int wheels,float weight,int passengers=2,float max_load=24000.00)
int get_passengers()
float efficiency()
void show()
}
vehicle::vehicle(int wheels,float weight)
{
vehicle::wheels=wheels
vehicle::weight=weight
}
int vehicle::get_wheels()
{
return wheels
}
float vehicle::get_weight()
{
return weight/wheels
}
void vehicle::show()
{
cout <<"车轮:" <<wheels <<"个" <<endl
cout <<"重量:" <<weight <<"公斤" <<endl
}
car::car(int wheels, float weight,
int passengers) :vehicle (wheels, weight)
{
passenger_load=passengers
}
int car::get_passengers ()
{
return passenger_load
}
void car::show()
{
cout <<" 车型:小车" <<endl
vehicle::show()
cout <<"载人:" <<passenger_load <<"人" <<endl
cout <<endl
}
truck:: truck(int wheels, float weight,int passengers, float max_load):vehicle(wheels,weight)
{
passenger_load=passengers
payload=max_load
}
int truck::get_passengers()
{
return passenger_load
}
float truck::efficiency()
{
return payload/(payload+weight)
}
void truck::show()
{
cout <<"车型:卡车" <<endl
vehicle:: show ()
cout <<"载人:" <<passenger_load <<"人" <<endl
cout <<"效率:" <<efficiency() <<endl
cout <<endl
}
void main ()
{
car car1(4,2000,5)
truck tru1(10,8000,3,340000)
cout <<"输出结果" <<endl
car1. show ()
tru1. show ()
}
//汽车类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() { } // 带参构造函数 public vehicle(int wheels, int weight) { super()this.wheels = wheelsthis.weight = weight} public void outInfo(){ System.out.println("我是vehicle")System.out.println("我有"+wheels+"个轮子")System.out.println("我的重量是:"+weight+"吨")}}____________________________________________________________________________ //小汽车类public class Car extends vehicle { private int passengers// 载人数 // 无参构造函数 public Car() { super()} // 带参构造函数 public Car(int wheels, int weight, int passengers) { super(wheels, weight)setPassengers(passengers)} public int getPassengers() { return passengers} public void setPassengers(int passengers) { this.passengers = passengers} public void outInfo(){ System.out.println("我是一辆Car")System.out.println("我有"+super.getWheels()+"个轮子")System.out.println("我的重量是:"+super.getWeight()+"吨")System.out.println("我的载人数是:"+passengers+"人")}}______________________________________________________________________ //卡车类public class Truck extends vehicle { private int passengers// 载人数 private int payload// 载重量 // 无参构造函数 public Truck() { super()} // 带参构造函数 public Truck(int wheels, int weight, int passengers, int payload) { super(wheels, weight)setPassengers(passengers)setPayload(payload)} public int getPassengers() { return passengers} public void setPassengers(int passengers) { this.passengers = passengers} public int getPayload() { return payload} public void setPayload(int payload) { this.payload = payload} public void outInfo(){ System.out.println("我是一辆Truck")System.out.println("我有"+super.getWheels()+"个轮子")System.out.println("我的重量是:"+super.getWeight()+"吨")System.out.println("我的载人数是:"+passengers+"人")System.out.println("我的载重量是:"+payload+"吨")}}______________________________________________________________________ //测试类public class Test { public static void main(String[] args) { vehicle v1 = new vehicle(3, 2)vehicle v2 = new Car(4, 3, 4)vehicle v3 = new Truck(6, 6, 3, 12)v1.outInfo()v2.outInfo()v3.outInfo()} }classVehicle{privateintwheels;privatefloatweight;protectedVehicle(intwheels,floatweight){this。wheels=wheels;this。weight=weight。是普通的除号,即10/2=5。
编写java程序的注意事项:
大小写敏感:Java是大小写敏感的,这就意味着标识符Hello与hello是不同的。
类名:对于所有的类来说,类名的首字母应该大写。如果类名由若干单词组成,那么每个单词的首字母应该大写,例如 MyFirstJavaClass。
方法名:所有的方法名都应该以小写字母开头。如果方法名含有若干单词,则后面的每个单词首字母大写,例如myFirstJavaClass。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)