简体   繁体   English

在不调用父 class 构造函数的情况下,可以在 java 中调用直接子 class 构造函数

[英]Without calling parents class constructor,it is possible to call direct child class constructor in java

I have a parents class whose name 'parents' and child class whose name 'child' which is extends parent class.我有一个父母 class,他们的名字是“父母”,孩子 class 的名字是“孩子”,是父母 class 的延伸。 if child class and parents class both having same argument but different behaviour constructor then if i am create child class objcet it will automatically call parents class constructor which i don't want.how it is possible. if child class and parents class both having same argument but different behaviour constructor then if i am create child class objcet it will automatically call parents class constructor which i don't want.how it is possible.

Ex.前任。

    class parents
    {
      parents()
      {
      
     System.out.println("hello, i am 
          parents class constructor");
      }
    }

    class child extends parents
    {
      child()
      {
     System.out.println("hello, i am 
  child class constructor");
      }
    }

    class main
    {
      public static void 
   main(String[] args)
      { 
        child c = new child();
      }
    }

Now here,现在在这里,

Output is..... Output 是.....

    hello, i am  parents class constructor
    hello, i am  child class constructor

But i want only...但我只想...

    hello, i am child class constructor

Means only child class constructor is run not parent class.意味着只有子 class 构造函数运行而不是父 class。

How it is possible...怎么可能...

Whenever the constructor of a class is called, the constructors of its parent class are implicitly invoked as well.每当调用 class 的构造函数时,也会隐式调用其父 class 的构造函数。

The reasoning, as pointed in the comment by @SilvioMayolo, is that正如@SilvioMayolo 在评论中指出的那样,原因是

"If you don't want the child to behave like the parent class, then it shouldn't be a parent class." “如果你不希望孩子表现得像父母 class,那么它不应该是父母 class。”

Conceptually, it is a child class is because the child inherits some properties or functionalities from its parent .从概念上讲,它是一个子 class 是因为子继承了其父的一些属性或功能 As in your example, if you don't want "hello, i am parent class constructor" to be printed when the child constructor is called, you actually do not need the parent class of the child to actually be its parent.在您的示例中,如果您不希望在调用子构造函数时打印"hello, i am parent class constructor" ,则实际上不需要子的父 class 实际上是其父。

Therefore, what functionality you want to achieve is not possible in Java because it is an implicit feature of the language.因此,您想要实现的功能在 Java 中是不可能实现的,因为它是语言的隐含特性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 声明一个Java对象但调用子类的构造函数 - Declaring a Java object but calling the constructor of a child class 在不知道类名称的情况下调用构造函数(java) - Calling the constructor without knowing the name of the class (java) 在Java中,超级构造函数调用是否有可能实际调用调用类中的构造函数? - In Java, is it possible for a super constructor invocation actually invoke a constructor in the calling class? Java:抽象类中的直接构造函数 - Java: Direct constructor in abstract class 在 Java 中,是否可以在没有 class 和构造函数的情况下创建 object? - In Java, Is it possible to create an object without a class and a constructor? 在不知道Java中类的名称的情况下调用类构造函数 - Calling class constructor without knowing the name of the class in Java 在Java中,您可以通过父类的子类的构造函数调用父类的超类构造函数吗? - In Java, can you call a parent class's superclass constructor through the constructor of the child class of the parent class? 子类中的Java构造函数错误 - Java constructor error in child class Java子类中的构造函数不正确 - incorrect constructor in Java child class Java:如何在子类构造函数中调用超类构造函数? - Java: How does a call to the super class constructor inside of a child class constructor work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM