inheritance

Get all last descendants of a base type?

↘锁芯ラ 提交于 2020-01-25 06:31:09
问题 This question is an opposite of How to find types that are direct descendants of a base class? If this is the inheritance hierarchy I have, class Base { } class Derived1 : Base { } class Derived1A : Derived1 { } class Derived1B : Derived1 { } class Derived2 : Base { } I need a mechanism to find all the sub types of Base class in a particular assembly which are at the end of the inheritance tree. In other words, SubTypesOf(typeof(Base)) should give me -> { Derived1A, Derived1B, Derived2 } 回答1:

Custom XML serialization with inheritance

[亡魂溺海] 提交于 2020-01-25 00:33:45
问题 I have the following class structure which I want to serialize: [XmlRoot("SomeClass")] public class SomeClass { private BaseClass[] _itemArray; public BaseClass[] ItemArray { get { return _itemArray; } set { _itemArray = value; } } public PPSPStatReportMessage() : base() { } } public class SomeOtherClass { private int _property5; [XmlAttribute("Property5")] public int Property5 { get { return _property5; } set { _property5 = value; } } private string _property6; [XmlText()] public string

How do I access a parent's methods inside a child's constructor in PHP?

纵然是瞬间 提交于 2020-01-24 20:40:05
问题 Say I have class child() and class parent() . The parent has a constructor and a few other public methods, and the child is empty apart from a constructor. How do I go about calling a parent's methods inside of the child's constructor, as in: Class Parent { public function __construct() { // Do stuff (set up a db connection, for example) } public function run($someArgument) { // Manipulation return $modifiedArgument; } } Class Child extends Parent { public function __construct() { // Access

Call hidden (non virtual) method of derived class from base

☆樱花仙子☆ 提交于 2020-01-24 19:25:48
问题 Is there a way (templates, macros anything else) to substitute a call to hidden_in_derived from common method at compile time , so that instance of Derived calls it's own hidden_in_derived ( without making hidden_in_derived virtual in Base) ? #include <iostream> class Base { public: void common() { // some calls to other methods hidden_in_derived(); // yet some calls to other methods } void hidden_in_derived() { std::cout << "A" << std::endl; } }; class Derived : public Base { public: void

Call hidden (non virtual) method of derived class from base

旧城冷巷雨未停 提交于 2020-01-24 19:25:06
问题 Is there a way (templates, macros anything else) to substitute a call to hidden_in_derived from common method at compile time , so that instance of Derived calls it's own hidden_in_derived ( without making hidden_in_derived virtual in Base) ? #include <iostream> class Base { public: void common() { // some calls to other methods hidden_in_derived(); // yet some calls to other methods } void hidden_in_derived() { std::cout << "A" << std::endl; } }; class Derived : public Base { public: void

How to pass method result as parameter to base class constructor in C++?

£可爱£侵袭症+ 提交于 2020-01-24 19:05:30
问题 I've trying to achieve something like this: class Base { public: Base(string S) { ... }; } class Derived: Base { public: int foo; string bar() { return stringof(foo); // actually, something more complex }; Derived(int f) : foo(f), Base(bar()) { }; } Now, this doesn't work as I want, because bar() is called in the Derived constructor before foo is initialized. I considered adding a static function similar to bar() which takes foo as a parameter - and using that in the initialization list, but

Access hidden property in base class c#

帅比萌擦擦* 提交于 2020-01-24 14:09:13
问题 In my ASP.NET Core API, I have a DTO class BaseDto and another DerivedDto that inherits from BaseDto and hides some of its properties, because they're required in DerivedDto . I also have a BaseModel class to which both BaseDto and DerivedDto will be mapped through another class Mapper . Something like the following code: using System.ComponentModel.DataAnnotations; public class BaseDto { public string Name { get; set; } } public class DerivedDto : BaseDto { [Required] public new string Name

Possible to avoid default call to super() in Java?

混江龙づ霸主 提交于 2020-01-24 13:24:06
问题 Assume for some reason that I don't want to implicitly call super() which is done by default. class Animal { public Animal() { System.out.println("Constructing an animal."); } } class Dog extends Animal { public Dog() { System.out.println("Constructing a dog."); } public static void main(String[] a) { new Dog(); } } Is there any way to "disable" the default behavior that super() is invoked when making a new Dog? Or would that be principally and conceptually wrong? I mean there could be cases

How to arbitrarily extend an “object” in Go

拈花ヽ惹草 提交于 2020-01-24 12:46:08
问题 I hope my question can be made clear. I've done my best to make this concise, but please ask for clarification if it is necessary. In JavaScript, it's common practice to have a "plugin" modify an existing object by creating new methods. jQuery plugins do this, for example. I have the need to do something similar in Go, and am looking for the best way to do it. The simplest to implement would simply be to store functions in a map[string]func type of data structure, then call these new "methods

“this” keyword type when called on an object of derived class from base class

纵然是瞬间 提交于 2020-01-24 11:02:53
问题 If I have something like this: class Base { public void Write() { if (this is Derived) { this.Name();//calls Name Method of Base class i.e. prints Base ((Derived)this).Name();//calls Derived Method i.e prints Derived } else { this.Name(); } } public void Name() { return "Base"; } } class Derived : Base { public new void Name() { return "Derived"; } } and use the following code to call it, Derived v= new Derived(); v.Write(); // prints Base then the Name method of base class gets called. but