JAVA学習:内部クラス

3687 ワード


一、内部クラスのアクセス規則:
 
1.内部クラスは、プライベートを含む外部クラスのメンバーに直接アクセスできます.形式は外部クラス名です.this
2、外部クラス内部クラスにアクセスするには、内部クラスオブジェクトを作成する必要があります.
 
コード:
 
class Outer

{

	private int x = 3;



	

	class Inner// 

	{

		//int x = 4;

		void function()

		{

			//int x = 6;

			System.out.println("innner :"+Outer.this.x);

		}

	}



	/**/

	void method()

	{

		Inner in = new Inner();

		in.function();

	}

}





class  InnerClassDemo

{

	public static void main(String[] args) 

	{

		Outer out = new Outer();

		out.method();



		 。

		Outer.Inner in = new Outer().new Inner();

		in.function();

	}

}


  
 
  
二、内部クラスアクセスフォーマット:
 
1、内部クラスは外部クラスのメンバーの位置に定義され、プライベートではなく、外部の他のクラスで内部クラスオブジェクトを作成できます.
2、フォーマット:外部クラス名.内部クラス名変数名=外部クラスオブジェクト.内部クラスオブジェクト;
3、フォーマット:Outer.Inner in = new Outer().new Inner();
4、内部クラスがメンバーの位置にある場合、メンバー修飾子によって修飾することができる.例えばprivate
5、内部クラスがstaticによって修飾されると、外部クラスのstaticメンバーに直接アクセスするしかありません.
 
注意:
 
1、外部の他のクラスでstatic内部クラスの非静的メンバーに直接アクセスするにはどうすればいいですか?
new Outer.Inner().function();
2、外部の他のクラスでstatic内部クラスの静的メンバーに直接アクセスするにはどうすればいいですか?
outer.Inner.function();
3、外部クラスの静的メソッドが内部クラスにアクセスする場合、内部クラスはstaticである必要があります.
 
コード:
 
 
class Outer

{

	private static  int x = 3;



	

	static class Inner// 

	{

		static void function()

		{

			System.out.println("innner :"+x);

		}

	}



	static class Inner2

	{

		void show()

		{

			System.out.println("inner2 show");

		}

	}



	public static void method()

	{

		// Inner.function();

		new Inner2().show();

	}



}





class  InnerClassDemo2

{

	public static void main(String[] args) 

	{

		Outer.method();

		Outer.Inner.function();

		new Outer.Inner().function();

		 。

		Outer.Inner in = new Outer().new Inner();

		in.function();

	}

}


 
  
三、内部クラスは局部で定義する:
 
1、メンバー修飾子で修飾してはいけない
2.外部クラスの参照があるため、外部クラスのメンバーに直接アクセスできます.
3、ただし、そのローカル内の変数にはアクセスできません.finalで修飾されたローカル変数にのみアクセスできます.
 
コード:
 
class Outer

{

	int x = 3;



	void method(final int a)

	{

		final int y = 4;

		class Inner

		{

			void function()

			{

				System.out.println(y);

			}

		}

		new Inner().function();

		

	}

}





class  InnerClassDemo3

{

	public static void main(String[] args) 

	{

		Outer out = new Outer();

		out.method(7);

		out.method(8);

	}

}


 
 
四、匿名の内部クラス(実は内部クラスの略写フォーマット):
 
1、内部クラスはクラスを継承するか、インタフェースを実装する必要があります.
2、匿名内部クラスのフォーマット:new親クラスまたはインタフェース(){子クラスの内容を定義}
3、実は匿名の内部クラスは匿名のサブクラスのオブジェクトです.コンテンツ付きのオブジェクトとして理解できます.
4、匿名内部クラスで定義されている方法は3つを超えないことが望ましい.
 
コード:
 
 
 abstract class AbsDemo

{

	abstract void show();

	

}





class Outer

{

	int x = 3;



	class Inner extends AbsDemo

	{

		int num = 90;

		void show()

		{

			System.out.println("show :"+num);

		}

		void abc()

		{

			System.out.println("hehe");

		}

	}



	public void function()

	{

		AbsDemo a = new Inner();

		Inner in = new Inner();

		in.show();

		in.abc();

	



		AbsDemo d = new AbsDemo()

		{

			int num = 9;

			void show()

			{

				System.out.println("num"+num);

			}

			void abc()

			{

				System.out.println("haha");

			}

		};



		d.show();

	}

}



class InnerClassDemo4 

{

	public static void main(String[] args) 

	{

		new Outer().function();

	}

}