简体   繁体   English

当我们创建子类对象时,super如何引用父类对象?

[英]how can super refer to parent class object when we create a child class object?

When we create a child object, single object is created of child class which uses parent class variables.当我们创建子对象时,会创建使用父类变量的子类的单个对象。 this is my understanding.这是我的理解。 I have read on many places that super refer to parent class object but here only one single object is created which inherits its members from parent class.我在很多地方都读过 super 引用父类对象,但这里只创建了一个从父类继承其成员的对象。 Does a parent class object is implicitly created inside child one to which super refers to?.I dont exactly get how people have written and in which context that it refer to parent class object ?父类对象是否是在 super 所指的子类中隐式创建的?我不完全了解人们是如何编写的以及它在哪种上下文中引用父类对象? when only child class object is created singly.当仅单独创建子类对象时。

Have a look there for super meaning and use.看看那里的超级含义和用途。 https://docs.oracle.com/javase/tutorial/java/IandI/super.html https://docs.oracle.com/javase/tutorial/java/IandI/super.html

Basically, you can see any object as an onion with the inner peel being Object and the outer one your object's class, with all the super classes in between.基本上,您可以将任何对象视为洋葱,内皮是 Object,外皮是您的对象的类,所有超类都在它们之间。 Each peel comes with its own attributes, class (overloaded, overridden, or new) and constructors (same) but the whole thing is a single object, each peel only seeing toward the inner core (except for overriden methods that are called to their outmost overriding peel - ie a method implementation that does not exist yet at the the considered peel declaration : see https://pretagteam.com/question/calling-an-overridden-method-superclass-an-calls-overridden-method )每个果皮都有自己的属性、类(重载、覆盖或新的)和构造函数(相同)但整个事物是一个对象,每个果皮只看到内核(除了被调用到最外层的覆盖方法)覆盖果皮 - 即在所考虑的果皮声明中尚不存在的方法实现:请参阅https://pretagteam.com/question/calling-an-overridden-method-superclass-an-calls-overridden-method

  1. If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super.如果您的方法覆盖了其超类的方法之一,则可以通过使用关键字 super 来调用被覆盖的方法。
  1. You can also use super to refer to a hidden field (although hiding fields is discouraged).您还可以使用 super 来引用隐藏字段(尽管不鼓励隐藏字段)。
  1. You can also use super(...) to invoke a specific superclass constructor (whether it is not public or need to be called with specific parameters)您还可以使用 super(...) 来调用特定的超类构造函数(无论它是不是公共的还是需要使用特定参数调用)

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

相关问题 如果我们创建子类的对象,是否会创建父类的对象? - will there be object creation of parent class if we create an object of child class? 为什么不能引用子类对象引用父类对象? - Why can't reference to child Class object refer to the parent Class object? 我们可以创建一个不能从类中调用任何方法的类(超类或子类)的对象吗? - Can we create an object of a class (super or sub class) which cannot invoke any methods from the class? Object类如何成为超类的子类? - How can the Object class be a super class of subclasses? 如何使用子类引用使用父关键字调用父类方法以创建对象? - How to call a parent class method using child class reference using super keyword for object creation? 子类可以创建抽象的超类类型的对象吗? - Can a child class creates object of type super class that is abstract? 创建子类对象时父类和子类如何混合 - how parent and child classes mix when creating a child class object PHP-如果我们在php中创建子类的对象,它会调用父类构造函数,然后调用子类构造函数(如java)吗? - PHP -If we create object of child class in php, does it call parent class constructor and then child class constructor like java? 我们如何在java中创建类Object类 - How can we create a class like Object class in java 我如何引用另一个类的对象 - how can I refer to an object of another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM