java基础--内部类



一、内部类的访问规则

将一个类定义在另一个类的里面,对里面那个类就称为内部类

访问特点:

  • 内部类可以访问外部类中的成员,包括私有成员。可以直接访问的原因是因为内部类中持有一
          个外部类的引用,格式:外部类名.this
  • 外部类要访问内部类中的成员必须要建立内部类的对象

class Outer {
	int num = 1;
	
	//内部类
	class Inter {
		int num = 2;
		void function() {
			int num =3;
			System.out.println(" Inter num is " + num);	
			System.out.println(" function  num is " +this. num);			
			System.out.println(" Outer num is " +Outer.this. num);
		}
	}
	
	void method() {
		//外部类访问内部类的的成员的方式
		Inter inter = new Inter();
		inter.function();
	}
}
public class InterDemo {
	public static void main(String[] args) {
		Outer outer = new Outer();
		outer.method();
		System.out.println("-------------------------");		
		//直接访问内部类的成员
		Outer.Inter inter1 = new Outer().new Inter();
		inter1.function();
	}
}
二、静态内部类  

当内部类在成员位置上,就可以被成员修饰符所修饰,比如,private、static

当内部类被static修饰后,只能直接访问外部类中的static成员

在外部其它类中,如何直接访问static内部类的非静态成员呢?

new Outer.Inter().function();

在外部其它类中,如何直接访问static内部类的静态成员呢?

Outer.Inter.function();

注意:当内部类定义了静态成员,该内部类必须是静态的。外部类的静态方法

           访问内部类,该内部类也必须是静态的

内部类的定义原则,当描述事物时,事物里面还有事物,用内部类

class Outer {
    private static int num = 1;
	
	//内部类
	static  class Inter {
		void function() {
			System.out.println(" Outer num is " + num);
		}
		
		static void sfunction() {
			System.out.println(" Outer num is " + num);
		}		
	}
	
	static class Inter1 {
		void show() {
			System.out.println("Inter1");
		}
	}
	static void method() {
		new Inter1().show();;
	}
}
public class InterDemo {
	public static void main(String[] args) {
		//直接访问静态内部类的非静态方法
		new Outer.Inter().function();
		//直接访问静态内部类的静态方法
		Outer.Inter.sfunction();
	}
}

内部类即可以定义在成员位置上,也可以定义在局部上。定义在局部上的类不能被

成员修饰符(private、static)修饰,可以直接访问外部类成员,若要访问局部变量

只能访问被final修饰的局部变量

class Outer1{
	
	int num = 1;
	
	void method(final int a) {

		final int num2 = 2;
		//a++;不能运算
		//局部内部类
		class Inter1 {
			void function()  {
				System.out.println("Outer1 num is " + num);
				System.out.println("method num2 is " + num2);
				System.out.println("Outer1 num is " + a);				
			}
		}
		new  Inter1().function();
	}
}
public class InterDemo1 {
	public static void main(String[] args) {
		Outer1 outer1 = new Outer1();
		outer1.method(7);
		//可以赋值
		outer1.method(8);		
	}
}

三、匿名内部类 

匿名内部类:

1、匿名内部类其实是内部类的简写格式

2、定义匿名内部类的前提:内部类必须是继承一个或者实现接口。

3、匿名内部类的格式:new 父类或者接口() { 定义子类的内容}

4、其实匿名内部类就是一个匿名子类对象

5、匿名内部类的方法最好不超过三个,因为会导致阅读性太差

abstract class absDemo {
	abstract void show(); 
}

class Outer2 {
	int num = 1;
//	
//	class Inter2 extends absDemo {
//		void show() {
//			System.out.println("Outer2 num is " + num);
//		}
//	}
	void method() {
//		new Inter2().show();
		//匿名内部类,等效屏蔽掉的那部分内部类定义,内部类对象的创建
		new absDemo() {
			void show() { 
				System.out.println("Outer2 num is " + num);
			}
		}.show();
	}
}
public class InterDemo2 {
	public static void main(String[] args) {
		new Outer2().method();
	}
}
一个小练习:

interface Inter {
	abstract void method();
}
class Test {
	//补足代码。通过匿名内部类
}
public class InterDome3 {
	public static void main(String[] args) {
		Test.function().method();
	}
}
小练习解答:

interface Inter {
	public abstract void method();
}
class Test {
	//补足代码。通过匿名内部类
	
	/* static class Inner implements Inter {
		public void method() {
			System.out.println("method run");
		}
	}*/
	
	static Inter function() {
		return new Inter() {
			public void method() {
				System.out.println("method run");
			}
		};
	}
}
public class InterDome3 {
	public static void main(String[] args) {
		//Test.function()说明function为static的
		//.method()说明function返回类型为Inter类型对象
		Test.function().method();
	}
}












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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值