super

Why should I call super() in Java?

自闭症网瘾萝莉.ら 提交于 2019-12-20 03:07:32
问题 I see an example from a book that I read about java: public class A{ public A(){ System.out.println("A"); } } public class B extends A{ public B(){ super(); System.out.println("B"); } public static void main(String[] args){ B b = new B(); } } I can't understand why should super() be here? Even if I delete super() , I would get the same result (A would be printed, and then B). As I understand, when I initialize the subclass, then the parent class is initialize before it. So why use super() ?

Why [[HomeObject]] is different in shorthand syntax of method?

可紊 提交于 2019-12-19 03:43:30
问题 This question is derived from super keyword unexpected here The accepted answer says: Because super is only valid inside methods. But in MDN, it seems these two are both methods: let person = { greeting() { return "Hello"; } }; let friend = { // shorter syntax for method? greeting() { return super.greeting() + ", hi!"; } // method? // greeting: function() { // return super.greeting() + ", hi!"; // Throw error: Uncaught SyntaxError: 'super' keyword unexpected here // } }; Object.setPrototypeOf

Use of 'super' keyword when accessing non-overridden superclass methods

孤街浪徒 提交于 2019-12-19 02:04:08
问题 I'm trying to get the hang of inheritance in Java and have learnt that when overriding methods (and hiding fields) in sub classes, they can still be accessed from the super class by using the 'super' keyword. What I want to know is, should the 'super' keyword be used for non-overridden methods? Is there any difference (for non-overridden methods / non-hidden fields)? I've put together an example below. public class Vehicle { private int tyreCost; public Vehicle(int tyreCost) { this.tyreCost =

How can I use super() with one argument in python

一个人想着一个人 提交于 2019-12-18 14:09:45
问题 While reading about the super() object in Python, I read the following statement: If the second argument is omitted, the super object returned is unbound What does this exactly mean and how do I use super() with one argument in code? 回答1: Python function objects are descriptors, and Python uses the descriptor protocol to bind functions to an instance. This process produces a bound method . Binding is what makes the 'magic' self argument appear when you call a method, and what makes a property

what does super() do without any arguments?

梦想与她 提交于 2019-12-18 11:08:46
问题 I'm learning react from the docs, but not sure what the super() does in this example. Usually, doesn't it take the arguments that are passed to making a new instance and then calls React.Component's constructor method to incorporate these arguments into the instance? What does it do without any arguments? class LikeButton extends React.Component { constructor() { super(); this.state = { liked: false }; this.handleClick = this.handleClick.bind(this); } handleClick() { this.setState({liked:

How does multiple inheritance work with the super() and different __init__() arguments?

空扰寡人 提交于 2019-12-18 10:23:15
问题 I'm just diving into some more advanced python subjects (well, advanced to me at least). I am now reading about multiple inheritance and how you can use super(). I more or less understand the way the super function is used, but (1) What's wrong with just doing it like this ?: class First(object): def __init__(self): print "first" class Second(object): def __init__(self): print "second" class Third(First, Second): def __init__(self): First.__init__(self) Second.__init__(self) print "that's it"

Java How to call method of grand parents? [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-18 03:50:55
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why is super.super.method(); not allowed in Java? Let's assume I have 3 classes A , B and C , each one extending the previous one. How do I call the code in A.myMethod() from C.myMethod() if B also implements myMethod ? class A { public void myMethod() { // some stuff for A } } class B extends A { public void myMethod() { // some stuff for B //and than calling A stuff super.myMethod(); } } class C extends B {

React, why use super(props) inside of ES6 class constructor? [duplicate]

℡╲_俬逩灬. 提交于 2019-12-17 22:21:58
问题 This question already has answers here : What's the difference between “super()” and “super(props)” in React when using es6 classes? (10 answers) Closed 3 years ago . I realize the super keyword can be used to call functions in a parent component. However, I'm not totally clear why you would use the super keyword in the example below - just passing it whatever props are being passed to the constructor. Can someone please shed some light on the various reasons for using the super keyword in an

Force base method call

那年仲夏 提交于 2019-12-17 19:56:53
问题 Is there a construct in Java or C# that forces inheriting classes to call the base implementation? You can call super() or base() but is it possible to have it throw a compile-time error if it isn't called? That would be very convenient.. --edit-- I am mainly curious about overriding methods. 回答1: There isn't and shouldn't be anything to do that. The closest thing I can think of off hand if something like having this in the base class: public virtual void BeforeFoo(){} public void Foo() {

super() in constructor

怎甘沉沦 提交于 2019-12-17 18:03:21
问题 I'm reading through some code. In the constructor it has super() but the class implements interface which of course doesn't have a constructor. So which super() it is referring to? public class BoundingBox implements IBoundingVolume { public BoundingBox() { super(); mTransformedMin = new Number3D(); mTransformedMax = new Number3D(); mTmpMin = new Number3D(); mTmpMax = new Number3D(); mPoints = new Number3D[8]; mTmp = new Number3D[8]; mMin = new Number3D(); mMax = new Number3D(); for(int i=0;