polymorphism

Entity Framework Polymorphic associations

五迷三道 提交于 2019-12-29 04:36:07
问题 I'm going to use Entity Framework soon for a Booking System ( made from scratch ). I have been doing a few Prototypes, trying to figure out what I want to do before the project is started (I'm still discussing requirements etc. with the client). Consider this case: I have a Booking, and a booking can have associated Ressources, that can be booked, but these ressource can be different, and have different Fields etc. I have never really used EF before, so I don't know how I can achieve this

The inheritance of attributes using __init__

你。 提交于 2019-12-29 02:47:49
问题 I'm Java person who just started learning Python. Take this example: class Person(): def __init__(self, name, phone): self.name = name self.phone = phone class Teenager(Person): def __init__(self, name, phone, website): self.name=name self.phone=phone self.website=website I'm sure there's a lot of redundant code (I know in Java, there are a lot of redundancies for the bit of code above). Which parts are redundant with respect to which attributes are already inherited from the parent class?

virtual function call from base class

不想你离开。 提交于 2019-12-29 02:45:10
问题 Say we have: Class Base { virtual void f(){g();}; virtual void g(){//Do some Base related code;} }; Class Derived : public Base { virtual void f(){Base::f();}; virtual void g(){//Do some Derived related code}; }; int main() { Base *pBase = new Derived; pBase->f(); return 0; } Which g() will be called from Base::f() ? Base::g() or Derived::g() ? Thanks... 回答1: The g of the derived class will be called. If you want to call the function in the base, call Base::g(); instead. If you want to call

Using the 'new' modifier in C#

只谈情不闲聊 提交于 2019-12-28 20:35:23
问题 I read that the new modifer hides the base class method. using System; class A { public void Y() { Console.WriteLine("A.Y"); } } class B : A { public new void Y() { // This method HIDES A.Y. // It is only called through the B type reference. Console.WriteLine("B.Y"); } } class Program { static void Main() { A ref1 = new A(); // Different new A ref2 = new B(); // Polymorpishm B ref3 = new B(); ref1.Y(); ref2.Y(); //Produces A.Y line #xx ref3.Y(); } } Why does ref2.Y(); produce A.Y as output?

list of polymorphic objects

帅比萌擦擦* 提交于 2019-12-28 18:44:21
问题 I have a particular scenario below. The code below should print 'say()' function of B and C class and print 'B says..' and 'C says...' but it doesn't .Any ideas.. I am learning polymorphism so also have commented few questions related to it on the lines of code below. class A { public: // A() {} virtual void say() { std::cout << "Said IT ! " << std::endl; } virtual ~A(); //why virtual destructor ? }; void methodCall() // does it matters if the inherited class from A is in this method { class

Accessing private instance variables of parent from child class?

时间秒杀一切 提交于 2019-12-28 11:52:07
问题 Let's say we have a class foo which has a private instance variable bar . Now let us have another class, baz , which extends foo . Can non-static methods in baz access foo 's variable bar if there is no accessor method defined in foo ? I'm working in Java, by the way. 回答1: No, not according to the java language specification, 3rd edition: 6.6.8 Example: private Fields, Methods, and Constructors A private class member or constructor is accessible only within the body of the top level class (§7

why polymorphism doesn't treat generic collections and plain arrays the same way?

好久不见. 提交于 2019-12-28 10:06:52
问题 assume that class Dog extends class Animal: why this polymorphic statement is not allowed: List<Animal> myList = new ArrayList<Dog>(); However, it's allowed with plain arrays: Animal[] x=new Dog[3]; 回答1: The reasons for this are based on how Java implements generics. An Arrays Example With arrays you can do this (arrays are covariant as others have explained) Integer[] myInts = {1,2,3,4}; Number[] myNumber = myInts; But, what would happen if you try to do this? Number[0] = 3.14; //attempt of

Polymorphism in Overloaded and Overridden Methods

假如想象 提交于 2019-12-28 07:01:49
问题 Let's take this simple Java code: public class Animal { public void eat() { System.out.println("Generic Animal Eating Generically"); } } public class Horse extends Animal { public void eat() { System.out.println("Horse eating hay "); } public void eat(String s) { System.out.println("Horse eating " + s); } } I'm trying to figure out which version of the three eat() methods will run. Now, when I type Animal a = new Animal(); a.eat(); The output is "Generic Animal Eating Generically", which is

C++ abstract class without pure virtual functions?

烂漫一生 提交于 2019-12-28 06:30:09
问题 I have a base class class ShapeF { public: ShapeF(); virtual ~ShapeF(); inline void SetPosition(const Vector2& inPosition) { mPosition.Set(inPosition); } protected: Vector2 mPosition; } Obviously with some ommitied code, but you get the point. I use this as a template, and with some fun (ommited) enums, a way to determine what kind of shape i'm using class RotatedRectangleF : public ShapeF { public: RotatedRectangleF(); virtual ~RotatedRectangleF(); protected: float mWidth; float mHeight;

How are C# Generics implemented?

筅森魡賤 提交于 2019-12-28 05:35:34
问题 I had thought that Generics in C# were implemented such that a new class/method/what-have-you was generated, either at run-time or compile-time, when a new generic type was used, similar to C++ templates (which I've never actually looked into and I very well could be wrong, about which I'd gladly accept correction). But in my coding I came up with an exact counterexample: static class Program { static void Main() { Test testVar = new Test(); GenericTest<Test> genericTest = new GenericTest