polymorphism

Attempting to learn polymorphism, etc. in VB6, but my code doesn't do what I want it to

拟墨画扇 提交于 2020-01-11 11:09:13
问题 Here's what I've got on a command button; it's just creating variables and attempting to output their ID (which should be an instance variable inherited from the base class.) Private Sub Command1_Click() Dim ball1 As Ball, ball2 As Ball Dim cube1 As Cube, cube2 As Cube Set ball1 = New Ball Set cube1 = New Cube Set cube2 = New Cube Set ball2 = New Ball MsgBoxTheID (ball1) 'errors; should be 0 MsgBoxTheID (ball2) 'errors; should be 3 MsgBoxTheID (cube1) 'errors; should be 1 MsgBoxTheID (cube2)

C# - How to convert List<Dog> to List<Animal>, when Dog is a subclass of Animal?

和自甴很熟 提交于 2020-01-11 08:08:41
问题 I have a class Animal , and its subclass Dog . I have a List<Animal> and I want to add the contents of some List<Dog> to the List<Animal> . Is there a better way to do so, than just cast the List<Dog> to a List<Animal> , and then use AddRange ? 回答1: You don't need the cast if you're using C#4: List<Animal> animals = new List<Animal>(); List<Dog> dogs = new List<Dog>(); animals.AddRange(dogs); That's allowed, because AddRange() accepts an IEnumerable<T> , which is covariant. If you don't have

Simple organization of Rust traits for “polymorphic” return

試著忘記壹切 提交于 2020-01-11 06:38:07
问题 I have a basic struct called Frame that is useful for a bunch of calculations:. pub struct Frame<T> { grid_val: Vec<T>, grid_space: Vec<[T; 2]>, calculated_result: Option<Vec<T>> } Frame can be used to describe most basic calculations, but sometimes there's more complicated issues that come up and I need to add some more geometric information. So I used composition for each geometry: pub struct Sphere<T> { grid: Frame<T>, radius: T } pub struct Hyperbola<T> { top_grid: Frame<T>, bottom_grid:

Overriding vals in Scala

和自甴很熟 提交于 2020-01-10 04:30:06
问题 I have tried to override val in Scala as this: trait T{ val str: String } sealed abstract class Test extends T{ val str: String = "str" } class Test1 extends Test{ val str = super.str + "test1" //super may not be used on value str } But this refused to compile. Why? I wanted to override value in the abstract class using the abstract class's value itself. How to do this? In my particular case I cannot pass it as a class parameter. 回答1: Scala compiler does not allow to use super on a val . If

Inheritance in curiously recurring template pattern polymorphic copy (C++)

丶灬走出姿态 提交于 2020-01-10 04:17:27
问题 I'm using CRTP to add a clone method to inherited classes, for example: class Base { virtual ~Base() {}; virtual Base* clone() const = 0; }; template<class Derived> class BaseCopyable : Base { public: virtual Base* clone() const { return new Derived(static_cast<Derived const&>(*this)); } }; class A : public BaseCopyable<A>; class B : public BaseCopyable<B>; etc... But if I have a class that inherits from B, for example: class differentB : public B; Then clone() doesn't return an object of

What is polymorphic method in java?

笑着哭i 提交于 2020-01-09 11:11:44
问题 I'm studying java language for SCJP test. It is little bit hard to understand "polymorphic method". Could you explain it for me? or give me some links? 回答1: "Polymorphic" means "many shapes." In Java, you can have a superclass with subclasses that do different things, using the same name. The traditional example is superclass Shape , with subclasses Circle , Square , and Rectangle , and method area() . So, for example // note code is abbreviated, this is just for explanation class Shape {

Is it possible to restrict a Swift generic class function return type to the same class or subclass?

随声附和 提交于 2020-01-09 10:52:49
问题 I am extending a base class (one which I do not control) in Swift. I want to provide a class function for creating an instance typed to a subclass. A generic function is required. However, an implementation like the one below does not return the expected subclass type. class Calculator { func showKind() { println("regular") } } class ScientificCalculator: Calculator { let model: String = "HP-15C" override func showKind() { println("scientific") } } extension Calculator { class func create<T

Is it possible to restrict a Swift generic class function return type to the same class or subclass?

痞子三分冷 提交于 2020-01-09 10:51:10
问题 I am extending a base class (one which I do not control) in Swift. I want to provide a class function for creating an instance typed to a subclass. A generic function is required. However, an implementation like the one below does not return the expected subclass type. class Calculator { func showKind() { println("regular") } } class ScientificCalculator: Calculator { let model: String = "HP-15C" override func showKind() { println("scientific") } } extension Calculator { class func create<T

How can I know the classes that extend my base class at runtime? [duplicate]

偶尔善良 提交于 2020-01-07 09:06:43
问题 This question already has answers here : Get all derived types of a type (8 answers) Get all inherited classes of an abstract class [duplicate] (4 answers) Closed 5 years ago . During runtime , I would like to populate a drop down list with classes that have extended my base class. Currently I have an enum, and this is what I use to populate that list, but I want to add additional classes (and other people are adding classes) and do not want to have to maintain an enum for this purpose. I

Container of derived objects / smart pointers

对着背影说爱祢 提交于 2020-01-07 08:29:12
问题 Lets say i have: class Base {...}; class Derived1 : public Base {...}; class Derived2 : public Base {...}; class Derived3 : public Base {...}; class Store { public: void Add(const Base&); //Adds mix of Derived1/2/3 private: vector<const Base*> vec; vector<shared_ptr<const Base> > vec_smart; }; //------------------------------------------------------ void Store::Add(const Base& b){ vec.push_back(&b); //got sliced } When using vector of pointers to Base, it got sliced. I suppose i have to use