inheritance

Accessing derived class property members from base class object in CSharp

非 Y 不嫁゛ 提交于 2019-12-29 09:32:20
问题 I am having trouble accessing property members of derived class using base class object. Scenario: public class BaseClass{ public virtual Write(BaseClass data){ } } public class DerivedClass : BaseClass{ private string name: public string Name {get {return name} set {name = value;} } public override Write(BaseClass data){ Console.println(data.Name); // gives me error here } } 回答1: The reason you have a problem accessing properties in derived classes is that the base class does not (and more

Get integer value from another class

好久不见. 提交于 2019-12-29 09:24:08
问题 I know i have done this before, but I just cant remember how to do it. I have a integer, that i want to be able to change in another class of mine. how do i do it? MainViewClass : UIViewController { int score; } #import "MainViewClass.h" OtherClass : MainViewClass{ } Then in the .m of OtherClass i want to be able to use the variable score. How do i do this? I have searched around the internet, and have tried several things to try to get it to work will no success. Thanks for looking! Have a

Are static inner classes a good idea or poor design?

柔情痞子 提交于 2019-12-29 08:43:05
问题 I'm find I have several places that having public static inner classes designed that extend "helper" classes makes my code a lot more type safe and, in my opinion, readable. For example, imagine I have a "SearchCriteria" class. There are a lot of commonalities for the different things I search for (a search term and then a group of search term types, a date range, etc.) By extending it in a static inner class, I tightly couple the extension and the searchable class with the specific

Are static inner classes a good idea or poor design?

て烟熏妆下的殇ゞ 提交于 2019-12-29 08:42:26
问题 I'm find I have several places that having public static inner classes designed that extend "helper" classes makes my code a lot more type safe and, in my opinion, readable. For example, imagine I have a "SearchCriteria" class. There are a lot of commonalities for the different things I search for (a search term and then a group of search term types, a date range, etc.) By extending it in a static inner class, I tightly couple the extension and the searchable class with the specific

How to use namedtuples in multiple inheritance

与世无争的帅哥 提交于 2019-12-29 08:23:06
问题 Is it possible to create a class that inherits from multiple instances of namedtuple, or create something to the same effect (having an immutable type that combines the fields of the base types)? I haven't found a way to do so. This example illustrates the problem: >>> class Test(namedtuple('One', 'foo'), namedtuple('Two', 'bar')): >>> pass >>> t = Test(1, 2) TypeError: __new__() takes 2 positional arguments but 3 were given >>> t = Test(1) >>> t.foo 1 >>> t.bar 1 The problem seems to be that

C++ Inherited template classes don't have access to the base class [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-29 08:18:15
问题 This question already has answers here : “not declared in this scope” error with templates and inheritance [duplicate] (2 answers) Closed 5 years ago . I am working with template classes for the first time, and trying to figure out why the compiler doesn't seem to like when I use inheritence. Here is the code: template <typename T> struct xPoint2 { T x; T y; xPoint2() { x = 0; y = 0; }; }; template <typename T> struct xVector2 : xPoint2<T> { xVector2() { x = 0; y = 0; }; }; The compiler

Python parent class accessing class variable of child class

夙愿已清 提交于 2019-12-29 07:35:09
问题 I'm currently trying to implement some inheritance in my Python project and have hit a road block. I'm trying to create a baseParentClass that will handle the basic functionality for a lot of child classes. In this specific example I am trying to initialize an instance with a number of attributes (set to 0), stored as a class variable list (called ATTRS) in the Child. I am unsure of how to use this ATTRS in the parent class. class Parent(object): def __init__(): for attr in ATTRS: setattr

Inheriting static variable from abstract class

守給你的承諾、 提交于 2019-12-29 06:42:34
问题 I have half a dozen classes which all extend the same abstract class. The abstract class has a static variable pointing to some JNI code that I only want to load once per instantiation of the classes. From what I understand this results in exactly one instance of this static variable being instantiated, but what I want is for each of the extending classes to have their own static instance of the variable that is unique for the given child class. I want to write some code in my abstract class

Method Chaining: How to use getThis() trick in case of multi level inheritance

人盡茶涼 提交于 2019-12-29 05:30:06
问题 My question is in context of Method chaining + inheritance don’t play well together?. But unfortunately all examples/answers of method chaining uses single level of inheritance. My usecase involves multi level of inheritance for e.g abstract class PetBuilder{...} class DogBuilder extends PetBuilder{..} class DogType1Builder extends DogBuilder {...} To construct a Dog Object,i will be using either DogBuilder or DogType1Builder how to use getThis trick for the above use case? I want to use

Pass current object type into base constructor call

♀尐吖头ヾ 提交于 2019-12-29 05:27:31
问题 How do I grab the Type of the inherited class and pass it into the base constructor of the class also inherited? See the code sample below: // VeryBaseClass is in an external assembly public abstract class VeryBaseClass { public VeryBaseClass(string className, MyObject myObject) { } } // BaseClass and InheritedClass are in my assembly public abstract class BaseClass : VeryBaseClass { public BaseClass(MyObject myObject) : base(this.GetType().Name, myObject) // can't reference "this" (Type