本帖最后由 黑马刘涛 于 2012-7-17 17:43 编辑
- public class TestCircle1 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- InnerCircle1 innerCircle1 = new InnerCircle1 ();
- System.out.println("The area of the circle of radius"+innerCircle1.radius+"is"+
- innerCircle1.getArea());
-
- InnerCircle1 innerCircle2 = new InnerCircle1 (25);
- System.out.println("The area of the circle of radius"+innerCircle2.radius+"is"+
- innerCircle2.getArea());
-
- InnerCircle1 innerCircle3 = new InnerCircle1 (125);
- System.out.println("The area of the circle of radius"+innerCircle3.radius+"is"+
- innerCircle3.getArea());
-
- innerCircle2.radius =100;
- System.out.println("The area of the circle of radius "+ innerCircle3.radius+"is"+
- innerCircle3.getArea());
- }
-
- static class InnerCircle1 //内部类,注意加static修饰符,因为main函数为静态
- {
- double radius;
-
- InnerCircle1(){
- radius = 1.0 ;
-
- }
- InnerCircle1(double newRadius){
- radius = newRadius;
- }
- double getArea () {
- return radius*radius*Math.PI;
- }
- }
-
- }
复制代码 //原先定义了一个Circle1类,在编译的时候会生成Circle1.class文件,毫无疑问现在要弄成内部类。
|
|