inheritance

How does Attribute Inheritance in C++ Work?

丶灬走出姿态 提交于 2020-05-29 11:37:47
问题 Suppose you wanted to reproduce the following Python snippet: class Base: name = "base" def announce(self): print(self.name) class Derived(Base): name = "derived" Base().announce() Derived().announce() ... which would output: "base" "derived" Initially, you may be inclined to write something like the following: #include <iostream> #include <string> struct Base { std::string name = "base"; void announce() { std::cout << name << std::endl; } }; struct Derived : public Base { std::string name =

Is there a way to call a method on definition of a subclass in Python?

隐身守侯 提交于 2020-05-29 05:17:28
问题 The __init__ method defines what is done on creating an instance of a class. Can I do something equivalent when a subclass is created? Let's say I have the abstract class Entity : class Entity: def __onsubclasscreation__(cls): for var in cls.__annotations__: cls.__dict__[var] = property(lambda self:self.vars[var]) This would mean that whenever I define a new class inheriting from Entity , all annotated variables of that class would receive a getter: class Train(Entity): wagons: int color: str

Specify implement interface on derived class when base class implements it abtract

…衆ロ難τιáo~ 提交于 2020-05-29 05:16:11
问题 I was wondering if there are any (runtime) difference if I implement an interface on a derived class if the base abstract class implements it already abstract: public interface IFoo { void Bar(); } public abstract class Base : IFoo { public abstract void Bar(); } public class Derived : Base, IFoo // does this make any difference? { public override void Bar() { // do something } } Is there any difference writing the "implemention" IFoo on Derived for example when I check if an instance

Is there a way to call a method on definition of a subclass in Python?

天涯浪子 提交于 2020-05-29 05:15:29
问题 The __init__ method defines what is done on creating an instance of a class. Can I do something equivalent when a subclass is created? Let's say I have the abstract class Entity : class Entity: def __onsubclasscreation__(cls): for var in cls.__annotations__: cls.__dict__[var] = property(lambda self:self.vars[var]) This would mean that whenever I define a new class inheriting from Entity , all annotated variables of that class would receive a getter: class Train(Entity): wagons: int color: str

C# type arguments cannot be inferred from usage in Select with multiple returns

試著忘記壹切 提交于 2020-05-28 12:45:40
问题 I don't think I'm doing anything too esoteric, but I don't see any other questions about this. The following code (I've reduced it to the essentials) generates a compiler error in C# 4. However, it should be obvious what the type argument is - there's a greatest common denominator ("class A") that is also explicitly defined in the return type of the method "Frob". Shouldn't the compiler make a list of all the return types in the lambda expression, create an ancestry tree to find their common

C# type arguments cannot be inferred from usage in Select with multiple returns

末鹿安然 提交于 2020-05-28 12:44:19
问题 I don't think I'm doing anything too esoteric, but I don't see any other questions about this. The following code (I've reduced it to the essentials) generates a compiler error in C# 4. However, it should be obvious what the type argument is - there's a greatest common denominator ("class A") that is also explicitly defined in the return type of the method "Frob". Shouldn't the compiler make a list of all the return types in the lambda expression, create an ancestry tree to find their common

C# type arguments cannot be inferred from usage in Select with multiple returns

吃可爱长大的小学妹 提交于 2020-05-28 12:44:10
问题 I don't think I'm doing anything too esoteric, but I don't see any other questions about this. The following code (I've reduced it to the essentials) generates a compiler error in C# 4. However, it should be obvious what the type argument is - there's a greatest common denominator ("class A") that is also explicitly defined in the return type of the method "Frob". Shouldn't the compiler make a list of all the return types in the lambda expression, create an ancestry tree to find their common

How can a class that inherits from a NumPy array change its own values?

℡╲_俬逩灬. 提交于 2020-05-27 06:35:10
问题 I have a simple class that inherits from the NumPy n-dimensional array. I want to have two methods of the class that can change the array values of an instance of the class. One of the methods should set the array of the class instance to the values of a list data attribute of the class instance and the other of the methods should append some list values to the array of the class instance. I'm not sure how to accomplish this, but my attempt is as follows: import numpy class Variable(numpy

Force derived class to implement interface

本秂侑毒 提交于 2020-05-27 03:26:10
问题 I am here today (like yesterday) with another weird interface question. I have a class: public class InputDevice<T> where T : Button { protected List<T> buttons = new List<T>(); protected InputDevice() { //Only allow instanciation from within a derived class } } As you can see, this class cannot be instantiated. A class that derives from it might be able to be instantiated. This is a subclass: public sealed class Keyboard : InputDevice<KeyboardButton> { public Keyboard() { buttons.Add(new

Get child class name from parent

北慕城南 提交于 2020-05-25 03:22:41
问题 I have a base class for my all of activities ( ActivityBase ) that itself derives from android.app.Activity . In onCreate I want to execute some conditional logic based on the currently executing child class. If SomeCustomActivity and AnotherCustomActivity both extend ActivityBase , how can I determine in the parent class ( ActivityBase ) which of the two is the currently executing one? 回答1: Use instanceof operator. Supposing you have a base class and two subclasses named Base , SubOne and