class A{
double n;
int m;
void f(){
System.out.printf("子类继承方法f(),n=%f,m=%d\n",n,m);
}
void g(){
System.out.printf("你好,n=%f,m=%d\n",n,m);
}
}
class B extends A{
int n=12;
void g(){
System.out.printf("子类重写方法g(),n=%d,m=%d\n",n,m);
}
void cry(){
System.out.printf("子类新增的方法,n=%d,m=%d\n",n,m);
}
}
public class Example5_8{
public static void main(String args[]){
A a;
a=new B();
a.n=0.618;
a.m=200;
a.f();
a.g();
B b=(B)a;
b.n=555;
b.cry();
}
}
class Animal{
void cry(){}
}
class Dog extends Animal{
void cry(){
System.out.println("Wang!Wang!......");
}
}
class Cat extends Animal{
void cry(){
System.out.println("Miao~~Miao~~...");
}
}
public class Example5_9{
public static void main(String args[]){
Animal animal;
animal=new Dog();
animal.cry();
animal=new Cat();
animal.cry();
}
}
public abstract class 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=botttom;
}
public double getVolume(){
return bottom.getArea()*height;
}
}
public class Lader extends 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 extends Geometry{
double r;
Circle(double r){
this.r=r;
}
public double getArea(){
return(3.14*r*r);
}
}
public class Example5_10{
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());
}
}