详述静态内部类与非静态内部类

1.按照是否有static修饰分为静态内部类和非静态内部类,有static修饰的为静态内部类,没有static修饰的为非静态内部类

2.静态内部类

  • 直接在类中定义的静态有名内部类
Body类:
package com.jd;

/**
 * 外部类
 */
public class Body {

	/**
	 * 静态有名内部类
	 */
	public static class Heart{

		public void beat(){
			System.out.println("正在跳动...");
		}
	}
}

Test类:
package com.jd;

import com.jd.Body.Heart;

public class Test {

	public static void main(String[] args) {
		Heart heart = new Body.Heart();
		heart.beat();
	}
}
static class Stomach{//静态内部类
		void work() {
			System.out.println("doing......");
		}
	}
	
	public void getStomach() {
		Stomach stomach = new Stomach();
		stomach = new Stomach();
	}
	
	public void getStomach(int a) {
		Stomach stomach = new Stomach();
		stomach = new Body.Stomach();
	}

由于静态的在类加载的时候处理:静态变量直接赋值  静态方法直接分配地址  静态代码块直接执行;非静态的只能在创建对象时执行操作:变量 ;JVM对静态的操作比非晶态的靠前
所以直接在类中定义的静态有名内部类,在静态和非静态方法中创建该类的对象时,都可以直接用

Stomach stomach = new Stomach();这是因为此时该静态类在类加载的时候已经存到了内存中,所以可以直接用该静态类的类名创建对象。

而此时在非静态方法中时,stomach = new Body.Stomach();是用Body类名直接调用该静态内部类,再创建对象

  • 直接在类中定义的静态匿名内部类
class Father {

	public void eat() {
		System.out.println("筷子吃饭....");
	}
}

/**
 * 外部类
 */
public class OutClass {

	/**
	 * 匿名内部类
	 */
	static Father son = new Father(){
		
		@Override
		public void eat() {
			System.out.println("筷子吃饭....");
		}
	};
}
  • 在静态内部类中需要注意一下几点:

1.如果为static内部类只能直接定义在外部类中。(只有成员变量才能用static修饰,局部变量不能咏static修饰,只能用static修饰)

public class OutClass {

	public static void main(String[] args) {
		
		/**
		 * 静态有名内部类
		 */
		static class InClass {

			public void printInfo() {
				System.out.println("我是有名内部类");
			}
		}
	}
}

2.静态内部类仅能直接访问外部类的静态成员变量和方法,可以通过创建外部类的对象间接使用非静态的成员变量和方法。

public class OutClass {
	
	private double weight=72;
	
	public static void print(String name) {
		System.out.println(name);
	}
	
	static class InClass{
		{
			double weight = new OutClass().weight;//由于weight是非静态的,所以在静态内部类中使用时必须先创建外部类对象
			print("Tom");//由于print方法为静态方法,所以可以直接使用。
		}
	}
}

3.在非外部类中定义的内部类和局部变量一样,其使用范围从定义的位置开始到其所在直接语句块结束。

public class OutClass {
	public static void main(String[] args) {
		if(args!=null) {
			class InClass{
				
			}
		}
		InClass inClass = new InClass();//无法创建对象,因为内部类作用范围无法作用到这里
	}
}

4.只有有名静态内部类中才允许有静态成员(静态属性、静态代码块和静态方法)。

3.非静态内部类

  • 直接在类中定义的非静态内部类,创建对象时必须先创建外部类对象
class Heart{
		static int age=12;
		static {
			
		}
		static void work() {
			System.out.println("doing......");
		}
	}
	
	public void getHeart() {
		System.out.println(this.age);
		Heart heart = new Heart();
		heart = this.new Heart();//

此时的Heart heart = new Heart();相当于heart = this.new Heart();因为是外部类在调用执行该方法,所以此时Java就自动地建立了外部类对象

  • 直接在类中定义的非静态内部类,在静态方法中创建对象,必须“显式”创建外部类对象
public static void getHeart(int a) {
		System.out.println(new Body().age);
		Heart heart = new Body().new Heart();
	}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值