【问题标题】:Java Inheritance ExampleJava 继承示例
【发布时间】:2011-05-31 01:33:08
【问题描述】:

下面是继承的例子

class Parent {
    Parent(int a, int b) {
        int c = a + b;
        System.out.println("Sum=" + c);
    }
    void display() {
        System.out.println("Return Statement");
    }
}
class Child extends Parent {
    Child(int a, int b) {
        int c = a - b;
        System.out.println("Difference=" + c);
    }
}
public class InheritanceExample {
    public static void main(String args[]) {
        Child c = new Child(2, 1);
        c.display();
    }
}

当我没有非参数化构造函数 parent() 时出现以下错误

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Implicit super constructor Parent() is undefined. Must explicitly invoke another constructor

    at Child.<init>(InheritanceExample.java:14)
    at InheritanceExample.main(InheritanceExample.java:22)

你能解释一下无参数构造函数在基类中的用途吗?

【问题讨论】:

  • 在这种情况下,它只是一个创建父类实例的默认构造函数。通常您会将对象变量设置为某个默认值或 null。
  • 请包含错误的文本,而不是屏幕截图。
  • 真的是源代码吗?它通过复制/粘贴为我编译并运行良好。
  • @TofuBeer:如果您在基类中包含不带参数的构造函数,它将编译。如果没有,它不会。
  • 是 Java 约定在类中使用大写字母并在同一行中使用大括号。

标签: java


【解决方案1】:
class child extends parent
{
    child(int a,int b)
    {
        int c=a-b;
        System.out.println("Difference="+c);
    }
}

子类构造函数必须做的第一件事就是调用父类构造函数。 如果您没有显式执行此操作(例如super(a,b)),则隐含调用默认构造函数(super())。

为此,您必须拥有此默认构造函数(不带任何参数)。

如果你不声明任何构造函数,你会得到默认构造函数。如果至少声明了一个构造函数,则不会自动获取默认构造函数,但可以重新添加。

您收到的错误消息是抱怨对 super() 的隐含调用不起作用,因为父类中没有这样的构造函数。

两种修复方法:

  1. 添加默认构造函数
  2. 在子构造函数的第一行,调用一个非默认父构造函数(super(a,b)

另外,请不要使用全小写的类名。

【讨论】:

    【解决方案2】:

    它要求 parent() 的原因是因为 child 扩展了 parent,但是您没有在 child 构造函数中显式调用 super(a,b)。由于没有显式调用父构造函数,javac 尝试调用默认构造函数 parent() 并在找不到时报错。

    您可以通过以下代码看到这一点:

    class parent
    {
       public parent() {
          System.out.println("Parent Constructor");
       }
    
       public parent(int a,int b) {
          int c=a+b;
          System.out.println("Sum="+c);
       }
    
       public void display() {
          System.out.println("Return Statement");
       }
    }
    
    class child extends parent
    {
       public child(int a,int b) {
          int c=a-b;
          System.out.println("Difference="+c);
       }
    }
    
    public class InheritanceExample
    {
       public static void main(String args[]) {
          child c=new child(2,1);
          c.display();
       }
    }
    

    输出:

    Parent Constructor
    Difference=1
    Return Statement
    

    此外,这在没有默认构造函数的情况下也可以正常工作:

    class parent
    {
       public parent(int a,int b) {
          int c=a+b;
          System.out.println("Sum="+c);
       }
    
       public void display() {
          System.out.println("Return Statement");
       }
    }
    
    class child extends parent
    {
       public child(int a,int b) {
          super(a,b);
          int c=a-b;
          System.out.println("Difference="+c);
       }
    }
    
    public class InheritanceExample
    {
       public static void main(String args[]) {
          child c=new child(2,1);
          c.display();
       }
    }
    

    输出:

    Sum=3
    Difference=1
    Return Statement
    

    【讨论】:

      【解决方案3】:

      我认为有一个类似的问题:

      Why should the derived class constructor always access base class constructor?

      你可以这样想:由于“child”继承自“parent”,“child”也必须是“parent”的有效实例(多态性),然后它才能充当“parent”本身。因此,“child”必须做的第一件事就是将自己构造为有效的“parent”——因此通过 super() 调用“parent”的构造函数必须是构造函数中的第一个方法调用。如果不存在这样的调用,则隐式调用“父”的无参数构造函数。

      【讨论】:

        【解决方案4】:

        出现错误的原因是,如果我们不显式调用 super ,那么 JVM 会将 super() 放入子类的构造函数中,并且这个 super() 在父类中搜索不带参数的构造函数,该参数不在您的类中所以这是错误的。要么将非参数化构造函数放在父类中,要么将语句 super(a,b) 放在子构造函数的第一行。

         class Parent 
            {
                Parent(int a, int b) 
               {
                    int c = a + b;
                    System.out.println("Sum=" + c);
                   }
                void display() 
            {
                 System.out.println("Return Statement");
                }
            }
            class Child extends Parent 
            {
                Child(int a, int b) 
            {
                super(a,b);
                int c = a - b;
                     System.out.println("Difference=" + c);
               }
            }
            class InheritanceExample 
            {
                public static void main(String args[]) 
            {
                     Child c = new Child(2, 1);
                c.display();
               }
           }
        

        【讨论】:

          【解决方案5】:
          public class Mobile{
          private String manufacturer;
          private String operating_system;
          public String model;
          private int cost;
          
          Mobile(String man, String o,String m, int c){
          this.manufacturer=man;
          this.operating_system=o;
          this.model=m;
          this.cost=c;
          }
          public String getModel(){
          return this.model;
          }
          }
          
          public class Android extends Mobile{
          Android(String man, String o,String m, int c){
          super(man,o,m,c);
          }
          public String getModel(){
          return "This is Android mobile" +model;
          }
          

          【讨论】:

            【解决方案6】:
            import java.io.*;
            public class XXX
            {
            public static void main()throws IOException
            {
            System.out.println("Enter your name.");
            String name = in.readLine();
            System.out.println(name+" rules!! Thank You!!");
            }
            }
            

            【讨论】:

              猜你喜欢
              • 2019-04-03
              • 2017-08-21
              • 2015-12-13
              • 1970-01-01
              • 2011-09-12
              • 2011-07-22
              • 1970-01-01
              • 1970-01-01
              • 2013-03-07
              相关资源
              最近更新 更多