分别定义一个形状(Shape)的抽象类和接口,该抽象类或接口有求面积的抽象方法getArea(),并且有正方形(Rect)和圆形(Circle)继承或实现Shape,利用类方法的重写,覆盖抽象方法getArea(),然后定义一个Test类,在main方法中生成具体对象,分别计算各个图形的面积
注意:
1.重写要求覆盖父类的求面积的方法getArea();
------解决方案--------------------
作业题自己搞定
------解决方案--------------------
去《thinking in java》书上看吧,那里有经典例子
------解决方案--------------------
------解决方案--------------------
LZ果然很懒...这东西,有发帖这功夫都做完了-___-||
------解决方案--------------------
这么简单的作业 - -!
------解决方案--------------------
public abstract class TestTable {
public abstract float getArea(float r);
}
public class A extends TestTable {
// Rect
public float getArea(float r) {
float area = r * r;
return area;
}
}
public class B extends TestTable {
// Circle
public float getArea(float r) {
float area = (float) (Math.PI * r * r);
return area;
}
}
------解决方案--------------------
这么简单的作业都跑上论坛来发帖
唉。。。
------解决方案--------------------
仅供参考,希望还是自己试着写写
public abstract class Shape{
public abstract float getArea(float r);
}
public class Rect extends Shape{
public float getArea(float r) {
float area = r * r;
return area;
}
}
public class Circle extends Shape{
public float getArea(float r) {
float area = (float) (Math.PI * r * r);
return area;
}
}
public class Test {
public static void main(String[] args){
Rect rt=new Rect();
System.out.println(rt.getArea(12));
Circle cl=new Circle();
System.out.println(cl.getArea(12));
}
}
------解决方案--------------------
public interface Shape {
double Getarea(double length);
}
public class Circle implements Shape {
private final double PI = 3.14;
public double Getarea(double length) {
return PI * length * length / 4;
}
}
public class Rect implements Shape {
public double Getarea(double length) {
return length * length;
}
}
public class Test {
public static void main(String[] args) {
Rect Jie = new Rect();
Circle S = new Circle();
System.out.println(Jie.Getarea(2));
System.out.println(S.Getarea(6));
}
}