Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
请用简单的Java代码修复此错误_Java_Inheritance - Fatal编程技术网

请用简单的Java代码修复此错误

请用简单的Java代码修复此错误,java,inheritance,Java,Inheritance,我正在尝试使用类E扩展类Test1,我只想显示一条消息 class Test1 { public static void main(String[] args) { E e = new E(); } } class E extends Test1 { System.out.println("Hello World!"); } 我发现以下错误: Test1.java:10: <identifier> expected System.out.pr

我正在尝试使用类E扩展类Test1,我只想显示一条消息

class Test1
{
 public static void main(String[] args) 
 {
    E e = new E();
 } 
} 
class E extends Test1
{
  System.out.println("Hello World!");
}
我发现以下错误:

Test1.java:10: <identifier> expected
        System.out.println("Hello World!");
                          ^
Test1.java:10: illegal start of type
        System.out.println("Hello World!");
                           ^
2 errors
Test1.java:10:预期值
System.out.println(“你好,世界!”);
^
java:10:类型的非法开始
System.out.println(“你好,世界!”);
^
2个错误

您的E类缺少构造函数

class Test1
{
 public static void main(String[] args) 
 {
    E e = new E();
 } 
} 
class E extends Test1
{
  public E(){
     System.out.println("Hello World!");
  }
}
类E应该有实例块

class Test1 {
    public static void main(String[] args) {
        E e = new E();
    } 
} 
class E extends Test1 {
  {
     System.out.println("Hello World!");
  }
}

你知道那个错误是什么意思吗?你为解决这个问题付出了什么努力?类E扩展了Test1{public E(){System.out.println(“Hello World!”);}}@PeterRader No。他的输出不在方法/构造中。你不能让代码仅仅放在类中。我建议您阅读一些关于Java中类如何工作的参考资料。这不是它们应该用来做的。实际上它只缺少一副牙套。@MarkoTopolnik正如Marko所说,只需要一副牙套。默认构造函数由compiler@MarkoTopolnik这样做的副作用是将代码添加到类中的每个构造函数中。如果他们添加更多的构造函数,这可能会出乎意料。没错,@MarkoTopolnik是对的,构造函数的添加只是为了澄清问题。如果你关心更广泛的公众,那么请写一个答案,首先解释OOP的基础知识,以及为什么发布的代码在有大括号或没有大括号的情况下都是毫无意义的。