inheritance

Generics and inheritance: need a complex Map instance

て烟熏妆下的殇ゞ 提交于 2020-01-09 07:58:07
问题 public abstract class Mother { } public class Daughter extends Mother { } public class Son extends Mother { } I need a Map which keys are one of Daughter or Son classes, and which values are lists of objects of one of those two classes, respectively . For example: /* 1. */ map.put(Daughter.class, new ArrayList<Daughter>()); // should compile /* 2. */ map.put(Son.class, new ArrayList<Son>()); // should compile /* 3. */ map.put(Daughter.class, new ArrayList<Son>()); // should not compile /* 4.

Import only a class static method

痞子三分冷 提交于 2020-01-09 07:53:26
问题 I have the following decorator in a base class: class BaseTests(TestCase): @staticmethod def check_time(self, fn): @wraps(fn) def test_wrapper(*args,**kwargs): # do checks ... return test_wrapper And the following class inheriting from BaseTests: from path.base_posting import BaseTests from path.base_posting.BaseTests import check_time # THIS LINE DOES NOT WORK! class SpecificTest(BaseTests): @check_time # use the decorator def test_post(self): # do testing ... I would like to use the

Why does Java bind variables at compile time?

这一生的挚爱 提交于 2020-01-09 06:05:54
问题 Consider the following example code class MyClass { public String var = "base"; public void printVar() { System.out.println(var); } } class MyDerivedClass extends MyClass { public String var = "derived"; public void printVar() { System.out.println(var); } } public class Binding { public static void main(String[] args) { MyClass base = new MyClass(); MyClass derived = new MyDerivedClass(); System.out.println(base.var); System.out.println(derived.var); base.printVar(); derived.printVar(); } }

Why does Java bind variables at compile time?

冷暖自知 提交于 2020-01-09 06:05:29
问题 Consider the following example code class MyClass { public String var = "base"; public void printVar() { System.out.println(var); } } class MyDerivedClass extends MyClass { public String var = "derived"; public void printVar() { System.out.println(var); } } public class Binding { public static void main(String[] args) { MyClass base = new MyClass(); MyClass derived = new MyDerivedClass(); System.out.println(base.var); System.out.println(derived.var); base.printVar(); derived.printVar(); } }

Django model inheritance: create sub-instance of existing instance (downcast)?

谁说胖子不能爱 提交于 2020-01-08 19:43:11
问题 I'm trying to integrate a 3rd party Django app that made the unfortunate decision to inherit from django.contrib.auth.models.User , which is a big no-no for pluggable apps. Quoting Malcolm Tredinnick: More importantly, though, just as in Python you cannot "downcast" with Django's model inheritance. That is, if you've already created the User instance, you cannot, without poking about under the covers, make that instance correspond to a subclass instance that you haven't created yet. Well, I'm

Favor composition over inheritance [duplicate]

穿精又带淫゛_ 提交于 2020-01-08 16:51:28
问题 This question already has answers here : Prefer composition over inheritance? (32 answers) Closed 6 years ago . Favor composition over inheritance is very popular phrase. I read several articles and at the end each article says use inheritance when there is pure IS-A relationship between classes. An example from this article: Here between Apple and Fruit there is clear IS-A relationship i.e Apple IS-A Fruit, yet the author has also shown it as Apple HAS-A Fruit (composition) to show the

Why do attribute references act like this with Python inheritance? [duplicate]

感情迁移 提交于 2020-01-08 12:28:12
问题 This question already has answers here : How to avoid having class data shared among instances? (7 answers) Closed last year . The following seems strange.. Basically, the somedata attribute seems shared between all the classes that inherited from the_base_class . class the_base_class: somedata = {} somedata['was_false_in_base'] = False class subclassthing(the_base_class): def __init__(self): print self.somedata first = subclassthing() {'was_false_in_base': False} first.somedata['was_false_in

Pointer will randomly print it's address instead of it's pointed value, C++

烂漫一生 提交于 2020-01-08 03:06:07
问题 So I have this problem where the output prints the address of my pointer, I have no idea why this happens cuz the pointers is not modified at all Heres the code: using namespace std; class AndroideAbstracto { protected: int *vida; int *fuerza; int *velocidad; public: void setvalores(int vi, int fu, int ve) { velocidad = &ve; vida = &vi; fuerza = &fu; }; virtual void imprimir(void) = 0; }; class Androide : public AndroideAbstracto { public: void imprimir() { std::cout << "Caracteristicas del

Unity Interception in Derived Class

我怕爱的太早我们不能终老 提交于 2020-01-07 07:21:03
问题 I've got a situation where policy injection no longer works when I'm using a derived class. The classes involved look like this (basically an interface, an abstract base class, and an implementation class): public interface IRepository<T> { void Create(T iItem); } public abstract class ElmtRepository<T> : IRepository<T> { protected List<T> Items { get; set; } public ElmtRepository() { Items = new List<T>(); } public void Create(T iItem) { Items.Add(iItem); } } public class AcctPgmRepository :

Subclass-specific static member data

家住魔仙堡 提交于 2020-01-07 05:58:25
问题 I'm trying to implement a family of classes who keep track of how many instances exist on a per-class basis. Because all of these classes have this behavior, I'd like to pull it out into a single superclass so I don't have to repeat the implementation with each class. Consider the following code: class Base { protected static int _instances=0; protected int _id; protected Base() { // I would really like to use the instances of this's class--not // specifically Base._instances this._id = Base.