Java之内部类


一、内部类的访问规则:

1,内部类可以直接访问外部类中的成员,包括私有。之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用,格式 : 外部类名.this;

2,外部类要访问内部类,必须建立内部类对象;

3,当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中直接建立内部类对象。

格式:外部类名.内部类名  变量名 = 外部类对象.内部类对象;                   

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

public class InnerDemo
{

	public static void main(String[] args)
	{
//		Outer out=new Outer();
//		out.method();
//		在外部类之外直接访问内部类
		Outer.Inner in=new Outer().new Inner();
		in.fun();
//		System.out.println(in.x); 编译错误 x是private私有的,只能在本类中访问

	}

}

class Outer //外部类
{
	private int x=3;//外部类私有成员变量
	
//	private 
	class Inner//内部类,作为外部类的成员存在,可以用private修饰
	{
		private int x =4;
		void fun()
		{
			System.out.println(Outer.this.x);//输出 3
			System.out.println(this.x);//输出 4
			show();//访问外部类的show方法
		}
	}
	
	void method()
	{   
//		外部类要访问内部类的成员和函数要创建内部类的实例对象
		Inner in=new Inner();//创建内部类的实例
		System.out.println(in.x);
		in.fun();
	}
	
	private void show()
	{
		System.out.println("外部类show 方法!!!");
	}
	
}

二、静态内部类

当内部类在成员位置上,就可以被成员修饰符所修饰。比如,private:将内部类在外部类中进行封装。

static:内部类就具备static的特性。当内部类被static修饰后,只能直接访问外部类中的static成员,出现了访问局限。

public class InnerDemo01
{


	public static void main(String[] args)
	{
		Outer.Inner in=new Outer.Inner();
		in.fun();//访问内部类非静态方法 格式:new 外部类名.内部类名().(变量名或方法名);
		
		Outer.Inner2.show();//访问内部类中的静态方法 格式:外部类名.内部类名.(变量名或方法名);
		

	}

}

class Outer
{
	private static int x=3;
	static class Inner//静态内部类
	{
		void fun()
		{
			System.out.println("inner fun:"+x);//静态内部类只能访问外部类中静态的,不能访问非静态
		}
	}
	
	static class Inner2//定义另外一个静态内部类
	{
		static void show()
		{
			System.out.println("inner2 show");
		}
	}
}



三、局部内部类

内部类定义在局部时:

1.不能被成员修饰符修饰

2.可以直接访问外部类的成员,因为还持有外部类的引用,但是不可以访问它所在的局部中的变量。只能访问被final修饰的局部变量。

public class InnerDemo02
{



	public static void main(String[] args)
	{
		new Outer().method(6);

	}

}


class Outer
{
	private int x=3;//外部类成员变量
	void method(final int a)//final局部变量a
	{
		final int y=4;//final局部变量y
		class Inner//定义一个局部内部类
		{
			void fun()
			{
				System.out.println(x);
				System.out.println(y);
				System.out.println(a);
			}
		}
		
		new Inner().fun();//创建内部类实例访问内部类方法
	}
}

四、匿名内部类

1,匿名内部类其实就是内部类的简写格式。
2,定义匿名内部类的前提:内部类必须是继承一个类或者实现接口。
3,匿名内部类的格式: new 父类或者接口(){定义子类的内容}
4,其实匿名内部类就是一个匿名子类对象。而且这个对象有点胖,可以理解为带内容的对象。
5,匿名内部类中定义的方法最好不要超过3个。

public class InnerDemo03
{

	public static void main(String[] args)
	{
         new Outer().method();
	}

}

abstract class Person
{
	abstract void study();
}

class Outer
{
	private int num=3;
	class Student extends Person//内部类
	{  
		
		void study()//重写父类study方法
		{
			System.out.println("学习Java");
		}
		
		void play()//子类新增方法
		{   
			System.out.println(num);
			System.out.println("打篮球");
		}
	}
	
	public void method()
	{
		Student s=new Student();
		s.study();
		s.play();
	}
}

public class InnerDemo04
{

	public static void main(String[] args)
	{
         new Outer().method();
	}

}

abstract class Person
{
	abstract void study();
}

class Outer
{
	private int num=3;
    
	public void method()
	{
		
		/*
		Person p=new Person()
		{
			void study()
			{
				System.out.println("学习java");
			}
			void play()
			{
				System.out.println(num);
				System.out.println("打篮球");
			}
		};
		p.study();
		//p.play(); 编译失败	 
		 */
		
		//匿名内部类
		new Person()
		{
			void study()
			{
				System.out.println("学习java");
			}
		}.study();
	}

}

内部类练习

public class InnerTest
{

	public static void main(String[] args)
	{
		Outer.method().show();//根据此句补全代码
//        注意下面这种书写格式
		new Object()
		{
			void show()
			{
				System.out.println("show run....");
			}
		}.show();
	}

}

interface Inter
{
	void show();
}

class Outer
{
	static int num=99;
	//method方法为补全代码
    public static Inter method()
    {
    	Inter in=new Inter()
    	{
    		public void show()
    		{
    			System.out.println("num="+num);
    		}
    	};
    	return in;
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值