分别定义一个形状(Shape)的抽象类和接口,该抽象类或接口有求面积的抽象方法getArea(),并且有正方形(Rect)和圆形(Circle)继承或实现Shape,利用类方法的重写,覆盖抽象方法getArea(),然后定义一个Test类,在main方法中生成具体对象,分别计算各个图形的面积。
方法一:
package p2;public abstract class Shape {
public abstract double Getarea(double length);
}
package p2;public class Circle extends Shape {
final double PI=3.14;
public double Getarea(double length){
return PI*length*length;
}
}
package p2;public class Rect extends Shape {
public double Getarea(double length){
return length*length;
}}
package p2;public class Test {
public static void main(String[] args){
Rect Jie=new Rect();
Circle S=new Circle();
System.out.println(S.Getarea(1));
System.out.println(Jie.Getarea(6));
}
}
方法二:
package p1;public abstract class Shape {
private double length;
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public abstract double getArea(); }
package p1;public class Circle extends Shape{
public double getArea() {
return Math.PI*this.getLength()*this.getLength()/4;
}}
package p1;public class Rect extends Shape{
public double getArea() {
return this.getLength()*this.getLength();
}}
package p1;public class Test {
public static void main(String[] args) {
Shape s=new Rect();
s.setLength(4);
System.out.println(s.getArea());
s=new Circle();
s.setLength(10);
System.out.println(s.getArea());
}
}
接口与上面的一样。
方法一:
package p2;public abstract class Shape {
public abstract double Getarea(double length);
}
package p2;public class Circle extends Shape {
final double PI=3.14;
public double Getarea(double length){
return PI*length*length;
}
}
package p2;public class Rect extends Shape {
public double Getarea(double length){
return length*length;
}}
package p2;public class Test {
public static void main(String[] args){
Rect Jie=new Rect();
Circle S=new Circle();
System.out.println(S.Getarea(1));
System.out.println(Jie.Getarea(6));
}
}
方法二:
package p1;public abstract class Shape {
private double length;
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public abstract double getArea(); }
package p1;public class Circle extends Shape{
public double getArea() {
return Math.PI*this.getLength()*this.getLength()/4;
}}
package p1;public class Rect extends Shape{
public double getArea() {
return this.getLength()*this.getLength();
}}
package p1;public class Test {
public static void main(String[] args) {
Shape s=new Rect();
s.setLength(4);
System.out.println(s.getArea());
s=new Circle();
s.setLength(10);
System.out.println(s.getArea());
}
}
接口与上面的一样。
如果把第一种方法中的虚基类改成接口,第一种方法和第二种就差不多一样好了.
length应该是个属性,面积是根据这个属性子东确定的,而不是根据传入的参数来计算面积。
Shape c = new Circle(2);
Shape r = new Rectangle(2, 3);
Shape s = new Square(2); System.out.println(c.getArea());
System.out.println(r.getArea());
System.out.println(s.getArea());
}
}abstract class Shape {
public abstract double getArea();
}class Circle extends Shape {
private double radius; public Circle(double radius) {
this.radius = radius;
} public double getArea() {
return Math.PI * radius * radius;
}
}class Rectangle extends Shape {
private double width;
private double height; public Rectangle(double width, double height) {
this.width = width;
this.height = height;
} public double getArea() {
return width * height;
}
}class Square extends Rectangle {
public Square(double sideLength) {
super(sideLength, sideLength);
}
}
对象产生时就应该确定的,并不需要传递参数进去,第一种方法可以说是面向对
象和面向过程的结合体。第二种写法我认为也不好,把 length 看作是 Shape 的一个属性了,Shape 是
图形的父类,应该表示一些公有的属性,像圆形这种图形只有半径,根本不存在
length,因此把 length 作为 Shape 的属性我认为不合理。如果把图形的颜色
作为 Shape 的属性倒是非常合理的。
Shape c = new Circle(2);
Shape r = new Rectangle(2, 3);
Shape s = new Square(2); System.out.println(c.getArea());
System.out.println(r.getArea());
System.out.println(s.getArea());
}
}abstract class Shape {
public abstract double getArea();
}class Circle extends Shape {
private double radius; public Circle(double radius) {
this.radius = radius;
} public double getArea() {
return Math.PI * radius * radius;
}
}class Rectangle extends Shape {
private double width;
private double height; public Rectangle(double width, double height) {
this.width = width;
this.height = height;
} public double getArea() {
return width * height;
}
}class Square extends Rectangle {
public Square(double sideLength) {
super(sideLength, sideLength);
}
}