首页 > 代码库 > 面向对象3
面向对象3
class Point {
private double x;//成员变量
private double y;//成员变量
//构造方法
Point (double _x,double _y){
x = _x;
y = _y;
}
//方法
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setX(double i) {
x = i;
}
public void setY(double i) {
y = i;
}
}
class Circle {
private Point o;//Point类的复用
private double redius;//成员变量
//够着方法
Circle (Point p,double r){
o = p;
redius =r;
}
//重载
Circle (double r){
o = new Point(0.0, 0.0);
redius = r;
}
boolean contains(Point p){
double x = p.getX() - o.getX();
double y = p.getY() - o.getY();
if ((x*x + y*y) > redius*redius)
return false;
else
return true;
}
public void setO(double x,double y){
o.setX(x);
o.setY(y);
}
public Point getO(){
return o;
}
public double getRedius(){
return redius;
}
public void setRedius(double r){
redius = r;
}
public double area(){
return 3.14 * redius * redius;
}
}
public class TestCircle{
public static void main (String[] args){
Circle c1 = new Circle(new Point(1.0, 2.0), 2.0);
Circle c2 = new Circle(3.0);
System.out.println("c1:("+c1.getO().getX() + "," + c1.getO().getY()
+ ") " + "redius = " + c1.getRedius());
System.out.println("c2:("+c2.getO().getX() + "," + c2.getO().getY()
+ ") " + "redius = " + c2.getRedius());
System.out.println("c1:area= " + c1.area() );
System.out.println("c2:area= " + c2.area());
c1.setO(5.0,6.0);
c2.setRedius(6.0);
System.out.println("c1:("+c1.getO().getX() + "," + c1.getO().getY()
+ ") " + "redius = " + c1.getRedius());
System.out.println("c2:("+c2.getO().getX() + "," + c2.getO().getY()
+ ") " + "redius = " + c2.getRedius());
System.out.println("c1:area= " + c1.area() );
System.out.println("c2:area= " + c2.area());
Point p1 = new Point(5.2, 6.3);
System.out.println(c1.contains(p1));
System.out.println(c1.contains(new Point(10.0, 9.0)));
}
}
面向对象3