首页 > 代码库 > java继承和多态
java继承和多态
父类和子类
如果类C1扩展自另一个类C2,那么C1称为子类或派生类,C2称为父类或基类。派生类可以从它的基类中继承可访问的数据域和方法,还可添加新数据域和新方法
例如:实现一个几何图形基类;
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; }}
一个派生类Circle:
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); }}
一个派生类Rectangle :
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); } }
创建Circle与Rectangle对象:
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()); }}
注意:
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;}
覆盖与重载
重载方法意味着可以定义多个同名的方法,但是这些方法具有不同的签名;覆盖方法以为着为子类中的方法提供一个全新的实现,该方法已经在父类中定义。
覆盖的例子:
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); }}
运行结果:
10.0
10.0
重载的例子:
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); }}
运行结果:
10
20.0
多态
可以将子类的实例传给需要父类类型的参数
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());}