如何自己定义标识接口,让其他需要此接口功能的类必须实现此标识接口,否则运行时报错
就像可序列化接口(它没有定义任何东西却能产生不小的作用)一样~~如果某个类没有实现可序列化接口使用某些方法(比如writeObject)调用这个类的时候会出现运行错误
实现以后就不会出现运行错误~~我也想做这种接口(能更好的了解面向对象编程和思想)~~
求大大们给我提供一些相关方法、例子或思维~~~

解决方案 »

  1.   

    简单
    例子:
    interface Test {
    }class NotImplementInterfaceTest
    {
    };public class InterfaceTest
    {
    public static void main(String[] args) 
    {
    InterfaceTest it = new InterfaceTest();
    NotImplementInterfaceTest notImplement = new NotImplementInterfaceTest(); it.test(notImplement); // 报错,因为notImplement未实现接口Test
    } public void test(Test test) {
    // 代码
    }
    }
      

  2.   

    感谢你的回答,但你说的这种不是我要的答案你是依靠test方法的参数类型来限制的
    我是想问writeObject(Object object)这个方法是怎么作到限制使用接口的。
      

  3.   

    if(object instanceof Serializable)
      

  4.   

    楼上说的对
    用instanceof 就可以了不过在jdk中 ObjectOutputStream 
    的writeObject()方法的源代码没有
    类似这样的代码
    具体什么机制不清楚。
      

  5.   

    ObjectOutputStream   
    的writeObject()方法的源代码有,在第1149行
    if (obj instanceof String) {
    writeString((String) obj, unshared);
        } else if (cl.isArray()) {
    writeArray(obj, desc, unshared);
        } else if (obj instanceof Enum) {
    writeEnum((Enum) obj, desc, unshared);
        } else if (obj instanceof Serializable) {
    writeOrdinaryObject(obj, desc, unshared);
        } else {
    if (extendedDebugInfo) {
        throw new NotSerializableException(
    cl.getName() + "\n" + debugInfoStack.toString());
    } else {
        throw new NotSerializableException(cl.getName());
    }    
        }
    } finally {
        depth--;
        bout.setBlockDataMode(oldMode);
    }
        }