javase基础篇学习之成员内部类

内部类分为:
非静态内部类

package 内部类;
//脸类
public class 非静态Face {
	//外部类成员
	private String shape = "国字脸";
	//
	private void run() {
		System.out.println("脸上外部类方法");
	}
//鼻子内部类
	class Nose{
		private void run() {
			
			System.out.println("鼻子内部方法");
		}
		public void breath() {
			System.out.println("呼吸呼吸");
			//属于某个外部类的对象
			System.out.println("鼻子长脸上了"+非静态Face.this.shape+"形状的脸上");
			//两个方法相同会默认访问内部类的方法
			run();
			//重名时 :外部类名+this+加外部方法 Face.this.run();
		}
	}
	//耳朵内部类
	class Ear{
		public void tingHua() {
			System.out.println("耳朵听话");
			//访问外部脸类成员
			System.out.println("耳朵长再脸上"+非静态Face.this.shape+"形状的脸上");
			
		}
		
	}
	public static void main(String[] args) {
		//创建鼻子类对象并且访问自己成员方法和外部类的成员
		
		非静态Face.Nose  nose = new 非静态Face().new Nose();
		nose.breath();
		非静态Face.Ear  ear = new 非静态Face().new Ear();
		ear.tingHua();
	}
}

静态内部类:
静态内部类中不能访问外部类的普通属性也就是只能访问静态成员
匿名内部类

package 内部类;

public class 匿名Test {
	public void test(A a) {
		a.run();

	}
		 

	public static void main(String[] args) {
		匿名Test test1 = new 匿名Test();
		/* test1.test(new AA()); */
		test1.test(new A(){
				public void run() {
					System.out.println("匿名内部类");
				}
			
		});
		
	}

}
/*
 * //有名字内部类 class AA implements A { public void run() {
 * System.out.println("有名内部类");
 * 
 * } }
 */

interface A {
	void run();

}

局部内部类

在这里插入图片描述

在这里插入图片描述
循序渐进

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值