Exercise 1 Interface Java-

1. (1) preparation of an interface ShapePara, requires: interface methods: int getArea (): get the area of ​​the graph. int getCircumference (): get the perimeter pattern

(2) preparation of a circle class Circle, requirements: round Circle class implements the interface ShapePara.

This class includes member variables:

radius: public modified type double radius, represented by the circle radius.

x: private modified double variable x, the horizontal axis represents the center of the circle.

y: protected modified double variable y, the ordinate represents the center of the circle.

The method includes are:

Circle (double radius) with a constructor parameter. In radius parameter initialization parameter table, the center coordinate origin. double getRadius (): Get the return value of a radius of the method. void setCenter (double x, double y): center coordinates using the parameter set Circle class parameter table. void setRadius (double radius): using the parameter list of the Circle class parameter field radius.

interface:

. 1  Package com.lianxi;
 2  
. 3  public  interface ShapePara {
 . 4    
. 5      // constants 
. 6      Double the PI = 3.14 ;
 . 7      
. 8      // abstract method 
. 9      Double the getArea ();
 10      
. 11      Double the getCircumference ();
 12 is  }    
 13 is   
 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 }

result:

314.0

 

Guess you like

Origin www.cnblogs.com/datacenter/p/11447140.html