Java 内部类与匿名类

内部类的创建及使用

package w5;//

class TestInnerClass
{
	public static void main( String[] args )
	{
		Parcel p = new Parcel();
		Parcel m = new Parcel();
		p.testShip();

		Parcel.Contents c = m.new Contents(33);//在外部创建内部类时,必须基于外部类的实例建立,格式如上
		Parcel.Destination d = m.new Destination( "Hawii" );//d中的label赋值“Hawii”
		System.out.println(p.Getd());//输出beijing

		m.setProperty( c, d );//重新更新Parcel中的c d的值
		
		System.out.println(p.Getd());//仍然输出beijing,即实例P中的d的值没有改变
		System.out.println(m.Getd());//输出Hawii
		m.ship();
	}
}

class Parcel
{
  private Contents c;//定义内部类的变量,一般为private
  private Destination d;
  public String Getd()//实现private的访问
  {
	  return (d.readLabel());//内外类的成员可以相互访问,即使是private也可以
  }
  class Contents//定义内部类
  {
    private int i;//22
	Contents( int i ){ this.i = i; }
    int value() { return i; }
  }
  class Destination 
  {
	  private String label;//Beijing
    Destination(String whereTo) {label = whereTo;}
    String readLabel() { return label; }
  }
  void setProperty( Contents c, Destination d )
  {
	this.c =c; this.d = d;
  }
  void ship()
  {
	System.out.println( "move "+ c.value() +" to "+ d.readLabel() );
  }
  public void testShip() 
  {
    c = new Contents(22);//c中的i赋值22
    d = new Destination("Beijing");
    ship();
  }
}
/*
 * move 22 to Beijing
Beijing
Beijing
Hawii
move 33 to Hawii
*/

当内部类与外部类的字段相同时,使用this操作不同的字段

package w5;//
public class TestInnerThis
{    
	public static void main(String args[]){
	    AA a = new AA();
    	AA.B b = a.new B();
	    b.mb(333); 
    }
}

class AA
{
	private int s = 111;

	public class B {
	    private int s = 222;
    	public void mb(int s) {
	        System.out.println(s); // 局部变量s
	        System.out.println(this.s); // 内部类对象的属性s
	        System.out.println(AA.this.s); //  外层类对象属性s
	    }
    }
}
/*
333
222
111
*/

局部类的定义和使用

package w5;//
class TestInnerInMethod 
{
	public static void main(String[] args) 
	{
		Object obj = new Oute().makeTheInner(47);
		System.out.println("Hello World!" + obj.toString() );
	}
}

class Oute
{
	private int size = 5;
	public Object makeTheInner( int localVar )
	{
		final int finalLocalVar = 99;
		class Inner  //在方法中定义局部类,同局部变量一样,不能被public private等修饰
		{
			public String toString()
			{
				return ( " InnerSize: " + size + //局部类可以访问外部类成员,但不可访问该方法的局部变量,除非是final的局部变量
					// " localVar: " + localVar +   // Error! 
					" finalLocalVar: " + finalLocalVar
				);
			}
		}
		return new Inner();
	}
}

//Hello World! InnerSize: 5 finalLocalVar: 99

匿名类的使用(一次性使用的类)

package w5;//
class TestInnerAnonymous 
{
	public static void main(String[] args) 
	{
		Object obj = new Outerr().makeTheInner(47);
		System.out.println("Hello World!" + obj.toString() );
	}
}

class Outerr
{
	private int size = 5;
	public Object makeTheInner( int localVar )//此方法返回类型是一个对象
	{
		final int finalLocalVar = 99;
		 return new Object() //定义在了方法中,定义一个匿名类并创建实例,不需要class修饰,Object是父类的名字??
		{
			public String toString() 
			{
				return ( " InnerSize: " + size + 
					" finalLocalVar: " + finalLocalVar
				);
			}
		};
	}
}
//Hello World! InnerSize: 5 finalLocalVar: 99

匿名类作为方法的形参

package w5;//
import java.util.Arrays;
import java.util.Comparator;

class SortTest 
{
	public static void main(String[] args)
	{ 
	
		Book[] books = new Book[10];
		for (int i=0; i<books.length; i++ )
		{
			books[i] = new Book((int)(Math.random()*100));
		}
		dump(books);
		Arrays.<Book>sort( books, new Comparator<Book>()//匿名类作为方法的形参
		{
			public int compare(Book b1, Book b2)
			{ 
				return b1.getPrice()-b2.getPrice();
			}
		});
		dump(books);
	}
	static void dump( Book [] books )
	{
		for (int i=0; i<books.length; i++ )
		{
			System.out.print(books[i]+" ");
		}
		System.out.println();
	}
}
class Book
{
	private int price;
	Book(int price){ this.price=price; }
	int getPrice(){ return price;}
	public String toString(){ return ""+price; }
}
/*
92 57 29 1 52 66 7 68 37 16 
1 7 16 29 37 52 57 66 68 92 
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值