你的位置:首页 > Java教程

[Java教程]子类会继承父类对于接口的实现


 项目截图:

 

实现代码:

 1 package exercise2; 2  3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6  7 interface CircleShape { 8   double PI = 3.14159; 9 10   double area(double radius);11 }12 13 class Circle implements CircleShape {14   int radius;15 16   public Circle(int r) {17     radius = r;18   }19 20   public double perimeter() {21     return (2 * PI * radius);22   }23 24   @Override25   public double area(double radius) {26     return (PI * radius * radius);27   }28 }29 30 class Cylinder extends Circle implements CircleShape {// 子类会继承父类对于接口的实现31   int heigh;32   int radius;33 34   public Cylinder(int r, int h) {35     super(r);36     heigh = h;37   }38 39   public double baseAera(double radius) {40     return area(radius);41   }42 43   public double area1(double radius) {44     return (heigh * super.perimeter() + 2 * area(radius));45   }46 47   public double volume(double radius) {48     return (heigh * area(radius));49   }50 }51 52 public class Ex13_2 {53   public static void main(String[] args) throws IOException {54     int r1, r2;55     String t;56     System.out.println("请输入圆cir1和cir2的半径r1,r2");57     BufferedReader scan = new BufferedReader(new InputStreamReader(58         System.in));59     t = scan.readLine();60     r1 = Integer.parseInt(t);61     t = scan.readLine();62     r2 = Integer.parseInt(t);63     Circle cir1 = new Circle(r1);64     Circle cir2 = new Circle(r2);65     System.out.println("圆cir1的面积是" + cir1.area(r1) + "\t周长是:"66         + cir1.perimeter());67     System.out.println("圆cir2的面积是" + cir1.area(r2) + "\t周长是:"68         + cir2.perimeter());69     int h1, h2;70     System.out.println("请输入圆柱cy1的半径和高:");71     t = scan.readLine();72     r1 = Integer.parseInt(t);73     t = scan.readLine();74     h1 = Integer.parseInt(t);75     Cylinder cy1 = new Cylinder(r1, h1);76     System.out.println("cy1的底圆面积是:" + cy1.baseAera(r1));77     System.out.println("cy1的体积是:" + cy1.volume(r1) + "\t表面积是:"78         + cy1.area1(r1));79 80     System.out.println("请输入圆柱cy2的半径和高:");81     t = scan.readLine();82     r2 = Integer.parseInt(t);83     t = scan.readLine();84     h2 = Integer.parseInt(t);85     Cylinder cy2 = new Cylinder(r2, h2);86     System.out.println("cy2的底圆面积是:" + cy2.baseAera(r1));87     System.out.println("cy2的体积是:" + cy2.volume(r1) + "\t表面积是:"88         + cy2.area1(r2));89   }90 91 }