public class Circle { //先确定一个圆周率的值 final double PI=3.14; //圆心 private double point; //半径 private double r; //返回面积 public double area() { return PI*r*r; } //get圆心方法 public double getPoint() { return point; } //set圆心方法 public void setPoint(double point) { this.point = point; } //get半径方法 public double getR() { return r; } //set半径方法 public void setR(double r) { this.r = r; } //圆的无参构造器 public Circle(){} //圆的有参构造器 public Circle(double point,double r) { this.point = point; this.r = r; } public static void main(String[] args) { //第一种方式,首先实例化一个对象,通过构造器为属性赋值 Circle circle = new Circle(5.1,12); //输出圆的面积及属性 System.out.println("第一种方式圆的面积为:"+circle.area()); System.out.println("圆心为:"+circle.getPoint()+" 半径为:"+circle.getR()); //第二种方式,通过调用方法,为属性赋值,并且输出圆的面积及属性值 circle.setPoint(3); circle.setR(16); System.out.println("第二种方式圆的面积为:" + circle.area()); System.out.println("圆心为:"+circle.getPoint()+" 半径为:"+circle.getR()); } }