天天看點

掌握接口,從這三則案例開始! | 帶你學《Java面向對象程式設計》之六十二

上一篇:快速厘清抽象類與接口 | 帶你學《Java面向對象程式設計》之六十一

【本節目标】

通過閱讀本節内容,你将掌握接口的一般用法,并能夠做到熟練使用接口,通過某些設計模式完成項目的開發。

抽象類與接口是Java裡面最為核心的概念,也是所有設計模式的綜合展現,包括在日後學習的過程之中也會接觸到許多的系統提供的接口和抽象類。接口與抽象類在都可以使用的情況下,一定要優先使用接口,因為接口可以避免單繼承的局限。

案例分析一

定義一個ClassName接口,接口中隻有一個抽象方法getClassName();設計一個類Company,該類實作接口ClassName中的方法getClassName(),功能是擷取該類的類名稱;編寫應用程式使用Company類。

interface IClassName {    //按照要求定義接口
    public String getClassName() ;     //資料一定要傳回
}
class Company implements IClassName {
    public String getClassName() {
        return “Company” ;
    }
}
public class JavaDemo {
    public static void main(String args[]) {
        IClassName ica = new Company() ;
        System.out.println(ica.getClassName()) ;
    }
}           

執行結果:Company

接口前追加一個字母I是一個良好的習慣,也是這幾年一直強調的定義原則。

案例分析二(繪圖處理)

考慮一個繪圖的标準,并且可以根據不同的圖形來進行繪制;

圖一 繪圖示準

interface IGraphical {    //定義繪圖示準
    public void paint() ;   //繪圖
}
class Point {
    private double x ;
    private double y ;
    public Point(double x , double y) {
        this.x = x ;
        this.y = y ;
    }
    public double getX() {
        return this.x ;
    }
    public double getY() {
        return this.y ;
    }
}
class Triangle implements IGraphical {   //繪制三角形
    private Point [] x ;     //儲存第一條邊的坐标
    private Point [] y ;     //儲存第二條邊的坐标
    private Point [] z ;     //儲存第三條邊的坐标
    public Triangle(Point [] x , Point [] y , Point [] z) {
        this.x = x ;
        this.y = y ;
        this.z = z ;
    }
    public void paint() {
        System.out.println(“繪制第一條邊,開始坐标:[” + this.x[0].getX() + “,” + this.x[0].getY() + “],結束坐标:[” + this.x[1].getX() + “,” + this.x[1].getY() + “]”) ;
        System.out.println(“繪制第二條邊,開始坐标:[” + this.y[0].getX() + “,” + this.y [0].getY() + “],結束坐标:[” + this.y[1].getX() + “,” + this.y[1].getY() + “]”) ;
        System.out.println(“繪制第三條邊,開始坐标:[” + this.z[0].getX() + “,” + this.z[0].getY() + “],結束坐标:[” + this.z[1].getX() + “,” + this.z[1].getY() + “]”) ;
    }
}
class Circular implements IGraphical {
    private double radius ;
    public Circular(double radius) {
        this.radius = radius ;
    }
    public void paint() {
        System.out.println(“以半徑為 “” + this.radius + “”繪制圓形。”) ;
    }
}
class Factory {
    public static IGraphical getInstance(String className , double … args) {
        if (“Triangle”.equalsIgnoreCase(className)) {
           return new Triangle(new Point [] {new Point(args[0] , args[1]) , new Point(args[2] , args[3])} ,new Point [] {new Point(args[4] , args[5]) , new Point(args[6] , args[7])} ,new Point [] {new Point(args[8] , args[9]) , new Point(args[10] , args[11])}) ;
       }else if(“circular”.equalsIngnoreCase(className)) {
           return new Circular(args[0]) ;
       }else {
           return null ;
       }
    }
}
public class JavaDemo {
    public static void main(String args[]) {
       IGraphical iga = Factory.getInstance(“triangle” , 1.1,2.2,2.3.3,4.4,11.11,22.22,33.33,44.44,111.111,222.222,333.333,444.444) ;
       iga.paint() ;
       IGraphical igb = Factory.getInstance(“circular” , 88.11) ;
       igb.paint() ;
    }
}           

圖二 執行結果圖

案例分析三(圖形)

定義類Shape,用來表示一般二維圖形。Shape具有抽象方法area和perimeter,分别用來計算形狀的面積和周長。試定義一些二維形狀類(如矩形、三角形、圓形、橢圓形等),這些類均為Shape類的子類。

圖三 圖形設計

abstract class AbstractShape {
    public abstract double area() ;
    public abstract double perimeter() ;
}
class Circular extends AbstractShape {
    private double radius ;
    public Circular(double radius) {
         this.radius = radius ;
    }
    public double area() {
        return 3.1415926 * this.radius * this.radius ;
    }
    public double perimeter() {
        return 2 * 3.1415926 * this.radius ;
    }
}
class Rectangle extends AbstractShape {
    private double length ;
    private double width ;
    public Rectangle(double length , double width ) {
        this.length = length ;
        this.width = width ;
    }
    public double area() {
        return this.length * this.width ;
    }
    public double perimeter() {
        return 2 * (this.length + this.width) ;
    }
}
class Factory {
    public static AbstractShape getInstance(String className , double … args) {
        if (“Circular”.equalsIgnoreCase(className)) {
           return new Circular(args[0]) ;
        }else if (“Rectangle”. equalsIgnoreCase(className)) {
           return new Circular(args[0] , args[1]) ;
        }else {
           return null ;
        }
    }
}
public class JavaDemo {
    public static void main(String args[]) {
        AbstractShape asa = Factory.getInstance(“Circular” , 1.1) ;
        AbstractShape asb = Factory.getInstance(“Rectangle” , 1.5 , 10.2) ;
        System.out.println(“圓形面積:” + asa.area() + “、圓形周長:” + asa. perimeter()) ;
        System.out.println(“矩形面積:” + asb.area() + “、矩形周長:” + asb. perimeter()) ;
    }
}           

圖四 執行圖形結果圖

使用工廠設計模式完全隐藏了實作的子類。

想學習更多的Java的課程嗎?從小白到大神,從入門到精通,更多精彩不容錯過!免費為您提供更多的學習資源。

本内容視訊來源于

阿裡雲大學 下一篇:危險的轉型操作 | 帶你學《Java面向對象程式設計》之六十三 更多Java面向對象程式設計文章檢視此處