淡淡的灰色

一点激情,一点颓废,一点忧郁,一点希望。天空中那一抹淡淡的灰色...

« Sql Server 复习Let's start from here »

java学习笔记2

1.内部类(inner class)
隐藏细节
例子:
abstract class Contents {
	abstract public int value();
}

interface Destination {
	String readLabel();
}

public class Parcel3 {
	private class PContents extends Contents {
		private int i = 11;
		public int value() { return i; }
	}
	protected class PDestination implements Destination {
		private String label;
		private PDestination(String whereTo) {
			label = whereTo;
		}
		public String readLabel() { return label; }
	}
	public Destination dest(String s) {
		return new PDestination(s);
	}
	public Contents cont() {
		return new PContents();
	}
}
内部类PContents 被设为private,所以除了Parcel3 之外,其他任 何东西都不能访问它。PDestination 被设为protected,所以除了Parcel3,Parcel3 包内的类(因为 protected 也为包赋予了访问权;也就是说,protected 也是“友好的”),以及Parcel3 的继承者之外,其 他任何东西都不能访问PDestination。这意味着客户程序员对这些成员的认识与访问将会受到限制。

匿名内部类(anonymous inner class)
例子:
package com.trueteng.test;

public class Parcel6 {
	public Contents cont() {
		return new Contents() {
			private int i = 11;
			public int value() {
				return i;
			}
		}; // 分号这里必须要
	} 
         //下面的内部类定义方法如同上面的匿名内部类
	public Contents cont1(){
		class MyContents implements Contents{
			private int i = 11;
			public int value(){
				return i;
			}
		}
		return new MyContents();
	}

	public static void main(String[] args) {
		Parcel6 p = new Parcel6();
		Contents c = p.cont();
		Contents c1 = p.cont1();
	}
}
  • quote 1.irfept
  • http://www.ccliao.com
  • puygahx

    视频聊天免费视频聊天室,免费提供帅哥美女视频语音聊天交友.视频交友,约会交友
    视频聊天美女视频聊天室,适合您和帅哥美女视频激情聊天交友.才艺展示,互动表演
    视频聊天多人视频聊天室,真正真人的帅哥美女面对面聊天交友.白领交友,爱情交友
    CC聊是专业互动视频平台,全球华人视频聊天社区现场视频表演.美女对战,绝对劲爆
    CC聊是网络最大的一个视频聊天交友网站,注册会员已超过360万,得到网友极力欢迎

    视频聊天

    网址:http://www.ccliao.com
  • 2008-8-9 0:24:48  RevertComment

Comments.

◎ welcome to participate in the discussion

日历

Comments.

Previous.

ADMIN. | EMAIL. | ABOUT.

Powered By Z-Blog 1.8 Devo Build 80201Copyright 2004-2008 TrueTeng. Some Rights Reserved.