public class Example5_14 {
public static void main(String args[]) { Pillar pillar; Geometry tuxing; tuxing=new Lader(12,22,100); System.out.println("梯形的面积"+tuxing.getArea()); pillar=new Pillar(tuxing,58); System.out.println("梯形底的柱体的体积"+pillar.getVolume()); tuxing=new Circle(10); System.out.println("半径是10的圆的面积"+tuxing.getArea()); pillar.changeBottom(tuxing); System.out.println("圆形底的柱体的体积"+pillar.getVolume()); }}public interface Geometry {
public abstract double getArea();
}
public class Pillar {
Geometry bottom; double height; Pillar(Geometry bottom,double height){ this.bottom=bottom; this.height=height; } void changeBottom(Geometry bottom){ this.bottom=bottom; } public double getVolume(){ return bottom.getArea()*height; } }public class Lader implements Geometry {
double a,b,h; Lader(double a,double b,double h){ this.a=a;this.b=b;this.h=h; } public double getArea(){ return((1/2.0)*(a+b)*h); }}public class Circle implements Geometry{
double r; Circle(double r){ this.r=r; } public double getArea(){ return(3.14*r*r); }}
class NorthEast{
String land="黑土地";
}
class China{
int x=10,y=10;
LiaoNing dalian;
China(){
dalian=new LiaoNing();
}
void f(){
System.out.println("我是中国");
dalian.speak();
}
class LiaoNing extends NorthEast{
int z; void speak(){
System.out.println("我是大连,z="+z+":"+land);
}
void g(){
z=x+y;
f(); }
}
}
public class Example5_15 {
public static void main(String[] args) {
China china=new China();
china.f();
china.dalian.g();
}
}
abstract class Student{
abstract void speak();
}
class Teacher{
void look(Student stu){
stu.speak();
}
}
public class Example5_16 {
public static void main(String[] args) {
Teacher zhang=new Teacher();
zhang.look(new Student(){
void speak(){
System.out.println("这是匿名类中的方法");
}
}
);
}
}