2014年9月13日 星期六
繼承練習-形狀(圓形繼承橢圓、橢圓繼承圓形、都不繼承或著...)
Shape.java:
public interface Shape {
public double area();
public void display();
}
RoundShape.java:
public class RoundShape implements Shape {
private int radius1;
private int radius2;
public RoundShape(int radius1,int radius2) {
this.radius1 = radius1;
this.radius2 = radius2;
}
@Override
public double area() {
return radius1 * radius2 * 3.14;
}
@Override
public void display() {
System.out.println("draw and display...");
}
}
Circle.java:
public class Circle extends RoundShape {
public Circle(int radius) {
super(radius, radius);
}
}
Ellipse.java:
public class Ellipse extends RoundShape {
public Ellipse(int radius1, int radius2) {
super(radius1, radius2);
}
}
UserTest.java:
public class UserTest {
public static void main(String[] args) {
Circle circle = new Circle(10);
System.out.println(circle.area());
Ellipse ellipse = new Ellipse(5,10);
System.out.println(ellipse.area());
}
}
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言