Java Puzzler-原因是什么? [英] Java Puzzler- What is the reason?
本文介绍了Java Puzzler-原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了以下代码。
class String {
private final java.lang.String s;
public String(java.lang.String s){
this.s = s;
}
public java.lang.String toString(){
return s;
}
public static void main(String[] args) {
String s = new String("Hello world");
System.out.println(s);
}
}
当我执行它时,得到以下错误
When I execute it, get following error
The program compiled successfully, but main class was not found.
Main class should contain method: public static void main (String[] args).
为什么是这样?虽然主要方法被定义为什么系统不读/ ?
Why is it so?... though main method is defined why system is not reading/ recognizing it ?
推荐答案
public static void main(String[] args) {
因为您必须使用 java.lang.String
你自己。在你的main方法中,你使用的 String
实际上是定义的自定义 String
code> java.lang.String 。
Becuase you must use a java.lang.String
, not your own. In your main method, the String
you're using is actually the custom String
that was defined, not a real java.lang.String
.
这里是代码,澄清一下:
Here is the code, clarified a bit:
class MyString {
private final String s;
public MyString(String s){
this.s = s;
}
public String toString(){
return s;
}
public static void main(MyString[] args) { // <--------- oh no!
MyString s = new MyString("Hello world");
System.out.println(s);
}
}
所以,你可以从这个谜题中学到的教训是:不要将你的类命名为其他常用的类!
So, the lesson that you can learn from this puzzle is: don't name your classes as other commonly used classes!
这篇关于Java Puzzler-原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文