java 我们可以编写自定义标记接口吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10551699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can we write custom marker interfaces
提问by Anil Kumar C
I want to write my own marker interfaces like java.io.Serializable
or Cloneable
which can be understandable to JVM. Please suggest me the implementation procedure.
我想编写自己的标记接口,例如JVM 可以理解的java.io.Serializable
或Cloneable
可以理解的。请建议我执行程序。
For example I implemented a interface called NotInheritable
and all the classes implementing this interface has to avoid inheritance.
例如,我实现了一个被调用的接口NotInheritable
,所有实现这个接口的类都必须避免继承。
采纳答案by Marko Topolnik
public interface MyMarkerInterface {}
public class MyMarkedClass implements MyMarkerInterface {}
Then you can for example have method taking only MyMarkerInterface
instance:
然后你可以例如有只采用MyMarkerInterface
实例的方法:
public myMethod(MyMarkerInterface x) {}
or check with instanceof
at runtime.
或instanceof
在运行时检查。
回答by Vijay Bhatt
Yes We can write our own marker exception.... see following example....
是的,我们可以编写自己的标记异常.... 见下面的例子....
interface Marker{
}
class MyException extends Exception {
public MyException(String s){
super(s);
}
}
class A implements Marker {
void m1() throws MyException{
if((this instanceof Marker)){
System.out.println("successfull");
}
else {
throw new MyException("Unsuccessful class must implement interface Marker ");
}
}
}
/* Class B has not implemented Maker interface .
* will not work & print unsuccessful Must implement interface Marker
*/
class B extends A {
}
// Class C has not implemented Maker interface . Will work & print successful
public class C extends A implements Marker
{ // if this class will not implement Marker, throw exception
public static void main(String[] args) {
C a= new C();
B b = new B();
try {
a.m1(); // Calling m1() and will print
b.m1();
} catch (MyException e) {
System.out.println(e);
}
}
}
回答by Kumar Nishant
suppose myMethod should be called only if marker MyInterface should be marked there.
假设仅当标记 MyInterface 应在此处标记时才应调用 myMethod。
interface MyInterface{}
class MyException extends RuntimeException{
public MyException(){}
public MyException(String message){
super(message);
}
}
class MyClass implements MyInterface{
public void myMethod(){
if(!(this instanceOf MyInterface)){
throw new MyException();
}else{
// DO YOUR WORK
}
}
}
回答by Balaswamy Vaddeman
You can write your own Marker interface ,JVM do not know anything about it.
您可以编写自己的 Marker 接口,JVM 对此一无所知。
You have to provide functionality by using instanceof
. check this
您必须使用instanceof
. 检查这个
回答by Damian Leszczyński - Vash
A marker interface is an empty interface. This mean you just need to create a interface and do not create any method in it. Better explanation you will find here.
标记接口是一个空接口。这意味着您只需要创建一个接口,而不要在其中创建任何方法。你会在这里找到更好的解释。
This approach can be replaced with Annotationsthat have similar functionality over types. more