第五次上机

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

package Programe;

public class Vehicle {
       public int wheels;
       public double weight;
       public Vehicle( int wheels,double weight){
    	   this.wheels = wheels;
    	   this.weight = weight;
       }
       public void Print_Data(){
    	   System.out.println("车轮数:"+wheels+"车重:"+weight);
       }
}

package Programe;

public class Car extends Vehicle{
       public int loader;
       public Car(int wheels,double weight,int loader){
    	   super(loader, weight);
    	   this.wheels = wheels;
    	   this.weight = weight;
    	   this.loader = loader;
       }
       @Override
       public void Print_Data(){
    	   System.out.println("车轮数:"+wheels+"车重:"+"weight"+"载人数:"+loader);
       }
}

package Programe;

public class Truck extends Car {
       public double payload;
       public Truck(int wheels,double weight,int loader,double payload){
    	   super(loader, payload, loader);
    	   this.wheels = wheels;
    	   this.weight = weight;
    	   this.loader = loader;
    	   this.payload = payload;
       }
       @Override
       public void Print_Data(){
    	   System.out.println("车轮数:"+wheels+"车重:"+weight+"载人数:"+loader+"载重量:"+payload);
       }
}

package Programe;
 
public class Test {
       public static void main(String[] args){
    	   Vehicle v = new Vehicle(4,400);
    	   Car c = new Car(4,500,6);
    	   Truck t = new Truck(6,800,4,2000);
    	   v.Print_Data();
    	   c.Print_Data();
    	   t.Print_Data();
       }
}

编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类E。要求:
(1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak()方法,在speak方法中输出“咿咿呀呀......”的信息。
(2)People类是Monkey类的子类,在People类中重写方法speak(),在speak方法中输出“小样的,不错嘛!会说话了!”的信息。
(3)在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”的信息。
(4)在主类E的main方法中创建Monkey与People类的对象类测试这2个类的功能。

package Programe;

public class Monkey {
       Monkey(String s){
       }
       public void speak(){
    	   System.out.println("咿咿呀呀......");
       }
}

package Programe;

public class People extends Monkey{
	   public People(String s){   
		   super(s);
	   }
       @Override
       public void speak(){
    	   System.out.println("小样的,不错嘛,会说话了!");
       }
       public void think(){
    	   System.out.println("别说话! 认真思考!");
       }
}

package Programe;

public class E {
       public static void main(String[] args){
    	      Monkey m = new Monkey("王佳胜");
    	      People p = new People("苏炎");
    	      System.out.println("王佳胜:");
    	      m .speak();
    	      System.out.println("苏炎:");
    	      p.speak();
    	      p.think();
  }
}
posted @ 2019-05-05 11:48  18软工2班苏炎  阅读(108)  评论(0编辑  收藏  举报