subclass

In C# 4.0, is it possible to derive a class from a generic type parameter?

自作多情 提交于 2019-11-26 21:08:33
问题 I've been trying this, but I can't seem to figure this out. I want to do this... public abstract class SingletonType<TSingleton, TBaseClass> : TBaseClass where TSingleton : TBaseClass, new() where TBaseClass : class { static TSingleton _singleton; public static TSingleton Singleton => _singleton ?? (_singleton = new TSingleton()); } The plan was to use it like this which would sort of 'wrap' the singleton pattern around a base class... public class SingletonFoo : SingletonType<SingletonFoo,

Should I subclass the NSMutableArray class

北慕城南 提交于 2019-11-26 20:52:44
问题 I have an NSMutableArray object that I want to add custom methods to. I tried subclassing NSMutableArray but then I get an error saying "method only defined for abstract class" when trying to get the number of objects with the count method. Why is the count method not inherited? I read somewhere else that I will have to import some NSMutableArray methods into my custom class if I want to use them. I just want to add a custom method to the NSMutableArray class. So should I subclass

Subclassing int in Python

被刻印的时光 ゝ 提交于 2019-11-26 20:29:11
I'm interested in subclassing the built-in int type in Python (I'm using v. 2.5), but having some trouble getting the initialization working. Here's some example code, which should be fairly obvious. class TestClass(int): def __init__(self): int.__init__(self, 5) However, when I try to use this I get: >>> a = TestClass() >>> a 0 where I'd expect the result to be 5 . What am I doing wrong? Google, so far, hasn't been very helpful, but I'm not really sure what I should be searching for int is immutable so you can't modify it after it is created, use __new__ instead class TestClass(int): def _

Expose a private Objective-C method or property to subclasses

谁说胖子不能爱 提交于 2019-11-26 20:07:58
问题 According to some official talk, a class in Objective-C should only expose public methods and properties in its header: @interface MyClass : NSObject @property (nonatomic, strong) MyPublicObject *publicObject; - (void)publicMethod; @end and private methods/properties should be kept in class extension in .m file: @interface MyClass() @property (nonatomic, strong) MyPrivateObject *privateObject; - (void) privateMethod; @end and I don't think there is a protected type for things that are private

How to subclass UIScrollView and make the delegate property private

会有一股神秘感。 提交于 2019-11-26 18:54:54
问题 Here is what I want to achieve: I want to subclass an UIScrollView to have additional functionality. This subclass should be able to react on scrolling, so i have to set the delegate property to self to receive events like: - (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView { ... } On the other hand, other classes should still be able to receive these events too, like they were using the base UIScrollView class. So I had different ideas how to solve that problem, but all of

Swift 3: subclassing NSObject or not?

你说的曾经没有我的故事 提交于 2019-11-26 18:22:45
问题 I have read some posts like this one about the difference between subclassing NSObject in Swift or just having its native base class with no subclassing. But they all are a bit old posts, and I am not clear about this topic. When should you subclass NSObject ? What is the actual difference between subclassing it and not subclassing? What is currently the recommendation in Swift? 回答1: Apple's documentation about NSObject states the following as an introduction: NSObject is the root class of

Swift + CoreData: Cannot Automatically Set Optional Attribute On Generated NSManagedObject Subclass

主宰稳场 提交于 2019-11-26 18:07:20
问题 I have a coredata entity named Record and has a property dateUpdated. I noticed that the generated NSManagedObject subclass has no optional mark (?) CoreData Editor: Generated Subclass: Expected: UPDATED: It's tedious in my part, because each time I want to regenerate the subclasses, it means I also need to update all optional values manually. Having a non-optional (without '?') in subclass lead me to check evalue before assigning, like example below: // sample value: // serverDateFormatter =

Why do I get “non-static variable this cannot be referenced from a static context”?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 17:54:16
I have a very simple class which I want to use as a subclass of another one. But when I put its code in the parent's class I get : non-static variable this cannot be referenced from a static context On the other hand when I put the sublass GenTest 's class code outside the the "parent's" class code - JavaApp1 I do not get this error. public class JavaApp1 { class GenTest { @Deprecated void oldFunction() { System.out.println("don't use that"); } void newFunction() { System.out.println("That's ok."); } } public static void main(String[] args) { GenTest x = new GenTest(); x.oldFunction(); x

How do you use the ellipsis slicing syntax in Python?

北城余情 提交于 2019-11-26 16:55:44
This came up in Hidden features of Python , but I can't see good documentation or examples that explain how the feature works. nosklo You'd use it in your own class, since no builtin class makes use of it. Numpy uses it, as stated in the documentation . Some examples here . In your own class, you'd use it like this: >>> class TestEllipsis(object): ... def __getitem__(self, item): ... if item is Ellipsis: ... return "Returning all items" ... else: ... return "return %r items" % item ... >>> x = TestEllipsis() >>> print x[2] return 2 items >>> print x[...] Returning all items Of course, there is

Call subclass's method from its superclass

落花浮王杯 提交于 2019-11-26 16:45:15
问题 I have two classes, named Parent and Child , as below. Parent is the superclass of Child I can call a method of the superclass from its subclass by using the keyword super . Is it possible to call a method of subclass from its superclass? Child.h #import <Foundation/Foundation.h> #import "Parent.h" @interface Child : Parent { } - (void) methodOfChild; @end Child.m #import "Child.h" @implementation Child - (void) methodOfChild { NSLog(@"I'm child"); } @end Parent.h: #import <Foundation