Java-接口练习1

匿名 (未验证) 提交于 2019-12-02 21:52:03

1.(1)编写一个接口ShapePara,要求: 接口中的方法: int getArea():获得图形的面积。int getCircumference():获得图形的周长

(2)编写一个圆类Circle,要求:圆类Circle实现接口ShapePara。

该类包含有成员变量:

x:private修饰的double型变量x,表示圆心的横坐标。

y:protected修饰的double型变量y,表示圆心的纵坐标。

包含的方法有:

接口:

 1 package com.lianxi;  2   3 public interface ShapePara {  4     5     //常量  6     double PI=3.14;  7       8     //抽象方法  9     double getArea(); 10      11     double getCircumference(); 12 }     13   
 1 package com.lianxi;  2   3 public class Circle implements ShapePara {  4       5     //属性  6     private double x;  7     private double y;  8     private double radius;  9  10     public double getX() { 11         return x; 12          13     } 14  15     public void setchenter(double x,double y) { 16         this.x = x; 17         this.y = y; 18     } 19  20     public double getY() { 21         return y; 22     } 23  24     public double getRadius() { 25         return radius; 26     } 27  28     public void setRadius(double radius) { 29         this.radius = radius; 30     } 31      32     //构造方法 33     public Circle( double radius) { 34         super(); 35         this.x = 0; 36         this.y = 0; 37         this.radius = radius; 38     } 39  40     @Override 41     public double getArea() { 42          43         return PI*Math.pow(radius, 2); 44     } 45  46      47  48     @Override 49     public double getCircumference() { 50          51         return 2*PI*radius; 52     } 53  54 }
 1 package com.lianxi;  2   3 public class Test {  4   5     public static void main(String[] args) {  6         Circle c=new Circle(10);  7         c.setchenter(3, 4);  8         System.out.println(c.getArea());  9          10  11     } 12  13     public void han(ShapePara s) 14     { 15         s.getArea(); 16     } 17 }

结果:

314.0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!