【发布时间】:2010-04-29 05:09:23
【问题描述】:
我尝试封装。接口例外,静态内部类工作,非静态内部类不工作,无法理解术语:嵌套类,内部类,嵌套接口,接口抽象类——听起来太重复了!
糟糕! --- 来自接口的异常“非法类型”显然是因为值是常量(?!)
static interface userInfo
{
File startingFile=new File(".");
String startingPath="dummy";
try{
startingPath=startingFile.getCanonicalPath();
}catch(Exception e){e.printStackTrace();}
}
多种方法:接口、静态内部类图像 VS 非静态内部类图像
import java.io.*;
import java.util.*;
public class listTest{
public interface hello{String word="hello word from Interface!";}
public static class staticTest{
staticTest(){}
private String hejo="hello hallo from Static class with image";
public void printHallooo(){System.out.println(hejo);}
}
public class nonStatic{
nonStatic(){}
public void printNonStatic(){System.out.println("Inside non-static class with an image!");}
}
public static class staticMethodtest{
private static String test="if you see mee, you printed static-class-static-field!";
}
public static void main(String[] args){
//INTERFACE TEST
System.out.println(hello.word);
//INNNER CLASS STATIC TEST
staticTest h=new staticTest();
h.printHallooo();
//INNER CLASS NON-STATIC TEST
nonStatic ns=(new listTest()).new nonStatic();
ns.printNonStatic();
//INNER CLASS STATIC-CLASS STATIC FIELD TEST
System.out.println(staticMethodtest.test);
}
}
输出
hello word from Interface!
hello hallo from Static class with image
Inside non-static class with an image!
if you see mee, you printed static-class-static-field!
相关
- Nesting classes
- 内部类?
- 接口
【问题讨论】:
-
@ggf31416,静态接口有效。见stackoverflow.com/questions/71625/…
-
你应该让你的后续问题成为一个新问题,因为它会让这里的答案变得混乱。
-
非静态内部类的正确语法是:
new listTest().new nonStatic();。由于它不是静态的,它必须引用外部类的现有实例。
标签: java interface abstract-class nested-class reusability