定义一个shape类,在此类中定义一个computerarea()方法,定义它的3个子类 circle,

来源:百度知道 编辑:UC知道 时间:2024/06/08 00:42:43

public class Shape {
public void computerArea(){
System.out.println("父类方法");
}
}
public class Circle extends Shape {
public void computerArea(){
System.out.println("这是Circle类里的computerArea方法");
}
}
public class Retangle extends Shape{
public void computerArea(){
System.out.println("这是Retangle类里的computerArea方法");
}
}
import java.util.ArrayList;

public class Triangle extends Shape {
public void computerArea(){
System.out.println("这是Triangle类里的computerArea方法");
}
public static void main(String[] agrs){
Circle c=new Circle();
Retangle r=new Retangle();
Triangle t=new Triangle();
ArrayList<Shape> list=new ArrayList<Shape>();
list.add(c);
list.add(r);
list.add(t);
for(Shape p:list){
p.computerArea();
}

}
}