标题: 数据域封装的问题 [打印本页] 作者: 二月的雨 时间: 2014-12-8 16:39 标题: 数据域封装的问题 有下面一个很基本的代码,提问是:为什么第一,二次的显示结果是一样的呢?
源代码如下:
public class TestCircle3
{
public static void main(String[] args)
{
Circle3 myCircle = new Circle3(5.0);
System.out.println("The area of the radius "+myCircle.getRadius()+" is "+myCircle.getArea());
myCircle.setRadius(myCircle.getRadius() * 1.1);
System.out.println("The area of the radius "+myCircle.getRadius()+" is "+myCircle.getArea());
System.out.println("The number of objects creased is "+Circle3.getNumberOfObjects());
}
}
class Circle3
{
private double radius = 1.0;
private static int numberOfObjects;
public Circle3()
{
numberOfObjects++;
}
public Circle3(double newRadius)
{
radius = newRadius;
numberOfObjects++;
}