Java:可重用的接口、抽象类或内部类封装?
我尝试封装。接口例外,静态内部类工作,非静态内部类不工作,无法理解术语:嵌套类,内部类,嵌套接口,接口抽象类——听起来太重复了!
糟糕! --- 接口中的“非法类型”异常显然是因为值为常量(?!)
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);
}
}
< strong>输出
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!
相关
- 嵌套类
- 内部类?
- 接口
I try to encapsulate. Exeption from interface, static inner class working, non-static inner class not working, cannot understand terminology: nested classes, inner classes, nested interfaces, interface-abstract-class -- sounds too Repetitive!
BAD! --- Exception 'illegal type' from interface apparently because values being constants(?!)
static interface userInfo
{
File startingFile=new File(".");
String startingPath="dummy";
try{
startingPath=startingFile.getCanonicalPath();
}catch(Exception e){e.printStackTrace();}
}
MANY WAYS TO DO IT: Interface, static inner class image VS non-static innner class image
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);
}
}
OUTPUT
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!
Related
- Nesting classes
- inner classes?
- interfacses
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
接口中不能有代码。只是方法签名。
顶级接口不能是静态的。
我建议您从这里开始学习 Java。
You cannot have code in interfaces. Just method signatures.
Top level interfaces cannot be static.
I suggest you start your learning of Java here.
问题是您在方法之外编写代码。为此,您确实需要一个类,并且必须将代码放入方法中。例如:
这确实假设 java.io.File 已导入。
然后您可以调用 UserInfo.myMethod();
您可能还想导入 java.util.IOException 并捕获 IOException 而不是一般异常。
此外,按照 Java 惯例,类和接口以大写字母开头。
编辑:描述您最近对问题的评论:
当您想要强制类似的类(想想不同类型的 DVD 播放器)具有相同的基本功能(播放 DVD、停止、暂停)时,请使用接口。您可以类似地使用抽象类,但是当所有的类都会以相同的方式实现一些相同的事情时。
The problem is that you're writing code outside of a method. You do need a class for this and you must put your code inside a method. For example:
This does assume that java.io.File was imported.
You can then call UserInfo.myMethod();
You might also want to import java.util.IOException and catch an IOException instead of a general Exception.
Also, classes and interfaces start with a capital letter by Java conventions.
EDIT: To describe your recent comment on your question:
Use an interface when you want to force similar classes (Think different types of DVD players) to have the same basic functionality (playing dvds, stopping, pausing. You use an abstract class similarly, but when all of the classes will implement some of the same things the same way.
我想你想这样做:
I think you wanted to do this:
您不能将代码放入接口中,接口仅描述对象的行为方式。即使使用类时,也应该将此类代码放在方法中,而不是直接放在类主体中。
you cant put code in an interface, an interface only describes how an object will behave. Even when you use Classes, you should put this kind of code in a method, and not directly in the class body.
接口中不能有实际代码,只能有方法签名和常量。你想做什么?
You can't have actual code in an interface, only method signatures and constants. What are you trying to do?
看起来您想在这里编写一个
class
。Looks like you want to write a
class
here.