内部类

将一个类的定义放在另一个类的内部,称为内部类。
编写一个名为outer的外部类,它包含一个Inner内部类。在outer中添加一个方法他返回inner类型对象。在main中创建并初始化一个指向inner的引用。
程序编译后,会产生两个class文件,分别是Outer.class和Outer$Inner.class,其中$代表了下面程序中Outer.Inner中的那个。
Outer.Inner inner2=outer.new Inner();可以用来产生内部类对象,需要注意两点:
1.开头的Outer是为了标明需要生成的内部类对象在哪个外部类中
2.必须先有外部类对象才能生成内部类对象,因为内部类的作用就是为了访问外部类中的成员变量
package com.htxx.action.business;

public class Outer {
   class Inner{
	   
   }
   Inner getInner(){
	   return new Inner();
   }
   public static void main(String[] args) {
	Outer outer=new Outer();
	Inner inner=outer.getInner();
	//Inner inner2=new Inner();
  Inner inner2=outer.new Inner();
  Outer.Inner inner2=outer.new Inner();
}
}

当生成一个内部类对象,此对象与生成他的外部类对象产生某种联系,使得内部类对象可以访问外部类的成员变量。

package com.htxx.action.business;

public class Outer {
	private String hello="555";
   class Inner{
	   private String hello="444";
	   void say(){
		   String hello="333";
		   System.out.println(hello);
		   System.out.println(this.hello);
		   System.out.println(Outer.this.hello);
	   }
   }
   Inner getInner(){
	   return new Inner();
   }
   void say(){
	   System.out.println("hello");
   }
   public static void main(String[] args) {
	Outer outer=new Outer();
	Inner inner=outer.getInner();
	//Inner inner2=new Inner();
	Outer.Inner inner2=outer.new Inner();
	//inner2.say//不能访问外部类成员函数
	inner2.say();
}
}  
输出结果:333 444 555
内部类想生成对外部类对象的引用,可以使用外部类的名字.this;
package com.htxx.action.business;

public class Outer {
	private String hello="555";
   class Inner{
	   private String hello="444";
	   void say(){
		   String hello="333";
		   System.out.println(hello);
		   System.out.println(this.hello);
		   System.out.println(Outer.this.hello);
	   }
	   Outer getOuter(){
		   return Outer.this;
	   }
   }
   void say(){
	   System.out.println("hello");
   }
   public static void main(String[] args) {
	Outer outer=new Outer();
	//Inner inner2=new Inner();
	Outer.Inner inner2=outer.new Inner();
	inner2.getOuter().say();
}
}

在未拥有外部类对象时,是不能创建内部类对象的。这是因为内部类对象会暗暗连接到创建他的外部类对象。
但是如果你创建的是嵌套类(静态内部类),那么他不需要外部对象的引用。
package com.htxx.action.business;
class Outer{
	static class Inner{
		void say(){
			System.out.println("hello");
		}
	}
	Inner getInner(){
		return new Inner();
	}
}
public class A {
	public static void main(String[] args) {
      Outer.Inner inner=new Outer.Inner();
      inner.say();
	}


}
创建一个包含内部类的类,在另一个独立的类内,创建此内部类实例。
package com.htxx.action.business;


import com.htxx.action.business.Outer.Inner;


class Outer{
	class Inner{
		void say(){
			System.out.println("hello");
		}
	}
}
public class A {
	public static void main(String[] args) {
	    Outer outer=new Outer();
	    Outer.Inner inner=outer.new Inner();
	    inner.say();
	}


}
私有内部类:
这时只能由外部类操作
package com.htxx.action.business;


import javassist.runtime.Inner;


//import com.htxx.action.business.Outer.Inner;


class Outer{
	private class Inner{
		void say(){
			System.out.println("helloa ");
		}
	}
	void getInner(){
		 new Inner().say();
	}
}
public class A {
	public static void main(String[] args) {
	    Outer outer=new Outer();
	    //因为是private,对于非外部类是不可见的
	    Outer.Inner inner=outer.new Inner();
	    inner.say();
	    //The type Outer.Inner is not visible
	    Outer.Inner inner2=outer.getInner();
	  //The type Outer.Inner is not visible
	    outer.getInner();//外部类调用方法使用内部类
	}


}
在方法和作用域内的内部类(在定义该内部类域以外,内部类是不可用的):
package com.htxx.action.business;
import com.sun.swing.internal.plaf.basic.resources.basic;
public class A {
    void sayA(){
    	class B{
    		void sayB(){
    			System.out.println("haha");
    		}
    	}
    	B b=new B();
    	b.sayB();
    }
    public static void main(String[] args) {
		A a=new A();
		A.B b=b.new B();
		//A.B cannot be resolved to a type
	}
}
创建一个至少有一个方法的接口。在某个方法内定义内部类实现该接口,这个方法返回对此接口的引用。
package com.htxx.action.business;
interface JieKou{
	void m1();
}
public class A {
	JieKou sayA(){
    	class B implements JieKou{
			@Override
			public void m1() {
				// TODO Auto-generated method stub
				System.out.println("m1");
			} 		
    	}
    	return new B();
    }
    public static void main(String[] args) {


	}
}
重复前一个练习,但将内部类定义在某一个方法的作用域内。
package com.htxx.action.business;
interface JieKou{
	void m1();
}
public class A {
	void sayA(){
		if(2>1){
	    	class B implements JieKou{
				@Override
				public void m1() {
					// TODO Auto-generated method stub
					System.out.println("m1");
				} 		
	    	}
	    	B b= new B();
	    	b.m1();
		}


    	
    }
    public static void main(String[] args) {
       A a =new A();
       a.sayA();
	}
}
匿名内部类:
http://www.cnblogs.com/nerxious/archive/2013/01/25/2876489.html
http://blog.sina.com.cn/s/blog_62ea4cf40100mubj.html
接口内部的类:
正常情况下,不能再接口内部防止任何代码,但是嵌套类可以作为接口的一部分。
放到接口中的任何类自动是public和static的。因为类是static的,只是将嵌套类置于接口的命名空间内,
这并不违反接口的规则。甚至可以再内部类中实现外围接口。
创建一个包含嵌套类的接口,实现此接口并创建嵌套类的实例。
package com.htxx.action.business;


public interface Inter1 {
	void say();
    static class SAS implements Inter1{


	@Override
	public void say() {
		// TODO Auto-generated method stub
		System.out.println("111");
	}


    public static void main(String[] args) {
		new SAS().say();
	}
  }
  
}
-----------------------------------------参考文档-----------------------------
http://www.cnblogs.com/nerxious/archive/2013/01/24/2875649.html


 
 
 
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值