JAVA学习记录(三)面向对象编程三大特性之继承

继承:

如果类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());
        
    }
}
注意:

1、派生类并不是基类的一个子集,事实上比父类包含更多的信息和方法

2、父类中的私有数据域在该类之外是不可访问的,如果父类中定义了公共的访问器/修改器,那么可以通过这些公共的访问器/修改器来访问和修改它们

3、不是所有的“是”关系(is-a)都该用继承来建模。例如:如果要用类B去扩展类A,那么A应该要比B包含更多的信息

4、java中不允许多重继承


使用super关键字

关键字super的用途:

1、调用父类的构造方法

2、调用父类的方法

调用父类的构造方法的语法:super(), or super(parameters)

语句super(), or super(parameters)必须出现在子类构造方法的第一行,这是显式调用父类构造方法的唯一方式

上面代码Truck类中的构造方法可以使用下面的代码替换:

public Truck(int  type, String name, boolean filled) {
    super(name, filled);
    this.type = type;
}
super不仅可以引用父类的构造方法,也可以引用父类的方法:

super.方法名(参数)

改写Truck类中的printCar()方法:
public void printCircle() {
        System.out.println("The Car is creatd " + super.getDateCreated() +
                " and the type is " + type);
}

覆盖方法

子类从父类继承方法,有时候需要修改父类中定义的方法的实现,称为方法覆盖

Car类中的toString方法返回表示几何对象的字符串。这个方法可以被覆盖,返回表示圆的字符串,例如下面Truck中的新方法:

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);
    }
}














猜你喜欢

转载自blog.csdn.net/cyq6239075/article/details/78943811