polymorphism

Why assign a subclass object to a superclass reference? [duplicate]

橙三吉。 提交于 2019-12-12 01:43:56
问题 This question already has answers here : What does it mean to “program to an interface”? (31 answers) Closed 5 years ago . Animal is a superclass Feline is a subclass of Animal Cat is a subclass of Feline Canine is a sublcass of Animal Dog is a subclass of Canine All of the classes have their own eat() method that outputs: "(class) is eating" I've already tried creating an array of Animals, looping through them, and calling the eat() method, which outputs the proper output for each given

Java - Protected Modifier with Interface

爷,独闯天下 提交于 2019-12-12 01:17:24
问题 public interface ICalculator { public double multi(double a, double b); public int add(int a, int b); public int sub(int a, int b); } public class Calculator extends ICalculator { protected int add(double a, double b) { return a+b; } public double sub(int zahl1, int zahl2 ) { return a*b; } } why i can't use in the class Calculator a protected method ? My answer is that "protected" is it useful in the same class and in the subclasses . Well couldn't i also think that a method in an implemented

Is passing around value objects using polymorphism a bad practice?

和自甴很熟 提交于 2019-12-12 01:07:39
问题 public interface IRecord { } public class BirthRecord : IRecord { public string CityOfBirth; public Date DateOfBirth; public BirthRecord(string cityOfBirth, Date dateOfBirth) { // assign these to properties } } public class CarRecord : IRecord { public Color Color; public string Manufacturer; public CarRecord(Color color, string manufacturer) { // assign these to properties } } public interface IAccount { public List<IRecord> Records { get; set; } } public class Client { public void

SQLAlchemy Class Decoration for __mapper_args__

非 Y 不嫁゛ 提交于 2019-12-12 00:17:54
问题 I have a lot of classes which are part of polymorphism with my DB schema. With most of them I (need to) do: __mapper_args__ = {'polymorphic_identity': unique_integer} # unique_integer is a unique integer different for each class ex: 10 Instead of this, I'd like to use a decorator, ie.: @polid(10) class ClassName(Inherited): # instead of repeating for each class the following: # __mapper_args__ = {'polymorphic_identity': 10} # I would like to have above decorator or something to do the trick.

Ambiguity in static polymorphism [duplicate]

本小妞迷上赌 提交于 2019-12-12 00:13:24
问题 This question already has answers here : name hiding and fragile base problem (3 answers) Closed 4 years ago . Quite a simple question actually but one which is bugging me a lot and I'm unable to find a sound answer for it. How is the function call d.show(); NOT ambiguous for the following code AND why does b->show(); after b = &d; leads to the calling of Base_::show() ? #include <iostream> using std::cout; class Base_ { public: void show() { cout<<"\nBase Show"; } }; class Derived_ : public

Chaining, returning base objects, and type mismatch to extended classes

为君一笑 提交于 2019-12-11 20:23:52
问题 I have come across a class like this. It holds a "with" method that lets people chain things together. public class Bear { protected List<String> names = new ArrayList<String>(); protected List<String> foods = new ArrayList<String>(); public Bear withName(String name) { names.add(name); return this; } public Bear withFood(String food) { foods.add(food); return this; } } // ...somewhere else Bear b = new Bear().withName("jake").withName("fish"); I found two classes that shared 90% of the same

Controller Action with Derived Classes

僤鯓⒐⒋嵵緔 提交于 2019-12-11 19:56:20
问题 I have one base class and two derived classes: public class UserModel { public int Id {get; set; } public string Name {get; set; } public UserType UserType {get; set;} } public class StudentModel : UserModel { public string StudentProperty {get; set;} } public class TeacherModel : UserModel { public string TeacherProperty {get; set;} } In my controller ProfileController.cs I have the following two actions: public virtual ActionResult Detail(int id) { var userModel = _userService.Get(id);

Pass a pointer to a derived class pointer as a parameter to a constructor expecting a pointer to a base class pointer

你离开我真会死。 提交于 2019-12-11 19:27:43
问题 I've read Passing pointer to derived class, to function that expects pointer to base class? but the answer doesn't seem applicable to my issue. I checked via Ideone that the following compiles, as it should: class Base { public: Base() {} virtual ~Base() {} }; class Derived : public Base { public: Derived() {} virtual ~Derived() {} }; class Manager { public: Manager(Base* b) {} ~Manager() {} private: Manager() {} }; int main() { Derived* d = new Derived; Manager* m = new Manager(d); return 0;

Polymorphism with Serialized Data

谁都会走 提交于 2019-12-11 19:16:37
问题 I am trying to use Polymorphism with data that is to be serialized and deserialized. However, it seems that the polymorphic properties are being lost on serialization: // Serialize [System.Serializable] public ParentClass() { public string someString = "a string"; } [System.Serializable] public ChildClass() : ParentClass { public int someInt; public ChildClass(int _someInt) { someInt = _someInt; } } List<ParentClass> list = new List<ParentClass>(); list.Add(new ChildClass(5)); // Data is

Detect whether a method has been overridden

妖精的绣舞 提交于 2019-12-11 19:05:32
问题 Question Given a C++ base class pointer, is there a way to detect whether a certain virtual method has been overridden? Background I'm writing an interpreter for a little language. I have a base class to represent values with specializations for the various value types. Here's a simplified example: class Value { public: virtual bool CanBeString() const { return false; } virtual std::string GetAsString() const { throw std::logic_error("Value cannot be represented as a string."); } virtual bool