java - 如何在java类中包含接口(interface)

标签 java

我想在此类中包含一个 Printable 接口(interface),但它显示错误。

error:Class file collision: A resource exists with a different case:     
'/example01/bin/com/example/test/test.class'.

这是我的代码

package com.example.test;

import com.example.test.Printable;

class A implements Printable {
    public void a() {
        System.out.println("a method");
    }

    @Override
    public void b() {

    }

}

abstract class B implements Printable {
    public void b() {
        System.out.println("b method");
    }

}

class Call {
    void invoke(Printable p) {// upcasting
        if (p instanceof A) {
            A a = (A) p;// Downcasting
            a.a();
        }
        if (p instanceof B) {
            B b = (B) p;// Downcasting
            b.b();
        }

    }
}// end of Call class

class Test {
    public static void main(String args[]) {
        Printable p = new A();
        Call c = new Call();
        c.invoke(p);
    }
}

这是我的可打印接口(interface)类代码

public interface Printable {

    public void a ();
    public void b();
}

那么我如何将 Printable 接口(interface)导入到此类中,我们将不胜感激。谢谢

最佳答案

这是我将接口(interface)嵌入到类中的方法:

public class NestedClassInterface {

/**
 * Nested interface
 * @author Markus
 */
public interface Printable {
    public void a();
}

/**
 * MAIN
 * @param args
 */
public static void main(String[] args) {
    Printable t = new MyTest();
    t.a();
}

/**
 * Static class (otherwise the compiler cannot find it)
 * @author Markus
 *
 */
public static class MyTest
implements Printable {
    public void a() {
        System.out.println("NestedClassInterface.MyTest.a()");
    }
}

}

注释

让我们了解您的进展。

关于java - 如何在java类中包含接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25203133/

相关文章:

java - JPanel 失去焦点并且听众没有射击

java - 从 AnnotationConfigWebApplicationContext 加载 ApplicationContext 文件

java - POI excel 转换为二维数组

java - 如何最好地从方法中的匿名类返回值?

java - TortoiseSVN 和 Subclipse 图标不随 SVN 更新?

java - FileOutputStream 与 OutputStream,为什么以及何时?

java - JScrollPane 中具有空布局的 JPanel - 看不到元素

java - 为什么 javafx 8 UI 线程在更改监听器内行为异常?

java - 何时用 256 初始化数组

java.lang.IllegalStateException : The driver executable does not exist:/var/lib/jenkins/jobs/Ancon-Automation/workspace\src\main\. ..\chromedriver.exe