可以聲明用於實現一個或者多個接口的類。實現接口的類必須聲明該接口的所有方法。
-將子類對象的引用賦給超類類型的變量的做法是安全的,因爲子類對象是其超類的對象。然而,該引用只能用於調用超類方法。如果代碼通過超類變量來引用子類專用的成員,則編譯器將報告錯誤。觀看一下錯誤代碼:
Point point;//超類
Circle circle = new Circle(120,12,2.4);//子類
point = circle ;
point.setX(132);//正確,因爲是超類中定義的方法
point.setRadius(2.5);//錯誤,不能調用子類的方法
-試圖將超類對象的引用賦給子類類型的變量的做法將產生一個編譯錯誤,觀看一下代碼:
public class TestDemo {
public static void main(String args[]) {
Point point = new Point(20, 20);//超類
Circle circle;//子類
circle = point;//錯誤
}
}
-抽象類和抽象方法:由於不能繼承構造函數,不能將它們聲明爲抽象方法。
觀看以下實現多態性的案例:
//定義超類
public abstract class Shape {
public double getArea(){
return 0.0;
}
public double getVolume(){
return 0.0;
}
public abstract String getName();
}
//Point 類擴展於Shape類
public class Point extends Shape{
private int x;
private int y;
public Point(){}
public Point(int x,int y){
this.x=x;
this.y=y;
}
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public String getName(){
return "Point";
}
public String toString(){
return "["+getX()+","+getY()+"]";
}
}
//Circle 擴展於Point類
public class Circle extends Point{
private double radius;
public Circle(){
}
public Circle(int xValue,int yValue,double r){
super(xValue,yValue);
setRadius(r);
}
public void setRadius(double radius) {
this.radius=(radius<0.0?0.0:radius);
}
public double getRadius() {
return radius;
}
public double getDiameter(){
return 2*radius;
}
public double getCircumference(){
return Math.PI*getDiameter();
}
public double getArea(){
return Math.PI*radius*radius;
}
public String toString(){
return "Center="+super.toString()+"; Radius= " +getRadius();
}
}
//Cylinder 類擴展於Circle類
import java.util.concurrent.CyclicBarrier;
public class Cylinder extends Circle {
private double height;
public Cylinder(){
}
public Cylinder(int x,int y, double radius,double heightValue){
super(x,y,radius);
setHeight(heightValue);
}
public void setHeight(double heightValue){
height=(heightValue<0.0?0.0:heightValue);
}
public double getHeight(){
return height;
}
public double getArea(){
return 2*super.getArea()+getCircumference()*getHeight();
}
public double getVolume(){
return super.getArea()*getHeight();
}
public String getName(){
return "Cylinder";
}
public String toString(){
return super.toString()+"; Height= "+getHeight();
}
}
//測試程序
import javax.swing.*;
import java.text.DecimalFormat;
public class TestDemo {
public static void main(String args[]){
DecimalFormat twoDight = new DecimalFormat("0.00");
Point point = new Point(7,11);
Circle circle = new Circle(22,8,3.5);
Cylinder cylinder = new Cylinder(20,30,3.3,10.75);
String output = point.getName()+":"+ point + circle.getName()+":"+circle+ cylinder.getName()+":"+cylinder;
//**********以下利用了多態性**
Shape a[] = new Shape[3];
a[0]= point;
a[1]= circle;
a[2]= cylinder;
//********
//**以下迭代方法**
for(int i = 0;i<a.length;i++){
output+="\n\n"+a[i].getName()+":"+a[i].toString()+"\nArea= "+twoDight.format(a[i].getArea())+"\nVolume= "+twoDight.format(a[i].getVolume());
}
JOptionPane.showMessageDialog(null,output);
System.exit(0);
}
}