首页 > 编程语言 > 详细

java实训

时间:2019-05-06 01:27:06      阅读:113      评论:0      收藏:0      [点我收藏+]

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

class Vehicle{
 int wheels;
 int weight;
 public void a1(){
  System.out.println("车轮数:"+wheels+" 车重:"+weight);
 }
}
class car extends Vehicle{
 int wheels;
 int weight;
 int loader;
 public void a2(){
  System.out.println("车轮数:"+wheels+" 车重:"+weight+" 有载人数:"+loader);
 }
}
class Truck extends car{
 int wheels;
 int weight;
 int loader;
 int payload;
 public void a3(){
  System.out.println("车轮数:"+wheels+" 车重:"+weight+" 有载人数:"+loader+" 车载重:"+payload);
 }
}
public class Test {
 public static void main(String[] args) {  
  Truck e=new Truck();
  e.loader=4;
  e.payload=30;
  e.weight=50;
  e.wheels=4;
  e.a3();
 }
}
技术分享图片

 

 

java实训

原文:https://www.cnblogs.com/LYY1084702511/p/10817019.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!