关于内部类
public class OuterClass {
private double d1 = 1.0;
//insert code here
}
You need to insert an inner class declaration at line 3,Which two inner
class declarations are valid?(Choose two.)
A. class InnerOne{
public static double methoda() {return d1;}
}
B. public class InnerOne{
static double methoda() {return d1;}
}
C. private class InnerOne{
double methoda() {return d1;}
}
D. static class InnerOne{
protected double methoda() {return d1;}
}
E. abstract class InnerOne{
public abstract double methoda();
}
答:答案为 C、E;说明如下:
- 1)静态内部类可以有静态成员,而非静态内部类则不能有静态成员;故 A、B 错;
- 2)静态内部类的非静态成员可以访问外部类的静态变量,而不可访问外部类的非静态变量;故 D 错;
- 3)非静态内部类的非静态成员可以访问外部类的非静态变量;故 C 正确 。