inheritance

Upcasting/Downcasting in Java

痞子三分冷 提交于 2020-02-14 02:32:30
问题 I am trying to understand upcasting and downcasting in Java and I am confused by the following scenario (about my code, which is below): First - why is it that the code does not compile when I include the line myAnimal.bark(); , and Second - (assuming I comment out myAnimal.bark(); ) why does calling myAnimal.move() print "moveDog" instead of "moveAnimal" ? Isn't myAnimal restricted to methods from the Animal class because we have declared its type to be Animal , even though we are setting it

Swift protocol specializing generic protocol

流过昼夜 提交于 2020-02-08 07:34:12
问题 Is it possible to have a protocol that specializes a generic protocol? I want something like this: protocol Protocol: RawRepresentable { typealias RawValue = Int ... } This does compile, but when I try to access the init or rawValue from a Protocol instance, its type is RawValue instead of Int . 回答1: In Swift 4, you can add constraints to your protocol: protocol MyProtocol: RawRepresentable where RawValue == Int { } And now all methods defined on MyProtocol will have an Int rawValue. For

Java: Alternative to Multiple inheritance

点点圈 提交于 2020-02-07 05:48:04
问题 I have a class A that has to extend class B and C which are both provided by 3rd party and I don't have a control of them. Since Java doesn't support multiple inheritance. Using interface wouldn't help in my case since I do want to inherit the base classes' implementation and I don't want to copy their code over to my class, or I'll need to be aware of all their coming changes. What would be the best way to accomplish this need? All suggestion will be much appreciated! 回答1: class A class B2

C++ allow derived classes of friend to have access to private nested class

空扰寡人 提交于 2020-02-06 07:30:06
问题 Here's what I'm trying to do: class A { friend class C (and all of C's derived classes) public: void DoAThing() { mpMyC->DelegateResponsibility(myB); } private: class B { }; B mMyB; C* mpMyC; }; class C { // No problem here- C can see B virtual void DelegateResponsibility(const A::B& necessaryInfo); }; class D: public C { // Uh-oh- we don't inherit friendship virtual void DelegateResonsibility(const A::B& necessaryInfo); }; In short, I have a private nested class inside A because it's an

inheritance of private members in c#

最后都变了- 提交于 2020-02-05 13:16:14
问题 Are private members inherited when inheriting the class in c#? I have read some topic related to this, somebody telling that private members are inherited but cannot access the private members, somebody telling that it is not inherited when inheriting the class. Please explain the concept. if it is inheriting can any body give an explanation? thanks 回答1: If I understand your question correctly then you're not concerned about the accessibility you are only concerned about private members are

Polymorphism in Java and creating objects from these classes

烂漫一生 提交于 2020-02-04 02:02:43
问题 I'm a little confused exactly about how polymorphism and extending classes etc... works in Java when creating objects from these classes. I recently had a problem which someone on here helped me resolved (see Not able to access object sub class's variable? (Java / Android)) for background. I was attempting to create an object like so: Quad hero = new Hero(); Where Hero was a subclass of Quad(); I had variables in my Hero class which I wasn't above to access. The solution was to change my

Python subclassing a class with custom __new__

微笑、不失礼 提交于 2020-02-03 23:41:13
问题 There's a class (not created by me, from a 3rd party library) that has no __init__ declared (other than object 's __init__ ), and this is its __new__ : def __new__(cls, uid): self = super().__new__(cls, uid) self._info = get_info_from_uid(uid) return self I can't see why didn't they just use __init__ here, but they didn't. Now I'd like to subclass this and give it an additional attribute; before I checked the source-code of the class (only the documentation), I thought it's just using __init_

Python subclassing a class with custom __new__

非 Y 不嫁゛ 提交于 2020-02-03 23:41:10
问题 There's a class (not created by me, from a 3rd party library) that has no __init__ declared (other than object 's __init__ ), and this is its __new__ : def __new__(cls, uid): self = super().__new__(cls, uid) self._info = get_info_from_uid(uid) return self I can't see why didn't they just use __init__ here, but they didn't. Now I'd like to subclass this and give it an additional attribute; before I checked the source-code of the class (only the documentation), I thought it's just using __init_

Python subclass method to inherit decorator from superclass method

你说的曾经没有我的故事 提交于 2020-02-03 19:52:39
问题 I have a superclass that has a retrieve() method, and its subclasses each implement their own retrieve() method. I'd like every retrieve() method to be decorated to cache the return value when it receive the same args, without having to decorate the method in every subclass. Decorators don't seem to be inherited. I could probably call the superclass's method which would in turn set the cache, but currently my superclass raises a NotImplemented exception, which I like. import json import

Python subclass method to inherit decorator from superclass method

江枫思渺然 提交于 2020-02-03 19:50:03
问题 I have a superclass that has a retrieve() method, and its subclasses each implement their own retrieve() method. I'd like every retrieve() method to be decorated to cache the return value when it receive the same args, without having to decorate the method in every subclass. Decorators don't seem to be inherited. I could probably call the superclass's method which would in turn set the cache, but currently my superclass raises a NotImplemented exception, which I like. import json import