日期:2014-05-20  浏览次数:20632 次

怎样取出这个程序C的值?
public class Test2
{
public static void main(String[] args)
{

A x = new A();
System.out.println(x.a);
    A.B y = x.new B();
  System.out.println(y.b);
A.B m = new A().new B();
System.out.println(m.b);
System.out.println(m.c); //这一步有误,怎样改?

}
}
 class  A
{
int a = 5;
public class B
{
int b = 6;

public B()
{
int c = 10;
}

}

}


错误提示为
E:\java练习程序\practice\Test2>javac Test2.java
Test2.java:12: 错误: 找不到符号
                                System.out.println(m.c);        //这一步有误,怎样改?
                                                    ^
  符号:   变量 c
  位置: 类型为A.B的变量 m
1 个错误

而且如果把类B加上修饰static,变成

public class Test2
{
public static void main(String[] args)
{

A x = new A();
System.out.println(x.a);
    A.B y = x.new B();
  System.out.println(y.b);
A.B m = new A().new B();
System.out.println(m.b);
//System.out.println(m.c); //这一步有误,怎样改?

}
}
 class  A
{
int a = 5;
public static class B
{
int b = 6;

public B()
{
int c = 10;
}

}

}


有错误提示



加上static后,怎样取出b与c值呢


------解决方案--------------------
public B()
                {
                        int c = 10;//这是临时变量,那边获取不到的   
                }
------解决方案--------------------
class  A
{
        int a = 5;
        public class B
        {
                int b = 6;
                int c = 0;   
                 
                public B()
                {
                        this.c = 10;   
                }
                 
        }
     
}
------解决方案--------------------
引用:
class  A
{
        int a = 5;
        public class B
        {
                int b = 6;
                int c = 0;