polymorphism

Decorator pattern wasting memory

こ雲淡風輕ζ 提交于 2019-12-13 17:56:43
问题 I have this base class having the following interface: abstract class Base { abstract public object Val { get; } } For any derived classes, Val 's value must be specified at object creation time. The question is: How can I make a derived class do this (hopefully at compile time)? I tried adding a constructor: abstract class Base { public Base(object value) { val = value; } private object val; ... } But as you can see then I had to declare a private field to store value in it (because Value is

C++ Polymorphic Circular depenencies with double dispatch

送分小仙女□ 提交于 2019-12-13 17:40:29
问题 So, I'm having a big issue with circular dependency. My Square.h and Circle.h classes both inherit Shape.h, and use double dispatch in order to try and detect collision between the two. My classes are currently setup in the following sort of fashion Shape.h class Shape { public: virtual bool detectCollision(Shape* obj) = 0; virtual bool detectCollision(Square* obj) = 0; virtual bool detectCollision(Circle* obj) = 0; Square.h #include "shape.h" class Square : public Shape { public: bool

Elaborating on Polymorphism

﹥>﹥吖頭↗ 提交于 2019-12-13 16:44:02
问题 I have read many questions on stackoverflow about polymorphism. The question is a question of structuring my ideas because I have read and researched a lot to the point where I have many ideas in mind. I need comments and criticism about my thoughts on polymorphism. I will use Java to demonstrate my ideas. Polymorphism is a characteristic of having different forms when talking about a certain entity. It could be achieved in the following ways: A derived class inherits from its base class all

Deserializing JSON to a .net base class with JSON.net

痞子三分冷 提交于 2019-12-13 16:14:58
问题 I am trying to deserialize GeoJSON using the JSON.net library. The geometry component of each feature can be of many different types based on the "type" attribute value. I need to deserialize the geometry component of this GeoJSON into a geometry object model like so: public abstract class Geometry { ... } public class Point : Geometry { ... } public class LineString : Geometry { ... } public class Polygon : Geometry { ... } So based on the value of the "type" attribute, it will deserialize

How to prevent compiler from choosing the least generic type?

。_饼干妹妹 提交于 2019-12-13 16:06:25
问题 I have a method that looks up some storage for an instance of a particular class: def lookup[T >: Null: ClassTag]: T = { // Print what class tag we got: System.out.println(implicitly[ClassTag[T]].toString); null; // details omitted, just return null } It works well, but the problem is that if I don't provide an explicit type, the compiler chooses Null for T , and of course it doesn't work then: def print(msg: String) = { /* ... */ } print(lookup); prints Null and of course nothing is found.

What is the advantage to declaring an object of one type as in instance of another? [duplicate]

谁都会走 提交于 2019-12-13 14:37:22
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What does Base b2 = new Child(); signify? I'm a Java beginner. I understand the concepts of class inheritance, but there's one thing I don't quite understand. I'm reading through Java for Dummies, and it's explaining polymorphism. It gives this code as an example: class Player { public void move() {... class BetterPlayer extends Player { public void move() {... public class TicTacToeApp { public static void main

Base class method being called instead of derived, even when passed by reference [duplicate]

陌路散爱 提交于 2019-12-13 14:27:51
问题 This question already has answers here : What is object slicing? (17 answers) Closed 3 years ago . I have 3 classes: A , B , and AnotherClass . Where B is derived from A : class A { public: A(){} virtual void method() {//default action} }; Then I have a derived class, B: class B : public A { public: B(){} void method() {//redefine action} }; And AnotherClass : class AnotherClass { public: AnotherClass(A& a); A a; anotherMethod(){ a.method()} }; AnotherClass :: AnotherClass(A& a) : a(a) /

How To Have A Single Interface Return Different Data Types?

血红的双手。 提交于 2019-12-13 14:24:03
问题 In a nutshell, I want to use a single interface, IProducer , to create an object, IProduct . IProduct will have different components depending on which interface created it. The IProduct class will then be used by the IConsumer interface. The correct IConsumer class should be used (I do not want to do type checking myself) based on the derived type of IProduct . I would essentially like to use the Strategy pattern (different behaviors behind a single interface), but with the added ability to

How to add a class implementing an interface to an ArrayList

我只是一个虾纸丫 提交于 2019-12-13 14:15:49
问题 To go around having to implement ALL the methods in an interface, I created an abstract class that implements an interface, then have other classes extend the abstract class and override only the needed methods. I am building an API / Framework for my app. I would like to add classes that are instances of an interface IMyInterface to an ArrayList : ArrayList<Class<IMyInterface>> classes = new ArrayList<>(); classes.add(MyClass.class); Here is MyClass class MyClass extends AbstractIMyInterface

Jackson support for polymorphism without annotations and dedicated bean fields

点点圈 提交于 2019-12-13 14:01:21
问题 Is there a way to perform serialization/deserialization in Jackson of polymorphic classes w/out using annotations or specialized bean fields? I have to support class hierarchies that I cannot modify and don't wish to use annotations. I'd like to be able to designate a synthetic name, which would not be in the classes that I am serializing/deserializing, that would be inserted into the JSON representation and used to identify the type. 回答1: If mix-ins are not to your liking, there isn't