1. 使用面向对象的思想设计一个圆类,实现类的封装。要求属性有圆心和半径,有获得面积和周长的方法。圆心需要设计为Point(点)类,该类具有x,y(表示点的横、纵坐标)两个属性,并定义两个构造方法,一个无参数,另一个以坐标值为参数,设置x,y为给定坐标值。Point类的show方法输出该点的坐标值。
这道题相对比较简单,不多做解释了
package com.itcast.exercise;
public class CircleTest {
public static void main(String[] args) {
Point p = new Point(4,4);
Circle c = new Circle(4,p);
p.show();
c.getArea();
c.getL();
}
}
package com.itcast.exercise;
public class Circle {
private double r;
private Point p;
private double area;
private double perimeter;
public Circle(double r, Point p) {
this.r = r;
this.p = p;
}
public double getArea() {
area = Math.PI*r*r;
System.out.println("面积 = "+area);
return area;
}
public double getL() {
perimeter = 2*Math.PI*r;
System.out.println("周长 = "+perimeter);
return perimeter;
}
}
package com.itcast.exercise;
public class Point {
private double x;
private double y;
public Point() {
this.x = 0.0;
this.y = 0.0;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public void show() {
System.out.println("x = "+x+", y = "+y);
}
}