最近在研究内部类,详情看java编程思想第2版中文版第264页,里面有个例子public clcass Parcel4{
pbulic Destinatin dest(String s){ class Pdestination implements Destination{
private String label; private PDestination(String whereto){
Label = whereto
} public String readLabel()
{return Label;}
} return new PDestination(s);
}
}
ok,好了,在这里,我之前的想法,就是interface是一个行为的决定,而类,就是一个群体。那么我把以上函数翻译成汉字,但感觉老是不同,请各位老师指教一下。public class 特殊群{
public 写字 dest(string s){
class 英雄 implements 写字{
..................
}
}
return new 英雄(s)}以上就奇怪了,为什么一个行为,一个类,可以拉在一起说的那?还是我汉语的翻译或者理解,有点问题呢?
pbulic Destinatin dest(String s){ class Pdestination implements Destination{
private String label; private PDestination(String whereto){
Label = whereto
} public String readLabel()
{return Label;}
} return new PDestination(s);
}
}
ok,好了,在这里,我之前的想法,就是interface是一个行为的决定,而类,就是一个群体。那么我把以上函数翻译成汉字,但感觉老是不同,请各位老师指教一下。public class 特殊群{
public 写字 dest(string s){
class 英雄 implements 写字{
..................
}
}
return new 英雄(s)}以上就奇怪了,为什么一个行为,一个类,可以拉在一起说的那?还是我汉语的翻译或者理解,有点问题呢?
就像 人 这个类是一个模板(接口也是类,只不过是特殊类),可以定义 他的 属性(比如 皮肤,头发什么的) 行为 (比如 吃饭,走路) 当模板具体成 张三或是李四的时候,就可以重写属性和方法
至于你说的内部类,其实 是一个匿名类,功能主要是传参,而这个匿名类里的内容 ,其实是他的子类的内容,这不过这个子类没有名字 ,比如 addwindowlisten(new windowadapter(){......})
这里的windowadapter() 就是匿名类 {......} 里的内容是一个你看不见的windowadapter的子类实现的