一.
1.内部类的概念:
在Java中,类中除了可以定义属性和方法外,还可以定义类,该类称为内部类,内部类所在的类称为外部类
2.内部类的特性:
1)内部类与外部类经过编译后是两个独立的类
2)内部类是外部类的一个成员,因此内部类能够访问外部类的任何成员(包括私有),但是外部类不能直接访问内部类的成员
3)内部类可分为静态的,可以用protected和private修饰,而外部类只能有public或默认的访问修饰
3.使用场合不是必须的,是对类中的成员进一步的细分
二.
根据内部类的位置,修饰符,和定义方式不同,内部类分为如下4种:
1.成员内部类
/**
* @author CC
* 成员内部类
*/
public class Other {
//成员变量
private String name="Other";
private int count;
public void otherSay(){
System.out.println("外部类的输出方法");
//尝试访问内部类成员
//不能直接访问
//可以创建内部类对象,通过内部类对象来访问内部类的成员
Inner i=new Inner();
i.say();
}
//定义成员内部类
class Inner{
//声明类的成员
private String name="Other.Inner";
public void say(){
//输出外部类中的name值
//类名.this 表示外部类对象
System.out.println(Other.this.name);
//输出当前类(内部类)
System.out.println(this.name);
Other.this.count=10;
System.out.println("count="+Other.this.count);
Other.this.otherSay();
}
}
}
成员内部类的测试类:
public class TestOther {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建Other类对象
Other o=new Other();
//o.otherSay();
//创建内部类对象
Other.Inner inner=new Other().new Inner();
//Other.Inner inner2=o.new Inner();
inner.say();
}
}
2.静态内部类
/**
* @author CC
* 静态内部类
*/
public class Other2 {
//外部类成员
private static String name="Other";
private static int count;
private String str;
//访问内部类成员
public void test(){
System.out.println(Inner.name);
Inner i=new Inner();
System.out.println(i.str2);
}
//定义静态内部类
static class Inner{
//声明内部类成员
public static String name="Other.Inner";
public String str2="";
public void say(){
System.out.println("Other2.name="+Other2.name+",Other2.count="+Other2.count);
//静态内部类不允许直接访问外部类的实例成员
//System.out.println(Other2.this.str);
Other2 o2=new Other2();
System.out.println(o2.str);
}
}
}
静态内部类的测试类:
public class TestOther2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//直接访问内部类的静态成员
System.out.println(Other2.Inner.name);
//创建内部类对象:内部类是静态的
Other2.Inner obj=new Other2.Inner();
obj.str2="";
obj.say();
}
}
3.方法内部类:
在成员方法中定义的类,它与局部变量类似,作用域为定义它的代码块,因此它只能在定义该内部类的方法体内进行new
public class Other3 {
private static String name="Other3";
private int count=10;
public void say(){
String name="say";
//声明方法内部类
class Inner{
//定义成员
public String name="Other.Inner";
public void say(){
System.out.println("方法内部类的say");
System.out.println("Inner.name="+name);
System.out.println(Other3.name);
System.out.println("count="+count);
}
}
//Inner在say方法内部声明,一定要在方法内部new
Inner obj=new Inner();
obj.say();
}
}
方法内部类的测试类:
public class TestOther3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Other3 obj=new Other3();
obj.say();
}
}
4.匿名内部类
没有名称的内部类,声明匿名内部类时就会创建一个内部类对象,声明内部类结束后,对象也随即消失,匿名内部类不能重复使用
public class Other4 {
private String name="Other4";
public void other4Say(){
System.out.println(this.name);
System.out.println("-------------------------------");
}
public void say(){
System.out.println(this.name);
}
}
匿名内部类的测试类:
public class TestOther4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建Other4对象
Other4 o4=new Other4(){
//声明匿名类的成员
private String innerName="noName";
public void say(){
System.out.println(innerName);
}
};
o4.other4Say();
o4.say();
}
}