继承
来源:互联网 发布: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();
}
}
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 继承
- 构造函数模式扩展
- 关于花生壳动态ip映射的一点唠叨
- 设计模式——小谈单例
- Socket到底是什么?
- 一个页面从输入URL到页面加载显示完成,这个过程都发生什java 基础知识-数组的7种算法(排序、求和、最值、遍历...)么?
- 继承
- 【poj 2299】 Ultra-QuickSort 【Waterloo local 2005.02.05】
- 20171013学习日记 CSS
- C Looooops(扩展欧几里得)
- PAT 1003.我要通过!(20) java
- 线代008 矩阵的运算
- [Tensorflow]1.交叉熵(Cross Entropy)算法实现及应用
- python求素数时间
- 一个页面从输入URL到页面加载显示完成,这个过程都发生什么?