polymorphism

Polymorphism and overloading with static methods in C#.

匆匆过客 提交于 2019-12-21 09:38:08
问题 I have been trying to generate a Factory supposed to return a different object of a common interface (say Item ) according to the input parameter (I call it a context) of the function getItem(A context) Now, assume I define a new type of context: B which inherits from A . I wanted to return a different item depending on whether the object passed to the factory was of class B or A . I tried to do as follows (overloading the method): class Factory { static Item getItem(A context) {...} static

Why is void not covariant in Java?

可紊 提交于 2019-12-21 09:14:55
问题 If I have this interface: public interface Foo { void bar(); } Why can't I implement it like this? public class FooImpl implements Foo { @Override public Object bar() { return new Object(); } } It seems like void should be covariant with everything. Am I missing something? Edit: I should have been clearer that I'm looking for the design justification, not the technical reason that it won't compile. Are there negative consequences to making void covariant to everything? 回答1: Technically void

C++ polymorphism of a object in an array

半城伤御伤魂 提交于 2019-12-21 07:57:26
问题 I'm an embedded software engineer and coming from the world of bits and C. In that world, there are data in flash memory represented by const in C. And there are data in RAM. RAMs are expensive and limited, while flash memory is cheap and enough. Also, dynamic memory allocation using new, delete, malloc etc is not allowed due to fragmentation problem or safety regulations, static designs are preferred. I've around 2000 objects which have similar constant properties but different behaviors. So

Return-type polymorphism for pass-by-value

岁酱吖の 提交于 2019-12-21 07:38:07
问题 I'm not sure if the question title is accurate... Let me start by explaining my original simple scenario, and then move on to explain what would I like to do, but can't. Originally, I had something like: class Operand; Operand genOperandA() { ...; return Operand(); } Operand genOperandB() { ...; return Operand(); } ... // more operand-generation functions typedef Operand (*OpGen)(); // Table of function pointers static const OpGen generators[] = { genOperandA, genOperandB, ... }; // Function

Django - Polymorphic Models or one big Model?

此生再无相见时 提交于 2019-12-21 07:26:28
问题 I'm currently working on a model in Django involving one model that can have a variety of different traits depending on what kind of object it is. So, let's say we have a model called Mammal, which can either be an Elephant or a Dolphin (with their own traits "tusk_length" and "flipper_length" respectively). Basic OOP principles shout "polymorphism", and I'm inclined to agree. But, as I'm new to Django, I first want to know whether or not it is the best way to do so in Django. I've heard of

Django - Polymorphic Models or one big Model?

守給你的承諾、 提交于 2019-12-21 07:26:09
问题 I'm currently working on a model in Django involving one model that can have a variety of different traits depending on what kind of object it is. So, let's say we have a model called Mammal, which can either be an Elephant or a Dolphin (with their own traits "tusk_length" and "flipper_length" respectively). Basic OOP principles shout "polymorphism", and I'm inclined to agree. But, as I'm new to Django, I first want to know whether or not it is the best way to do so in Django. I've heard of

Effects of monomorphism restriction on type class constraints

匆匆过客 提交于 2019-12-21 06:59:33
问题 This code breaks when a type declaration for baz is added: baz (x:y:_) = x == y baz [_] = baz [] baz [] = False A common explanation (see Why can't I declare the inferred type? for an example) is that it's because of polymorphic recursion. But that explanation doesn't explain why the effect disappears with another polymorphically recursive example: foo f (x:y:_) = f x y foo f [_] = foo f [] foo f [] = False It also doesn't explain why GHC thinks the recursion is monomorphic without type

Multiple Method Signatures For A Single Abstract Function/Abstract Overloading

▼魔方 西西 提交于 2019-12-21 05:46:06
问题 I have an abstract class for moving data from one database to another, and sometimes the data required to create the basic entries is different, due to the presence of a legacy table in the destination database which includes instructions for locating the data in the source. Obviously simplified, here is where the problem comes into play: abstract class foo { protected abstract function createBaseEntry($id); } Sometimes, I only need the one ID passed to this function, but in some cases I need

How do I declare an overloaded operator in an abstract class and override it in a derived non-abstract class?

扶醉桌前 提交于 2019-12-21 05:19:07
问题 I'm trying to write an abstract class with some pure virtual binary operators, which should be implemented by the derived class in order to accomplish operator polymorphism. Here's a simplified example: class Base { public: virtual const Base& operator+ (const Base&) const = 0; }; class Derived : public Base { public: const Derived& operator+ (const Derived&) const; }; const Derived& Derived::operator+ (const Derived& rvalue) const { return Derived(); } It doesn't matter right now what the

Learning C++: returning references AND getting around slicing

醉酒当歌 提交于 2019-12-21 04:04:08
问题 I'm having a devil of a time understanding references. Consider the following code: class Animal { public: virtual void makeSound() {cout << "rawr" << endl;} }; class Dog : public Animal { public: virtual void makeSound() {cout << "bark" << endl;} }; Animal* pFunc() { return new Dog(); } Animal& rFunc() { return *(new Dog()); } Animal vFunc() { return Dog(); } int main() { Animal* p = pFunc(); p->makeSound(); Animal& r1 = rFunc(); r1.makeSound(); Animal r2 = rFunc(); r2.makeSound(); Animal v