應用場景:java
做用:ide
限制規定子類必須實現某些方法,但不關注細節。測試
使用規則:spa
舉例以下:設計
抽象類父類:code
package Java2_4_Practice; /** * 現有Shape圖形類,用Circle圓形子類,求圖形周長和麪積 * @author qiang * */ public abstract class Shape { public abstract void perimeter(); //抽象定義計算周長的方法 public abstract void area(); //抽象定義計算面積的方法 }
子類:input
package Java2_4_Practice; /** * 現有Shape圖形類,用Circle圓形子類,求圖形周長和麪積 * @author qiang * */ import java.util.Scanner; public class Circle extends Shape { @Override public void perimeter() { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("請輸入圓形的半徑:"); float a = input.nextFloat(); System.out.println("圓的周長爲:"+(a*2*Math.PI)); } @Override public void area() { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("請輸入圓形的半徑:"); float a = input.nextFloat(); System.out.println("圓的面積爲:"+(a*a*Math.PI)); } }
測試類模板
package Java2_4_Practice; /** * 現有Shape圖形類,用Circle圓形子類,求圖形周長和麪積 * @author qiang * */ public class Test1 { public static void main(String[] args) { // TODO Auto-generated method stub Shape shape1 = new Circle(); shape1.perimeter(); shape1.area(); } }