首页 > 编程语言 > 详细

java继承和多态

时间:2014-07-21 00:20:53      阅读:308      评论:0      收藏:0      [点我收藏+]

父类和子类

如果类C1扩展自另一个类C2,那么C1称为子类或派生类,C2称为父类或基类。派生类可以从它的基类中继承可访问的数据域和方法,还可添加新数据域和新方法

例如:实现一个几何图形基类;

bubuko.com,布布扣
class GeometricObject1 {
    private String color = "white";
    private boolean filled;
    private java.util.Date dateCreated;
    public GeometricObject1() {
        dateCreated = new java.util.Date();
    }
    public GeometricObject1(String color, boolean filled) {
        dateCreated = new java.util.Date();
        this.color = color;
        this.filled = filled;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    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 + "\ncolor: " + color + " and filled: " + filled;
    }
}
bubuko.com,布布扣

一个派生类Circle:

bubuko.com,布布扣
class Circle extends GeometricObject1 {
    private double radius;
    
    public Circle(double radius) {
        this.radius = radius;
    }
    public Circle(double radius, String color, boolean filled) {
        this.radius = radius;
        setColor(color);
        setFilled(filled);
    }
    public double getRadius() {
        return radius;
    }
    public void setRadius(double radius) {
        this.radius = radius;
    }
    public double getArea() {
        return radius * radius * Math.PI;
    }
    public double getDiameter() {
        return 2 * radius;
    }
    public double getPerimeter() {
        return 2 * Math.PI * radius;
    }
    public void printCircle() {
        System.out.println("The circle is creatd " + getDateCreated() +
                " and the radius is " + radius);
    }
}
bubuko.com,布布扣

一个派生类Rectangle :

bubuko.com,布布扣
class Rectangle extends GeometricObject1 {
    private double width;
    private double height;
    public Rectangle() {
        
    }
    public Rectangle(double width, double height) {
        this.height = height;
        this.width = width;
    }
    public Rectangle(double width, double height, String color, boolean filled) {
        this.width = width;
        this.height = height;
        setColor(color);
        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 getArea() {
        return width * height;
    }
    public double getPerimeter() {
        return 2 * (width + height);
    }    
}
bubuko.com,布布扣

创建Circle与Rectangle对象:

bubuko.com,布布扣
public class Main
{
    public static void main(String args[])
    {
        Circle circle = new Circle(1);
        System.out.println("A circle " + circle.toString());
        System.out.println("The radius is " + circle.getRadius());
        System.out.println("The area is " + circle.getArea());
        System.out.println("The diameter is " + circle.getDiameter());
        
        Rectangle rectangle = new Rectangle(2, 4);
        System.out.println("\nA rectangle " + rectangle.toString());
        System.out.println("The area is " + rectangle.getArea());
        System.out.println("The perimeter is " + rectangle.getPerimeter());
        
    }
}
bubuko.com,布布扣

注意:

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

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

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

4、java中不允许多重继承

使用super关键字

关键字super的用途:

1、调用父类的构造方法

2、调用父类的方法

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

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

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

public Circle(double radius, String color, boolean filled) {
    super(color, filled);
    this.radius = radius;
}

super不仅可以引用父类的构造方法,也可以引用父类的方法:

super.方法名(参数)

改写Circle类中的printCircle()方法:

public void printCircle() {
        System.out.println("The circle is creatd " + super.getDateCreated() +
                " and the radius is " + radius);
}

覆盖方法

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

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

public String toString() {
    return super.toString() + "\nradius is " + radius;
}

覆盖与重载

重载方法意味着可以定义多个同名的方法,但是这些方法具有不同的签名;覆盖方法以为着为子类中的方法提供一个全新的实现,该方法已经在父类中定义。

覆盖的例子:

bubuko.com,布布扣
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);
    }
}
bubuko.com,布布扣

运行结果:

10.0
10.0

重载的例子:

bubuko.com,布布扣
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);
    }
}
bubuko.com,布布扣

运行结果:

10
20.0

多态

可以将子类的实例传给需要父类类型的参数 

bubuko.com,布布扣
public class Main
{
    public static void main(String args[])
    {
        displayObject(new Circle(1, "red", false));
        displayObject(new Rectangle(1, 1, "black", true));    
    }
}

public static void displayObject(GeometricObject object) {
    System.out.println("Created on " + object.getDateCreated() +
            ".Color is " + object.getColor());
}

java继承和多态,布布扣,bubuko.com

java继承和多态

原文:http://www.cnblogs.com/yhws/p/3856567.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!