簡單繼承例子:java

來源:互聯網
上載者:User

標籤:繼承

通用類,來繼承出圓和矩形。

package circle;public class Geometric {private String color="white";private boolean filled;private java.util.Date dateCreated;public Geometric(){dateCreated=new java.util.Date();}public Geometric(String color,boolean filled){dateCreated=new java.util.Date();this.filled=filled;this.color=color;}public java.util.Date getDateCreated(){return dateCreated;}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 String toString(){return "created on"+dateCreated+"\ncolor:"+color+"and filled "+filled;}}package circle;import java.util.*;public class Circle extends Geometric{private double radius;public Circle(){}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 Math.PI*radius*radius;}}package circle;public class Rectangle extends Geometric {private double width;private double height;public Rectangle(){}public Rectangle(double width,double height){this.width=width;this.height=height;}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);}}package circle;public class main {public static void main(String[] args) {/**Geometric g1=new Geometric();g1.setFilled(true);Geometric g2=new Geometric("black",false);System.out.print(g1.toString());System.out.print(g2.toString());*/Circle c1=new Circle();c1.setRadius(1.0);System.out.println(c1.getArea());Circle c2=new Circle(2.0,"red",true);System.out.println(c2.getArea());Rectangle r1=new Rectangle();System.out.println(r1.getArea());Rectangle r2=new Rectangle(1.0,2.0);System.out.println(r2.getArea());}}

java中有兩個包時,如果在一個包中使用其他包的類,可匯入,import.包名.類名;即可方便使用。


重載方法意味著可以定義多個同名的方法,但這些方法具有不同的簽名。

覆蓋方法意味著為子類中的方法提供一個全新的實現。具體例子如下:

//類A的方法P覆蓋了在類B中定義的同一個方法。public class Test{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);}}
//類B有兩個重載方法P(double i)和P(int i),類B的p(double i)方法被繼承。public class Test{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);}}


本文出自 “小止” 部落格,請務必保留此出處http://10541556.blog.51cto.com/10531556/1880621

簡單繼承例子: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.