如何将接口放在一个单独的文件中?

huangapple go评论73阅读模式
英文:

How can I put interface in a seperate file?

问题

我被卡在这个错误上。我想把我的接口和类放在不同的文件中。但是编译器报错了。任何建议都将不胜感激。

我已经创建了一个接口和一个类。

..test\t1.java

package test;

interface t1 {
    public void sayHello();
}

..\test\HelloWorld.java

package test;

public class HelloWorld implements t1 {
    public void sayHello() {
        System.out.println("Hello World");
    }

    public static void main(String args[]) {
        HelloWorld h = new HelloWorld();
        h.sayHello();
    }
}

我尝试编译它,但是编译器报错了。

test>javac HelloWorld.java
HelloWorld.java:3: error: cannot find symbol
public class HelloWorld implements t1
                                   ^
  symbol: class t1
1 error
英文:

I am stuck with this error. I want to put my interface and class in separate files. But the compiler is throwing an error. Any suggestion will be appreciated

I have create an interface and class

..test\t1.java

package test;

 interface t1
{
	public void sayHello();
}

..\test\HelloWorld.java

package test;

public class HelloWorld implements t1
{
	public void sayHello()
	{
		System.out.println("Hello World");
	}
	public static void main(String args[])
	{
		HelloWorld h=new HelloWorld();
		h.sayHello();
	}
}

I tried to compile it but the compiler is throwing an error

test>javac HelloWorld.java
HelloWorld.java:3: error: cannot find symbol
public class HelloWorld implements t1
                                   ^
  symbol: class t1
1 error

答案1

得分: 3

编译

我没有包含完整的路径

javac -classpath e:\user\java; HelloWorld.java

运行

我没有包含有效的包名和类名以执行。

    E:\user\java\test>java -classpath e:\user\java\; test.HelloWorld
Hello World
英文:

For compilation

I didn't include the complete path

javac -classpath e:\user\java; HelloWorld.java

Run

I didn't include the valid package name with class name for execution.

    E:\user\java\test>java -classpath e:\user\java\; test.HelloWorld
Hello World

huangapple
  • 本文由 发表于 2020年10月20日 17:24:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64442243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定