通用类,来继承出圆和矩形。
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); } }
时间: 2024-10-04 22:15:31