subclass

C# calling overridden subclass methods without knowledge that it's a subclass instance

不羁的心 提交于 2019-12-10 15:46:57
问题 I have a base class with a virtual method, and multiple subclasses that override that method. When I encounter one of those subclasses, I would like to call the overridden method, but without knowledge of the subclass. I can think of ugly ways to do this (check a value and cast it), but it seems like there should be an in-language way to do it. I want the List to contain multiple subclasses within the same list, otherwise obviously I could just make a List. EDIT: Fixed the comment in the code

JAXB marshalling declared parent class vs. actual runtime subclass

大兔子大兔子 提交于 2019-12-10 15:44:51
问题 I'm using JAXB to marshal a class that has an instance variable that's declared as class Foo. At runtime that instance variable is set to an instance of a subclass of Foo, let's say FooBar. There are basic JAXB annotations on both class Foo and FooBar. The XML output shows that an instance of Foo is being marshaled instead of FooBar. Is there something specific I have to do in terms of annotations to tell JAXB how to properly marshal the runtime subclass in lieu of the declared superclass? I

How to get the modules included in a class

我怕爱的太早我们不能终老 提交于 2019-12-10 15:17:19
问题 How can I get an array of modules included in a class, excluding those that slipped in through inheritance? Notice that ancestors , <=> , included_modules will not work because their results do not distinguish modules that are included from modules that are prepended in a superclass. In other words, they cannot distinguish the following two cases: M is prepended to the superclass of B class A; end class B < A; end module M; end A.prepend M B.ancestors # => [B, M, A, Object, Kernel,

To subclass or not to subclass

 ̄綄美尐妖づ 提交于 2019-12-10 14:28:16
问题 I have three objects; Action, Issue and Risk. These all contain a nunber of common variables/attributes (for example: Description, title, Due date, Raised by etc.) and some specific fields (risk has probability). The question is: Should I create 3 separate classes Action, Risk and Issue each containing the repeat fields. Create a parent class "Abstract_Item" containing these fields and operations on them and then have Action, Risk and Issue subclass Abstract_Item. This would adhere to DRY

Can Typescript infer the type of an instance of an extension class instantiated by a method of its base?

孤街浪徒 提交于 2019-12-10 14:18:35
问题 Consider the following Typescript snippet: class Animal { constructor(name: string) { this.name = name; } name: string; haveBaby(name: string): ?? return type ?? { return new this.constructor(name); // Error } } class Cat extends Animal {} class Dog extends Animal {} class Gerbil extends Animal {} // etc. let sorachi = new Cat("Sorachi"); // a: Cat let sorachiJr = a.haveBaby("Sorachi Jr."); // I want: sorachiJr: Cat Animals can have babies, and a baby should be the same kind of animal as the

SubClassing UILabel

拟墨画扇 提交于 2019-12-10 13:48:22
问题 I read in this same site how to inset and UILabel (subclass UILabel and override the required methods). Before adding it to my app I decided to test it out in a standalone test app. Code is shown below. Here's MyUILabel.h #import <UIKit/UIKit.h> @interface MyUILabel : UILabel @end Here's MyUILabel.m #import "MyUILabel.h" #import <QuartzCore/QuartzCore.h> @implementation MyUILabel - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code }

call method in a subclass of UIView

为君一笑 提交于 2019-12-10 12:19:04
问题 I've got a UIViewController and an own class, a subclass of UIView. In my ViewController I make a instance of the uiview. If I tap the uiview a function gets called within it and an overlay appears. TO get rid of that overlay later the user has to tap somewhere on the screen(besides the instance of my class) How do I tell my class to dismiss the overlay? I already thought of delegate. So my thoughts were to make a MyUIViewControllerdelegate. If my viewcontroller receives a tap the delegate

How to subclass int and make it mutable

时光总嘲笑我的痴心妄想 提交于 2019-12-10 12:06:19
问题 Is it possible to subclass int and make it mutable? Consider the following class: class CustomBitVector(int): # bit 7 @property def seventh_property(self): return bool(self & (1 << 7)) @seventh_property.setter def seventh_property(self, val): self |= bool(val) << 7 # bit 6 @property def sixth_property(self): return bool(self & (1 << 6)) @sixth_property.setter def sixth_property(self, val): self |= bool(val) << 6 # ... a few more of these ... # bit 0 @property def zeroth_property(self): return

Getting clone of superclass

蓝咒 提交于 2019-12-10 12:01:33
问题 Here is my playground snippet: class Box { func clone() -> Box { return Box() // <- how to return superclass here? } } class VBox:Box { } let vb = VBox() let cBox = vb.clone() // now cBox is a Box, not a VBox My clone function returns a Box class in all cases. But for the subclass I want it to return the superclass (so above it should return VBox ). I know I could override the clone function inside VBox but I wonder if there's a smarter way. 回答1: You mean subclass. Box is the superclass,

Subclass constructors - Why must the default constructor exist for subclass constructors?

我怕爱的太早我们不能终老 提交于 2019-12-10 10:18:16
问题 Given a random class: public class A<T> { public T t; public A () {} // <-- why is this constructor necessary for B? public A (T t) { this.setT(t); } public T getT () { return this.t; } protected void setT (T t) { this.t = t; return; } } And an extended class: public class B extends A<Integer> { public B (Integer i) { this.setT(i); } } Why does B require A to have the empty constructor? I would have assumed it would want to use the similar constructor instead of the default constructor. I