java之 ------ 抽象類別

來源:互聯網
上載者:User

標籤:

封閉映像抽象類別及其子類

Area.java:

public interface Area{   public double area();}


Circle.java:

public final class Circle extends Ellipse        //圓類,最終類 {    public Circle(double radius)                 //構造方法,radius參數指定圓的半徑    {        super(radius,radius);        this.shape = "圓";    }    public Circle()    {        this(0);    }    public String toString()    {        return "半徑"+this.radius_a;    }    public static void main(String[] args)     {        new Circle(10).print();    }}


ClosedFigure.java:

public abstract class ClosedFigure               //閉合圖形抽象類別{    protected String shape;                      //形狀        protected ClosedFigure(String shape)         //構造方法,不能是抽象方法    {        this.shape = shape;    }    protected ClosedFigure()    {        this("未知");    }    public abstract double area();               //計算面積,抽象方法,以分號";"結束     public abstract double perimeter();          //計算周長,抽象方法    public void print()                          //顯示形狀、屬性、周長及面積    {        System.out.println("一個"+this.shape+","+this.toString()                           +",周長為"+this.perimeter()+",面積為"+this.area());    }}class ClosedFigure_ex  {    public static void main(String args[])       //抽象類別中可以包含main()方法    {        ClosedFigure g = new Ellipse(10,20);     //g引用橢圓對象        g.print();                               //顯示橢圓屬性        g = new Circle(10);                      //圓        g.print();        g = new Rectangle(10,20);                //g引用矩形對象        g.print();                               //顯示矩形屬性        g = new Square(10);                      //正方形        g.print();    }}


Ellipse.java:

public class Ellipse extends ClosedFigure              //橢圓類 {    protected double radius_a;                   //a軸半徑    protected double radius_b;                   //b軸半徑    public Ellipse(double radius_a, double radius_b)//構造方法    {        super("橢圓");        this.radius_a = radius_a;        this.radius_b = radius_b;    }    public Ellipse()    {        this(0,0);    }        public String toString()    {        return "a軸半徑"+this.radius_a+",b軸半徑"+this.radius_b;    }        public double area()                         //計算橢圓面積,覆蓋父類的抽象方法    {        return Math.PI*this.radius_a*this.radius_b;    }    public double perimeter()                    //計算橢圓周長,覆蓋父類的抽象方法    {        return Math.PI*(this.radius_a+this.radius_b);    }    public static void main(String args[])    {        new Ellipse(10,20).print();    }}


Rectangle.java:

public class Rectangle extends ClosedFigure            //矩形類{    protected double length;                     //長度    protected double width;                      //寬度    public Rectangle(double length, double width)//構造方法    {        super("矩形");        this.length = length;        this.width = width;    }    public Rectangle()    {        this(0,0);    }    public Rectangle(Rectangle r)    {        this(r.length, r.width);    }       public String toString()    {        return "長度"+this.length+",寬度"+this.width;    }        public double area()                         //計算矩形面積,實現父類的抽象方法    {        return this.width*this.length;    }    public double perimeter()                    //計算矩形周長,實現父類的抽象方法    {        return (this.width+this.length)*2;    }        public void setLength(double length)    {        this.length = length;    }    public void setWidth(double width)    {        this.width = width;    }    public double getLength()    {        return this.length;    }    public double getWidth()    {        return this.width;    }        public static void main(String args[])    {        new Rectangle(10,20).print();    }}


Square.java:

public final class Square extends Rectangle      //正方形是長方形的特例{    public Square(double width)    {        super(width,width);        this.shape = "正方形";    }    public Square()    {        this(0);    }        public String toString()    {        return "邊長"+this.length;    }    public static void main(String[] args) {        new Square(10).print();}}


java之 ------ 抽象類別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.