java中的匿名内部类
重看孙鑫老
师的视频教程,发现java的匿名内部类这么容易理解,以前看的时候就会糊涂,
的确是个不错的东东,稍微改了一下视频中的例子,例子如下:
上面这个类 的作用就是读出C:WINDOWS目录下后缀为.exe的所有文件。具体分析改天加上。
运行结果:
explorer.exe
hh.exe
IsUn0804.exe
IsUninst.exeiun6002.exe
jad.exe
regedit.exe
twunk_16.exe
twunk_32.ex e
unEBBPClient.exe
uninst.exe
UnSb0804.exe
winhelp.exe
winhlp32.exe
YingSoft-UnInstall.exe
到网上看到另外一个例子,颇有代表性,整理了一下,兹录如下 :
上面程序运行结果:
the anonymose class print
The name = redhacker
package
test;
import
java.io.File;
import java.io.FilenameFilter;
import java.io.FilenameFilter;
public class InnerNonameClassTest
{
/**
* @param args
* @author dougq
*/
public static void main(String[] args)
{
File f = new File ("C:\WINDOWS");
String[] names = f.list(new FilenameFilter() {
public boolean accept(File dir,String name) {
return name.indexOf (".exe")!=-1;
}
});
for (int i=0; i<names.length; i++) {
System.out.println(names [i]);
}
}
}
/**
* @param args
* @author dougq
*/
public static void main(String[] args)
{
File f = new File ("C:\WINDOWS");
String[] names = f.list(new FilenameFilter() {
public boolean accept(File dir,String name) {
return name.indexOf (".exe")!=-1;
}
});
for (int i=0; i<names.length; i++) {
System.out.println(names [i]);
}
}
}
上面这个类 的作用就是读出C:WINDOWS目录下后缀为.exe的所有文件。具体分析改天加上。
运行结果:
explorer.exe
hh.exe
IsUn0804.exe
IsUninst.exeiun6002.exe
jad.exe
regedit.exe
twunk_16.exe
twunk_32.ex e
unEBBPClient.exe
uninst.exe
UnSb0804.exe
winhelp.exe
winhlp32.exe
YingSoft-UnInstall.exe
到网上看到另外一个例子,颇有代表性,整理了一下,兹录如下 :
package
test;
public class
AClass {
String name = new String( "" );
public AClass() {
}
AClass(String name) {
this.name = name;
}
String name = new String( "" );
public AClass() {
}
AClass(String name) {
this.name = name;
}
public void print()
{
System.out.println( "The name = " + name );
}
}
System.out.println( "The name = " + name );
}
}
class QTest {
public static void main(String[] args){
(new AClass( "redhacker" ) {
public void print() {
System.out.println( "the anonymose class print" );
super.print ();
}
}).print();
}
}
public static void main(String[] args){
(new AClass( "redhacker" ) {
public void print() {
System.out.println( "the anonymose class print" );
super.print ();
}
}).print();
}
}
上面程序运行结果:
the anonymose class print
The name = redhacker