现在的位置: 首页 > 综合 > 正文

Java内部类

2017年11月09日 ⁄ 综合 ⁄ 共 3040字 ⁄ 字号 评论关闭

内部类的基本机构

public class Out {
	private int age = 12;

	class In {
		public void print() {
			System.out.println(age);
		}
	}
}
	public static void main(String[] args) {
		Out.In in = new Out().new In();
		in.print();
		/************************/
		Out out = new Out();
		Out.In in2 = out.new In();
		in2.print();
	}

内部类其实严重破坏了良好的代码结构,但为什么还要使用内部类呢?

因为内部类可以随意使用外部类的成员变量(包括私有)而不用生成外部类的对象,这也是内部类的唯一优点。如同心脏可以直接访问身体的血液,而不是通过医生来抽血

程序编译过后会产生两个.class文件,分别是Out.class和Out$In.class

其中$代表了上面程序中Out.In中的那个 .

Out.In in = new Out().new In()可以用来生成内部类的对象,这种方法存在两个小知识点需要注意

  1.开头的Out是为了标明需要生成的内部类对象在哪个外部类当中

  2.必须先有外部类的对象才能生成内部类的对象,因为内部类的作用就是为了访问外部类中的成员变量


内部类中的变量访问形式

public class Out {
	private int age = 12;
	
	class In{
		private int age = 13;
		public void print() {
			int age = 14;
			System.out.println("局部变量:"+age);//14
			System.out.println("内部类变量:"+this.age);//13
			System.out.println("外部类变量:"+Out.this.age);//12
		}
	}
	
	public static void main(String[] args) {
		Out.In in = new Out().new In();
		in.print();
		
	}
}


静态内部类

public class Out {
	private static int age = 12;
	
	static class In{
		public void print() {
			System.out.println(age);
		}
	}
	
	public void print() {
		new In().print();
	}
	
	public static void main(String[] args) {
		Out.In in = new Out.In();
		in.print();//12
		
		Out out = new Out();
		out.print();//12
	}
}

如果用static 将内部内静态化,那么内部类就只能访问外部类的静态成员变量,具有局限性

其次,因为内部类被静态化,因此Out.In可以当做一个整体看,可以直接new 出内部类的对象(通过类名访问static,生不生成外部类对象都没关系)


私有内部类

public class Out {
	private int age = 12;
	
	private class In{
		public void print() {
			System.out.println(age);
		}
	}
	
	public void outPrint() {
		new In().print();
	}
	
	public static void main(String[] args) {
		Out.In in = new Out().new In();
		in.print();//12
		
		Out out = new Out();
		out.outPrint();//12
	}
	
}

如果一个内部类只希望被外部类中的方法操作,那么可以使用private声明内部类。

上面的代码中,我们必须在Out类里面生成In类的对象进行操作,而无法再使用Out.In in = new Out().new In() 生成内部类的对象。也就是说,此时的内部类只有外部类可控制。如同是,我的心脏只能由我的身体控制,其他人无法直接访问它。


方法内部类

public class Out {
	 private int age = 12;
	 
	 public void print(final int x) {
		class In{
			public void inPrint() {
				System.out.println(x);
				System.out.println(age);
			}
		}
		new  In().inPrint();
	}
	 
	 public static void main(String[] args) {
		Out out = new Out();
		out.print(3);
	}
}

输出 3 12

在上面的代码中,我们将内部类移到了外部类的方法中,然后在外部类的方法中再生成一个内部类对象去调用内部类方法。如果此时我们需要往外部类的方法中传入参数,那么外部类的方法形参必须使用final定义。至于final在这里并没有特殊含义,只是一种表示形式而已。



匿名内部类也就是没有名字的内部类

正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写

但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口

匿名内部类的实现

public abstract class Person {//抽象类
	public abstract void eat();
}
public class Demo {
	public static void main(String[] args) {
		Person person = new Person() {
			@Override
			public void eat() {
				System.out.println("吃~");
			}
		};
		person.eat();
	}
}

在接口上使用匿名内部类

public interface Person {//接口
	public void eat();
}
public class Demo {
	public static void main(String[] args) {
	//直接将抽象类Person中的方法在大括号中实现了这样便可以省略一个类的书写
		Person person = new Person() {
			@Override
			public void eat() {
				System.out.println("吃~~");
			}
		};
		person.eat();
	}
}

Thread类的匿名内部类实现

		Thread t = new Thread() {
			public void run() {
				for (int i = 1; i <= 5; i++) {
					System.out.print(i + " ");
				}
			}
		};
		t.start();

Runnable接口的匿名内部类实现

	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			 for (int i = 1; i <= 5; i++) {
                 System.out.print(i + " ");
             }
		}
	};
	Thread thread = new Thread(runnable);
	thread.start();

抱歉!评论已关闭.