this.r=r; }
public double size(){ return 3.14*r*r; } }
class Conference implements Shape{ private double peoples; public Conference(double p){ peoples=p; }
public double size(){ return peoples; } }
class 圆柱体 implements Shape{ private Circle c; private double h;
public 圆柱体(Circle c1,double h1){ c=c1;h=h1; }
public double size(){
return c.size()*h; } }
public class Java2 {
public static void main(String args[]){ Shape s[]=new Shape[4]; s[0]=new Line(22,56,41,57); s[1]=new Circle(47); s[2]=new Conference(247); s[3]=new 圆柱体(new Circle(20),4); for (int z=0;z 8(3)定义一个抽象类水果,包括getweight()方法。 abstract class Fruit{ abstract public double getWeight(); } class Apple extends Fruit{ private double weight; public Apple(double weight){ this.weight=weight; } public double getWeight(){ return weight; } } class Peach extends Fruit{ private double weight; public Peach(double weight){ this.weight=weight; } public double getWeight(){ return weight; } } class Orange extends Fruit{ private double weight; public Orange(double weight){ this.weight=weight; } public double gerWeight(){ return weight; } @Override public double getWeight() { // TODO Auto-generated method stub return 0; } } public class Java3 { public static void main(String[]args){ Fruit s[]=new Fruit[3]; s[0]=new Apple(3); s[1]=new Peach(2); s[2]=new Orange(1); for(int k=0;k System.out.println(s[k].getClass().getName()+\ } } 8(4)定义Startstop接口,含有start()和stop()两个方法。分别创建会议和汽车两个类实现Startstop接口。 interface StartStop{ void Start(); void Stop(); } class Conference implements StartStop{ public void Start(){ System.out.println(\ } public void Stop(){ System.out.println(\} } class car implements StartStop{ public void Start(){ System.out.println(\ public void Stop(){ System.out.println(\ } }