java面向抽象编程样例
2015-10-30 19:17
369 查看
import java.util.*;
abstract class Geometry{
public abstract double getArea();
}
class Pillar{
Geometry bottom;
double height;
Pillar(Geometry bottom ,double height){
this.bottom=bottom;
this.height=height;
}
public double getVolume(){
return bottom.getArea()*height;
}
}
class Circle extends Geometry{
double r;
Circle(double r){
this.r=r;
}
public double getArea(){
return(3.14*r*r);
}
}
class Rectangle extends Geometry{
double a,b;
Rectangle(double a,double b){
this.a=a;
this.b=b;
}
public double getArea(){
return(a*b);
}
}
public class Main {
public static void main(String args[]){
Pillar pillar;
Geometry bottom;
bottom =new Rectangle(12,22);
pillar=new Pillar(bottom,58);
System.out.println("矩形的面积"+pillar.getVolume());
bottom=new Circle(10);
pillar=new Pillar(bottom,58);
System.out.println("圆柱的体积"+pillar.getVolume());
}
}
abstract class Geometry{
public abstract double getArea();
}
class Pillar{
Geometry bottom;
double height;
Pillar(Geometry bottom ,double height){
this.bottom=bottom;
this.height=height;
}
public double getVolume(){
return bottom.getArea()*height;
}
}
class Circle extends Geometry{
double r;
Circle(double r){
this.r=r;
}
public double getArea(){
return(3.14*r*r);
}
}
class Rectangle extends Geometry{
double a,b;
Rectangle(double a,double b){
this.a=a;
this.b=b;
}
public double getArea(){
return(a*b);
}
}
public class Main {
public static void main(String args[]){
Pillar pillar;
Geometry bottom;
bottom =new Rectangle(12,22);
pillar=new Pillar(bottom,58);
System.out.println("矩形的面积"+pillar.getVolume());
bottom=new Circle(10);
pillar=new Pillar(bottom,58);
System.out.println("圆柱的体积"+pillar.getVolume());
}
}
相关文章推荐
- java自动转型
- java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
- Java对象相关元素的初始化过程
- 深入JVM字节码执行引擎
- 我的第一篇博客,谈谈java的值传递
- (java)leetcode Longest Common Prefix
- JAVA设计模式之工厂设计模式
- (java)leetcode Excel Sheet Column Number
- spring DelegatingFilterProxy 过滤器 的原理及运用
- JDK内置工具使用介绍推荐
- Java基础之String中equals,声明方式,等大总结
- (java)leetcode Contains Duplicate
- Spring框架中获取连接池的四种方式
- eclipse中class注释中文乱码
- SpringMVC(二) 流程控制
- Java泛型(一)
- 在java中用递归函数求出1!+2!+3!+4!。。。。。。。
- Eclipse中Web工程Java类不编译问题
- (java)leetcode Invert Binary Tree
- myeclipse2014闪退的解决方法