polymorphism

Are Interfaces Compatible With Polymorphism

Deadly 提交于 2019-12-18 12:14:07
问题 I am having trouble with the concept of interfaces interacting with polymorphic types (or even polymorphic interfaces). I'm developing in C# and would appreciate answers staying close to this definition, although i think that still gives plenty of room for everyone to put forth an answer. Just as an example, let's say you want to make a program to paint things. You define an interface for the actor that Paints, and you define an interface for the subject which is painted, Furthermore you have

C++11 smart pointers and polymorphism

风格不统一 提交于 2019-12-18 12:05:56
问题 I'm rewriting an application using c++11 smart pointers. I have a base class: class A {}; And a derived class: class B : public A { public: int b; }; I have another class containing a vector with either A or B objects: class C { public: vector<shared_ptr<A>> v; }; I have no problem constructing C with A (base class) objects but how can I fill it with B (derived class) objects? I'm trying this: for(int i = 0; i < 10; i++) { v.push_back(make_shared<B>()); v.back()->b = 1; }; And the compiler

C# Polymorphism

邮差的信 提交于 2019-12-18 10:57:19
问题 What's the difference between run-time polymorphism and compile-time polymorphism? Also, what's the difference between early binding and late binding? Examples would be highly appreciated. 回答1: Compile Time Polymorphism Method overloading is a great example. You can have two methods with the same name but with different signatures. The compiler will choose the correct version to use at compile time. Run-Time Polymorphism Overriding a virtual method from a parent class in a child class is a

AngularJS - ngRepeat with multiple object types

孤者浪人 提交于 2019-12-18 10:35:30
问题 I have a list of items. An item can be a number of things, let's say the list is something like so : [userObject , vehicleObject , userObject , animalObject , animalObject] Now I want to render the list with ngRepeat directive that will use a template according to the type of the object (Polymorphic rendering). can this be done? maybe something like ( ng-use is an hypothetically directive): <ul> <li ng-repeat="item in items"> <img ng-use="item.type == 'user'" ng-src="item.src"> <a ng-use=

Replace conditional with polymorphism - nice in theory but not practical

試著忘記壹切 提交于 2019-12-18 10:35:10
问题 "Replace conditional with polymorphism" is elegant only when type of object you're doing switch/if statement for is already selected for you. As an example, I have a web application which reads a query string parameter called "action". Action can have "view", "edit", "sort", and etc. values. So how do I implement this with polymorphism? Well, I can create an abstract class called BaseAction, and derive ViewAction, EditAction, and SortAction from it. But don't I need a conditional to decided

python properties and inheritance

大城市里の小女人 提交于 2019-12-18 10:04:31
问题 I have a base class with a property which (the get method) I want to overwrite in the subclass. My first thought was something like: class Foo(object): def _get_age(self): return 11 age = property(_get_age) class Bar(Foo): def _get_age(self): return 44 This does not work (subclass bar.age returns 11). I found a solution with an lambda expression which works: age = property(lambda self: self._get_age()) So is this the right solution for using properties and overwrite them in a subclass, or are

Why upcasting does not show runtime polymorphism?

白昼怎懂夜的黑 提交于 2019-12-18 09:44:40
问题 I wrote a code to understand runtime polymorphism... class S{ int i=1; void m(){ System.out.println("sssssssssssssssssssss"); } } public class A extends S{ int i=2; void m(){ System.out.println("aaaaaaaaaaaaaaaaaaaaaaaa"); } public static void main(String[] args) { S a=(S)new A(); System.out.println(a.i); a.m(); } } Instance variable are subject to compile time binding, but why down casting of object of A does not meaning here? Means it's invoking A's method not S's method? 回答1: S a = (S)new

Coding an inventory system, with polymorphic items and manageable item types

◇◆丶佛笑我妖孽 提交于 2019-12-18 09:31:34
问题 We currently have an inventory system for our employees. It contains laptops, phones, but also ergonomic chairs, fridges or software licenses ... So very different stuff that admins can create/read/ update/delete. After doing a version completely based on admin interface, I gave up cause it didn't provide enough flexibility. So I rolled-up a complete custom version, but there is far too much code to my taste ... it's a pain to maintain. Some of the problems I have been facing include :

Rails 4 Polymorphic associations and concerns

泄露秘密 提交于 2019-12-18 07:43:47
问题 I'm trying to add an Evaluation model to my Rails 4 app. I have made a model called evaluation.rb . It has: class Evaluation < ActiveRecord::Base belongs_to :evaluator, :polymorphic => true belongs_to :evaluatable, :polymorphic => true I have also made concerns for evaluator and evaluatable as: module Evaluator extend ActiveSupport::Concern included do has_many :given_evaluations, as: :evaluator, dependent: :destroy, class_name: 'Evaluation' end end module Evaluatable extend ActiveSupport:

Implementing methods from an interface but with different parameters

删除回忆录丶 提交于 2019-12-18 07:10:09
问题 I am looking for a good way to have different implementations of the same method which is defined in an interface but with different parameter types. Would this be possible? In order to clarify this, suppose I have an interface Database and two implementing classes Database1 and Database2. Database has a method createNode(...) and another one modifyNode(...). The problem is that for Database1 the return type of the createNode method should be a long (the identifier). For Database2, however,