Java Inner Class

本文深入探讨Java内部类的各种特性,包括其访问权限、与外部类的交互方式、使用static声明的影响,以及如何在方法和匿名上下文中定义内部类。
(1)内部类可声明成public或private。当内部类声明成public或private时,对其访问的限制与成员变量和成员方法完全相同。
class Outer
{
    int score = 95;
    
    void inst()
    {
        Inner in = new Inner();
        in.display();
    }

    class Inner
    {
        void display()
        {
            System.out.println("成绩: score = " + score);
        }
    }
}

public class InnerClassDemo
{
    public static void main(String[] args)
    {
        Outer outer = new Outer();
        outer.inst();
    }
}
输出结果:
成绩: score = 95 
(2)外部类声明的属性可以被内部类所访问,那内部类声明的属性可以被外部类所访问么?
class Outer 
{ 
    int score = 95;
    
    void inst() 
    { 
        Inner in = new Inner(); 
        in.display(); 
    } 

    public class Inner
    {
        void display()
        {
            //在内部类中声明一name属性
            String name = "zhangsan";
            System.out.println("成绩: score = " + score);
        }
    }

    public void print()
    {
        //在此调用内部类的name属性
        System.out.println("name: " + name);
    }
} 

public class InnerClassDemo1 
{ 
    public static void main(String[] args) 
    { 
        Outer outer = new Outer(); 
        outer.inst(); 
    } 
}
编译结果:
InnerClassDemo1.java:21: cannot resolve symbol 
symbol   : variable name 
location: class Outer 
System.out.println("姓名:"+name) ; 
                            ^ 
1 error 
从程序编译结果中可以发现,外部类是无法找到内部类中所声明的属性。而内部类则可以访问外部类的属性。

(3)用static可以声明内部类,用static声明的内部类则变成外部类,但是用static声明的内部类不能访问非static的外部类属性。
class Outer 
{ 
    int score = 95;
    
    void inst() 
    { 
        Inner in = new Inner(); 
        in.display(); 
    } 
    
    // 这里用static声明内部类
    static class Inner
    {
        void display()
        {
            System.out.println("成绩: score = " + score);
        }
    }
} 

public class InnerClassDemo2 
{ 
    public static void main(String[] args) 
    { 
        Outer outer = new Outer(); 
        outer.inst(); 
    } 
}
编译结果:
InnerClassDemo2.java:14: non-static variable score cannot be referenced from a static
context 
System.out.println("成绩: score = " + score); 
                                          ^ 
1 error
    由编译结果可以发现,由于内部类Inner声明为static类型,所以无法访问外部类中的非static类型属性score。

(4)
class Outer 
{ 
    int score = 95;
    
    void inst() 
    { 
        Inner in = new Inner(); 
        in.display(); 
    } 

    public class Inner
    {
        void display()
        {
            System.out.println("成绩: score = " + score);
        }
    }
} 

public class InnerClassDemo3 
{ 
    public static void main(String[] args) 
    { 
        Outer outer = new Outer(); 
        Outer.Inner inner = outer.Inner(); 
        inner.display();
    } 
}
输出结果:
成绩: score = 95 
程序说明:
1、  程序9~15行用public声明一内部类,此内部类可被外部类访问。
2、  程序第22行用外部类的对象去实例化一内部类的对象。

(5)内部类不仅可以在类中定义,也可以在方法中定义内部类。
class Outer 
{ 
    int score = 95;
    
    void inst() 
    {
        class Inner
        {
            void display()
            {
                System.out.println("成绩: score = " + score);
            }
        }

        Inner in = new Inner(); 
        in.display(); 
    } 


} 

public class InnerClassDemo4 
{ 
    public static void main(String[] args) 
    { 
        Outer outer = new Outer(); 
        outer.inst();
    } 
}
输出结果:
成绩: score = 95

(6)
    在方法中定义的内部类只能访问方法中的final类型的局部变量,因为用final定义的局部变量相当于是一个常量,它的生命周期超出方法运行的生命周期。
class Outer 
{ 
    int score = 95;
    
    void inst(final int s) 
    {
        int temp =50;
        class Inner
        {
            void display()
            {
                System.out.println("成绩: score = " + (score + " " + s + " " + temp));
            }
        }

        Inner in = new Inner(); 
        in.display(); 
    } 


} 

public class InnerClassDemo5 
{ 
    public static void main(String[] args) 
    { 
        Outer outer = new Outer(); 
        outer.inst();
    } 
}
编译结果:
InnerClassDemo5.java:11: local variable tempis accessed from within inner class; 
needs to be declared final 
System.out.println("成绩: score = " + "成绩: score = " + (score + " " + s + " " + temp)));
                                                                                    ^
1 error 

由编译结果可以发现,内部类可以访问用final标记的变量s,却无法访问在方法内声明的变量temp,所以只要将第6行的变量声明时,加上一个final修饰即可:
final int temp = 20 ;

(7)匿名内部类
范例:TestNonameInner1.java 
interface A 
{ 
    public void fun1() ; 
} 

class B 
{ 
    int i = 10 ; 
    class C implements A 
    { 
        public void fun1() 
        { 
            System.out.println(i) ; 
        } 
    } 

    public void get(A a) 
    { 
        a.fun1() ; 
    } 

    public void test() 
    { 
        this.get(new C()) ; 
    } 
} 

class TestNonameInner1 
{ 
    public static void main(String[] args)  
    { 
        B b = new B() ; 
        b.test() ; 
    } 
} 
输出结果:
10 
引出匿名内部类
范例:TestNonameInner2.java 
interface A 
{ 
    public void fun1() ; 
} 

class B 
{ 
    int i = 10 ; 

    public void get(A a) 
    {
    a.fun1() ; 
    } 

    public void test() 
    { 
        this.get(new A() 
        { 
            public void fun1() 
            { 
                System.out.println(i) ; 
            } 
        } ); 
    } 
} 

class TestNonameInner2 
{ 
    public static void main(String[] args)  
    { 
        B b = new B() ; 
        b.test() ; 
    } 
} 
输出结果:
10 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值