第七周java作业

来源:互联网 发布:数据堂 王大亮 编辑:程序博客网 时间:2024/06/01 03:59

题目:在包bzu.aa中定义一个交通工具类(Vehicle):

 属性——载客量capacity

 方法

无参构造方法(给capacity初始化值为2并输出执行交通工具无参构造方法。

 有参构造方法(传capacity初始化,并输出执行交通工具的有参构造方法。

capacitysetget方法

print方法输出capacity

– bzu.aa中定义一个汽车类Car)继承交通工具类

属性——speed

 方法

无参构造方法(给speed初始化值为0并输出执行汽车类无参构造方法。

有参构造方法(用super关键字调用父类的有参构造方法,speed初始化,并输出执行汽车类有参构造方法。

加速speedupspeed+10返回speed

减速speeddownspeed-15返回speed

重写print方法:输出speedcapacity

– bzu.bb中定义一个final公交车类(Bus继承汽车类:

属性——载客量capacity<变量隐藏>

 方法

 无构造方法(给capacity初始化值为20并输出执行公交车类无参构造方法。

有参构造方法(用super关键字调用父类的有参构造方法,capacity初始化,并输出执行公交车类有参构造方法。

重写print方法:输出speed capacity父类的capacity

– bzu.bb中编写一个主类Test

主函数

调用无参构造方法创建一个Car的对象car调用加速方法将速度加至50调用print方法;调用减速方法,将速度减至20调用print方法。

调用有参构造方法创建一个Bus的对象bus;调用print方法。

代码:

package bzu.aa;

public class Car extends Vehicle{
 public int speed;

 public Car(){
  super();
  speed = 0;
  System.out.println("执行汽车类的无参构造方法");
 }
 public Car(int speed){
  super(3);
  this.speed = speed;
  System.out.println("执行汽车类的有参构造方法");
 }
public void setSpeedUp(int speed) {
 speed=speed+10;
 
}
public int getSpeedUp(){
return speed;
}
public void setSpeedDown(int speed) {
 speed=speed-15;
 
}
public int getSpeedDown(){
return speed;
}
 public void print(){
 System.out.println("载客量为:"+capacity);
 System.out.println("速度为:"+speed);
}
}

package bzu.aa;

public class Vehicle {
 protected int capacity;
 public Vehicle(){
  capacity=2;
  System.out.print("执行交通工具类的无参构造方法.");
 }
  public Vehicle(int C){
    capacity=C;
    System.out.print("执行交通工具类的有参构造方法.");
   }
   public void setCapacity(){
   
   }
   public int  getCapacity(){
    return capacity;
   }
   void print(){
    System.out.println("载客量为:"+capacity);
   }
}

package bzu.bb;

import bzu.aa.Car;

public class Bus extends Car{
int capacity;
Bus(){
 capacity=20;
 System.out.print("执行公交类的无参构造方法.");
 
}
public  Bus(int capacity){
 super(20);
 this.capacity=capacity;
 System.out.println("执行公交类的有参构造方法。");
 }
public void print(){
  System.out.println("载客量为:"+capacity);
  System.out.println("速度为:"+speed);
  System.out.println("载客量为;"+super.capacity);
 }
}

package bzu.bb;

import bzu.aa.Car;

public class Test {
 public static void main(String[] args){
Car car=new Car();
car.setSpeedUp(40);
car.getSpeedUp();
car.setSpeedDown(35);
car.getSpeedDown();
Bus bus = new Bus(25);
bus.print();
}
}

package bzu.bb;

import bzu.aa.Car;

public class Test {
 public static void main(String[] args){
Car car=new Car();
car.setSpeedUp(40);
car.getSpeedUp();
car.setSpeedDown(35);
car.getSpeedDown();
Bus bus = new Bus(25);
bus.print();
}
}

 package bzu.bb;

import bzu.aa.Car;

public class Bus extends Car{
int capacity;
Bus(){
 capacity=20;
 System.out.print("执行公交类的无参构造方法.");
 
}
public  Bus(int capacity){
 super(20);
 this.capacity=capacity;
 System.out.println("执行公交类的有参构造方法。");
 }
public void print(){
  System.out.println("载客量为:"+capacity);
  System.out.println("速度为:"+speed);
  System.out.println("载客量为;"+super.capacity);
 }
}

截图:

 

 总结:1.在一个类中可以选择定义有参的构造方法,也可以定义无参的构造方法。

             2.测试类一定要写主函数。

                  


 

 

 

 

 

 

原创粉丝点击