java(13)内部类

内部类就是定义在类内部的类,可以使java具备为普通类实现多重继承的能力。

非静态内部类,局部内部类,静态内部类,匿名内部类

非静态内部类:

 

package 内部类;


class Wai5
{
    int i = 8;
    class Nei
    {
        int i = 9;
        public void myVoid()
        {
            System.out.println("内部类的成员变量值为:"+this.i);
            System.out.println("外部类的成员变量值为:"+Wai5.this.i);
        }
    }
}

public class NeiBuLei5 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Wai5 w = new Wai5();
        Wai5.Nei wn2 = w.new Nei();
        wn2.myVoid();
    }

}

局部内部类:

package 内部类;

class WaiBu
{
	public void myVoid()
	{
		final int i=9;
		class Nei
		{
			public void myNeiVoid()
			{
				System.out.println("外部类的局部变量值:"+i);
			}
		}
		Nei n = new Nei();
		n.myNeiVoid();
	}
}

public class JuBuLei {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WaiBu w = new WaiBu();
		w.myVoid();
	}

}

  静态内部类:

package 内部类;
class WaiBu1
{
	static class NeiBU
	{
		int i = 1;
	}
	
	public void myVoid()
	{
		NeiBU n = new NeiBU();
		int ii = n.i;
		System.out.println("静态内部类的变量值为:" +ii);
	}
}
public class 静态局部类 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WaiBu1 w = new WaiBu1();
		w.myVoid();
	}

}

  匿名内部类:

package 内部类;

class NeiFather
{
    int i;
    public void myVoid()
    {
        System.out.println("这是内部类的父类方法");
    }
}

public class NiMing {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        NeiFather nf = new NeiFather()
        {
            {
                i=9;
            }
            
            public void myVoid()
            {
                System.out.println("匿名内部类成员变量的值为: "+i);
                
            }
        };
        nf.myVoid();
        }

}

 

posted on 2016-05-08 21:31  言满天下  阅读(228)  评论(0编辑  收藏  举报

导航