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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:31:45  来源:igfitidea点击:

can we write custom marker interfaces

java

提问by Anil Kumar C

I want to write my own marker interfaces like java.io.Serializableor Cloneablewhich can be understandable to JVM. Please suggest me the implementation procedure.

我想编写自己的标记接口,例如JVM 可以理解的java.io.SerializableCloneable可以理解的。请建议我执行程序。

For example I implemented a interface called NotInheritableand 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 MyMarkerInterfaceinstance:

然后你可以例如有只采用MyMarkerInterface实例的方法:

public myMethod(MyMarkerInterface x) {}

or check with instanceofat 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);
    }

}
}

OUTOPUT

输出

回答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

这种方法可以替换为在类型上具有类似功能的注释更多的