继承

来源:互联网 发布:linux 复制命令 编辑:程序博客网 时间:2024/06/05 18:59

package bzu.aa;

public class Vehicle {

  protected int capacity;

public Vehicle(){

capacity=2;

System.out.println("执行交通工具类的无参构造方法");

}

public Vehicle(int capacity){

this.capacity=capacity;

System.out.println("执行交通工具类的无参构造方法");

}

public void setCapacity(int capacity){

this.capacity=capacity;

}

public int  getCapacity(){

return capacity;

}

public void print(){

System.out.println("载客量的值为:"+capacity);

    }

}

package bzu.aa;

public class Car extends  Vehicle {

public double speed;

public Car(){

 double speed=0;

System.out.println("执行汽车类的无参构造方法");

}


public Car(double speed,int capacity){

super(capacity);

this.speed=speed;

System.out.println("执行汽车类的无参构造方法");

}

public double speedUP(){

return speed+=10;

}

public double speedDown(){

return speed-=15;

}

public void print(){

System.out.println("载客量的值为:"+capacity+"速度的值为:"+speed);

    }

 

 

}

package bzn.bb;

import bzu.aa.Car;

public final class Bus extends Car {

final int capacity;

Bus(){

capacity=20;

System.out.println("执行公交车类的无参构造方法");

}

Bus(int capacity){

capacity=super.getCapacity();

this.capacity=capacity;

System.out.println("执行公交车类的无参构造方法");

}

public void print(){

System.out.println("载客量的值为:"+capacity+"速度的值为:"+speed);

System.out.println("父类载客量的值为:"+super.capacity);

}

}

package bzn.bb;

import bzu.aa.Car;

public class Test {

public static void main (String args[]){

Car car=new Car();

car.speed=40;

car.speedUP();

car.print();

car.speed=35;

car.speedDown();

car.print();

Bus bus=new Bus();

bus.print();

}

}



原创粉丝点击