继承:
如果类A1扩展自另一个类A,那么A1称为子类或派生类,A称为父类或基类。派生类可以从它的基类中继承可访问的数据域和方法,还可添加新数据域和新方法
例如:实现一个汽车基类;
int[] Speed = { 100, 200, 300, 400, 500 };
int[] Member = { 5, 7, 9, 15, 20 };
class Car {
private String name = "car";
private boolean filled;
private java.util.Date dateCreated;
public Car() {
dateCreated = new java.util.Date();
}
public Car(String name, boolean filled) {
dateCreated = new java.util.Date();
this.name = name;
this.filled = filled;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public String toString() {
return "created on " + dateCreated + "\nname: " + name + " and filled: " + filled;
}
}
一个派生类Truck:
class Truck extends Car {
private int type;
public truck(int type) {
this.type = type;
}
public truck(int type, String name, boolean filled) {
this.type = type;
setName(name);
setFilled(filled);
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getSpeed() {
return Speed[type];
}
public double getMember() {
return Member[type];
}
public void printCar() {
System.out.println("The Car is creatd " + getDateCreated() +
" and the type is " + type);
}
}
一个派生类Bus :
class Bus extends Car {
private double width;
private double height;
private int type;
public Bus() {
}
public Bus(double width, double height,int type) {
this.height = height;
this.width = width;
this.type = type;
}
public Bus(double width, double height, int type,String name, boolean filled) {
this.width = width;
this.height = height;
this.type = type;
setName(name);
setFilled(filled);
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getSpeed() {
return Speed[type];
}
public double getMember() {
return Member[type];
}
}
创建Truck与Bus对象:
public class Main
{
public static void main(String args[])
{
Truck truck = new Truck(1);
System.out.println("A truck " + truck.toString());
System.out.println("The type is " + truck.getType());
System.out.println("The Speed is " + truck.getSpeed());
System.out.println("The Member is " + truck.getMember());
Bus bus = new Bus(2, 3,3);
System.out.println("\nA bus " + bus.toString());
System.out.println("The Speed is " + bus.getSpeed());
System.out.println("The Member is " + bus.getMember());
}
}
注意:
public Truck(int type, String name, boolean filled) {
super(name, filled);
this.type = type;
}
super不仅可以引用父类的构造方法,也可以引用父类的方法:
public void printCircle() {
System.out.println("The Car is creatd " + super.getDateCreated() +
" and the type is " + type);
}
public String toString() {
return super.toString() + "\ntype is " + type;
}
public class Main
{
public static void main(String args[])
{
A a = new A();
a.p(10);
a.p(10.0);
}
}
class B {
public void p(double i) {
System.out.println(i * 2);
}
}
class A extends B {
public void p(double i) { //覆盖(同名同参数)
System.out.println(i);
}
}
重载的例子:
public class Main
{
public static void main(String args[])
{
A a = new A();
a.p(10);
a.p(10.0);
}
}
class B {
public void p(double i) {
System.out.println(i * 2);
}
}
class A extends B {
public void p(int i) { //重载(同名不同参数)
System.out.println(i);
}
}