subclassing

How to subclass str in Python

牧云@^-^@ 提交于 2019-11-30 03:01:13
I am trying to subclass str object, and add couple of methods to it. My main purpose is to learn how to do it. Where I am stuck is, am I supposed to subclass string in a metaclass, and create my class with that meta, or subclass str directly? And also, I guess I need to implement __new__() somehow, because, my custom methods will modify my string object, and will return new mystr obj. My class's methods, should be completely chainable with str methods, and should always return a new my class instance when custom methods modified it. I want to be able to do something like this: a = mystr(

Required initializers for a subclass of UIViewController

柔情痞子 提交于 2019-11-30 03:00:23
问题 I've been attempting to follow a tutorial about creating a container view controller. It's in Objective-C. I want to convert it to Swift. I've found some of the same questions here, but I didn't get too much out of them. Here's the code. import UIKit class ContainerViewController: UIViewController { // Class "ContainerViewController" has no initializers - That I know why. // 'required' initializer 'init(coder:)' must be provided by a subclass of UIViewController var

What is subclassing?

非 Y 不嫁゛ 提交于 2019-11-29 17:22:00
问题 I am new to java and I am trying to create an XML document and clone a specific node (minus the textnode) of this document over and over again. Someone answered me and said that I should subclass the node and override the cloning. So my question is what is sub-classing? 回答1: @Charlie Martin has explained what subclassing means. However, it is not clear that you've been given good advice. If you are creating the XML document by assembling a DOM in memory, a better approach would be to create a

issubclass of abstract base class Sequence

对着背影说爱祢 提交于 2019-11-29 15:21:45
This list shows what methods you need to implement for your class to be "regarded" as Sequence: __getitem__ , __len__ , __contains__ , __iter__ , __reversed__ , index , and count . So why does this minimal implementation does not work, i.e. why issubclass(S, Sequence) is False ? from collections import * class S(object): def __getitem__(self, item): raise IndexError def __len__(self): return 0 def __contains__(self, item): return False def __iter__(self): return iter(()) def __reversed__(self): return self def index(self, item): raise IndexError def count(self, item): return 0 issubclass(S,

How to create a subclass in C#?

此生再无相见时 提交于 2019-11-29 11:40:08
问题 How do I create a subclass in C# for ASP.NET using Visual Studio 2010? 回答1: Do you mean this? public class Foo {} public class Bar : Foo {} In this case, Bar is the sub class. 回答2: Here is an example of writing a ParentClass and then creating a ChildClass as a sub class. using System; public class ParentClass { public ParentClass() { Console.WriteLine("Parent Constructor."); } public void print() { Console.WriteLine("I'm a Parent Class."); } } public class ChildClass : ParentClass { public

Objective-C sub-classing basics, how to add custom property;

落花浮王杯 提交于 2019-11-29 02:56:05
问题 I am having a issue subclassing MKPolygon. I want to add a simple int tag property but I keep getting an instance of MKPolygon instead of my custom class, so calling setTag: causes an exception. The problem is that MKPolygons are created using a class method: polygonWithCoordinates: count: and I dont know how to turn that into an instance of my class (which includes the tag property). How would you go about adding a tag property to MKPolygon? Thank you! 回答1: You should both use a category (as

Why doesn't Apple allow subclassing of UINavigationController? And what are my alternatives to subclassing?

醉酒当歌 提交于 2019-11-28 21:32:47
I'm currently building a tabbed iPhone application where each tab's view controller is an instance of UINavigationController , and where every subcontroller of every one of the UINavigationController instances is an instance of UITableViewController . Ideally, I'd like to subclass UINavigationController so that the controller for each tab is a subclass of UINavigationController that (in addition to having all the standard UINavigationController functionality, obviously) serves as the datasource and the delegate for each of the table views associated with its subcontrollers. Trying to do this

How to cast 'Class A' to its subclass 'Class B' - Objective-C

时光总嘲笑我的痴心妄想 提交于 2019-11-28 21:10:56
I'm using a framework which defines and uses 'ClassA', a subclass of NSObject. I would like to add some variables and functionality so naturally I created 'ClassB', a subclass of 'ClassA' Now my problem is this. Many of the methods within this framework return instances of 'ClassA' which I would like to cast to my subclass. For example take this method: - (ClassA *)doSomethingCool:(int)howCool Now in my code I try this: ClassB * objB; objB = (ClassB *)doSomethingCool(10); NSLog(@"objB className = %@", [objB className]); This runs just fine. No compile or runtime errors or anything. But what is

Objective C — narrow instance variable types in subclasses?

元气小坏坏 提交于 2019-11-28 12:36:45
Is it possible to narrow the allowed type of an ivar in a subclass. Something like this: @interface person: NSObject { NSArray *friendArray; } @interface mutablePerson: person { NSMutableArray *friendArray; } I just tried that exact code, and Xcode gave me a compile error. I'm wondering if there is a way around it. The project I am working on is going to have a lot of this sort of situation. I understand that I can use casts to make the code work. But I will be making an awful lot of casts if I do that, and I'm wondering if there is a better way. No, you can't redeclare ivars at all. However,

Extending Generic Classes

佐手、 提交于 2019-11-28 05:12:52
public class MyGeneric<T, E> {} public class Extend1<T, E> extends MyGeneric<T, E> {} public class Extend2 extends MyGeneric<String, Object> {} As far as I am aware, both of the subclasses in the above example are valid. I was wondering how Java knows when the types given in the superclass are going to be defined when the subclass is instantiated, and when they are actual class names (i.e. How does it know T, E are not class names)? A side note, is it permissible (even if uncommon) to use more than one letter for the generic types? What if (through some sever error of planning) The types