polymorphism

how to improve this method using polymorphism+overloading so as to reduce IS (type check)?

旧城冷巷雨未停 提交于 2019-12-17 19:51:33
问题 For example BaseClass MyBase() { public int Add(BaseClass next) { if (this is InheritedA && next is InheritedA) return 1; else if (this is InheritedA && next is InheritedB) return 2; else if (this is InheritedB && next is InheritedA) return 3; else if (this is InheritedB && next is InheritedB) return 4; } } where InheritedA , and InheritedB are its inherited classes. In fact, there are more Inherited classes, and the Add returns different results based on the order and types of its operand. I

Hiding Fields in Java Inheritance

我怕爱的太早我们不能终老 提交于 2019-12-17 18:56:03
问题 Within a class, a field that has the same name as a field in the superclass hides the superclass's field. public class Test { public static void main(String[] args) { Father father = new Son(); System.out.println(father.i); //why 1? System.out.println(father.getI()); //2 System.out.println(father.j); //why 10? System.out.println(father.getJ()); //why 10? System.out.println(); Son son = new Son(); System.out.println(son.i); //2 System.out.println(son.getI()); //2 System.out.println(son.j); /

using UUID as primary key in rails and polymorph relationships

耗尽温柔 提交于 2019-12-17 18:11:28
问题 I'm creating a rails 3 application that will be decentralized and I need to use UUID as primary key for my tables, what would be the best gem, plugin for the Job. I also would like to know if it is possible to make in ActiveRecord polymorphic relationships without using the polymorphicable_type column for it, given the case that I'm using UUID. I have created a demo http://github.com/boriscy/uuidrails3 that uses UUID as keys, you should check the module UUIDHelper inside lib/ and also all the

Overriding interface method return type with derived class in implementation

混江龙づ霸主 提交于 2019-12-17 17:56:12
问题 I am trying to implement (C#) an interface method in a class, returning a derived type instead of the base type as defined in the interface: interface IFactory { BaseCar GetCar(); } class MyFactory : IFactory { MyCar GetCar() { } } Where, of course: class MyCar : BaseCar { } However, the following error happens: 'MyFactory' does not implement interface member 'IFactory.GetCar()'. 'MyFactory.BaseCar()' cannot implement IFactory.GetCar()' because it does not have the matching return type of

How to force child same virtual function call its parent virtual function first

可紊 提交于 2019-12-17 16:50:43
问题 guys, I have a case that needs the child class needs to call its parent virtual function at first before call its override virtual function. BaseClass::Draw() { } ChildClass::Draw() { BaseClass::Draw(); // BaseClass Draw must be called first. } GrandChildClass::Draw() { ChildClass::Draw(); // ChildClass Draw must be called first. } I want to hide this behavior from clients. Is there pattern on this? Thanks. 回答1: For simple cases you can use a second, private member function for the

Fortran polymorphism in pointers

谁说我不能喝 提交于 2019-12-17 16:50:43
问题 I am trying to use pointers to create links between objects. Using Fortran and here is the code piece: module base_pars_module type,abstract,public :: base_pars end type end module module test_parameters_module use base_pars_module type, extends(base_pars) :: test_pars contains procedure :: whoami end type contains function whoami(this) result(iostat) class( test_pars) :: this write(*,*) 'i am a derived type child of base_pars' end type end module module base_mask_module use base_pars module

Copy object - keep polymorphism

橙三吉。 提交于 2019-12-17 16:33:37
问题 The following code tries to copy an object and keep the original type. Unfortunately it does not work (every copied object will become a Super instead of being of the same class as its original). Please note that copySuper(const Super& givenSuper) should not know anything about the subclasses of Super . Is it possible to do such a copy? Or do I have to change the definition of copySuper ? #include <string> #include <iostream> class Super { public: Super() {}; virtual ~Super() {}; virtual std:

In java , can we pass superclass Object to subclass reference?

血红的双手。 提交于 2019-12-17 15:39:34
问题 In java, can we pass superclass Object to subclass reference ? I know that it is a weird question/practically not viable, but I want to understand the logic behind this Why is it not allowed in java. class Employee { public void met1(){ System.out.println("met1"); } } class SalesPerson extends Employee { @Override public void met1(){ System.out.println("new met1"); } public void met2(){ System.out.println("met2"); } } public class ReferenceTest { public static void main(String[] args) {

Inheritance Mapping with Fluent NHibernate

走远了吗. 提交于 2019-12-17 15:37:10
问题 Given the following scenario, I want map the type hierarchy to the database schema using Fluent NHibernate. I am using NHibernate 2.0 Type Hierarchy public abstract class Item { public virtual int ItemId { get; set; } public virtual string ItemType { get; set; } public virtual string FieldA { get; set; } } public abstract class SubItem : Item { public virtual string FieldB { get; set; } } public class ConcreteItemX : SubItem { public virtual string FieldC { get; set; } } public class

Polymorphism (in C) [duplicate]

岁酱吖の 提交于 2019-12-17 15:32:14
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How can I simulate OO-style polymorphism in C? I'm trying to better understand the idea of polymorphism with examples from languages I know; is there polymorphism in C? 回答1: This is Nekuromento's second example, factored in the way I consider idiomatic for object-oriented C: animal.h #ifndef ANIMAL_H_ #define ANIMAL_H_ struct animal { // make vtable_ a pointer so they can be shared between instances // use _ to