课堂练习:
1.在包bzu.aa中定义一个交通工具类(Vehicle):
属性——载客量(capacity)
方法
无参构造方法(给capacity初始化值为2,并输出“执行交通工具类的无参构造方法。”)
有参构造方法(传参给capacity初始化,并输出“执行交通工具的有参构造方法。”)
capacity的set、get方法
print方法:输出capacity
2.在包bzu.aa中定义一个汽车类(Car)继承交通工具类:
属性——speed
方法
无参构造方法(给speed初始化值为0,并输出“执行汽车类的无参构造方法。”)
有参构造方法(用super关键字调用父类的有参构造方法,传参给speed初始化,并输出“执行汽车类的有参构造方法。”)
加速(speedup):speed+10并返回speed;
减速(speeddown):speed-15并返回speed;
重写print方法:输出speed和capacity。
3.在包bzu.bb中定义一个final的公交车类(Bus),继承汽车类:
属性——载客量(capacity)<变量隐藏>
方法
无参构造方法(给capacity初始化值为20,并输出“执行公交车类的无参构造方法。”)
有参构造方法(用super关键字调用父类的有参构造方法,传参给capacity初始化,并输出“执行公交车类的有参构造方法。”)
重写print方法:输出speed、 capacity及父类的capacity。
4.在包bzu.bb中编写一个主类Test:
主函数
结果展示:
1.在包bzu.aa中定义一个交通工具类(Vehicle):
属性——载客量(capacity)
方法
无参构造方法(给capacity初始化值为2,并输出“执行交通工具类的无参构造方法。”)
有参构造方法(传参给capacity初始化,并输出“执行交通工具的有参构造方法。”)
capacity的set、get方法
print方法:输出capacity
2.在包bzu.aa中定义一个汽车类(Car)继承交通工具类:
属性——speed
方法
无参构造方法(给speed初始化值为0,并输出“执行汽车类的无参构造方法。”)
有参构造方法(用super关键字调用父类的有参构造方法,传参给speed初始化,并输出“执行汽车类的有参构造方法。”)
加速(speedup):speed+10并返回speed;
减速(speeddown):speed-15并返回speed;
重写print方法:输出speed和capacity。
3.在包bzu.bb中定义一个final的公交车类(Bus),继承汽车类:
属性——载客量(capacity)<变量隐藏>
方法
无参构造方法(给capacity初始化值为20,并输出“执行公交车类的无参构造方法。”)
有参构造方法(用super关键字调用父类的有参构造方法,传参给capacity初始化,并输出“执行公交车类的有参构造方法。”)
重写print方法:输出speed、 capacity及父类的capacity。
4.在包bzu.bb中编写一个主类Test:
主函数
调用无参构造方法创建一个Car的对象car;调用加速方法将速度加至50,调用print方法;调用减速方法,将速度减至20,调用print方法。
调用有参构造方法创建一个Bus的对象bus;调用print方法。
代码如下:
package bzu.aa;
public class Vehicle {
int capacity;
Vehicle(){
this.capacity = 2;
System.out.println("执行交通工具类的无参构造方法,容量为:"+capacity);
}
Vehicle(int capacity){
this.capacity = capacity;
System.out.println("执行交通工具类的有参构造方法,容量为:"+capacity);
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public void print() {
System.out.println("输出容量:"+this.capacity);
}
}
package bzu.aa;
public class Car extends Vehicle {
private int speed;
Car() {
this.speed = 0;
System.out.println("执行汽车类的无参构造方法,速度为:"+speed);
}
Car(int capcity,int speed){
super(capcity);
this.speed = speed;
System.out.println("执行汽车类的有参构造方法,速度为:"+this.speed);
}
public int getSpeed() {
return speed;
}
public int speedUp() {
return speed+=10;
}
public int speedDown() {
return speed-=15;
}
public void print() {
System.out.println("速度为:"+this.speed+","+"容量为:"+getCapacity());
}
}
package bzu.aa;
public final class Bus extends Car{
public int capcity;
Bus(){
this.capacity = 2;
System.out.println("执行公交车类的无参构造方法,容量为:"+capacity);
}
Bus(int capacity,int speed){
super(capacity,speed);
this.capacity = capacity;
System.out.println("执行公交车类的有参构造方法,容量为:"+this.capacity);
}
public void print() {
System.out.println("速度为:"+getSpeed()+",容量为"+this.capacity+",父类容量为:"+getCapacity());
}
}
package bzu.aa;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Car car = new Car();
for (int i = 1 ; i < 6 ; i++) car.speedUp();
for (int j = 1 ; j < 3 ; j++) car.speedDown();
car.print();
Bus bus = new Bus(50,100);
bus.print();
}
}
结果展示: