subclassing

subclassing int to attain a Hex representation

有些话、适合烂在心里 提交于 2019-12-11 01:07:57
问题 Basically I want to have access to all standard python int operators, eg __and__ and __xor__ etc, specifically whenever the result is finally printed I want it represented in Hex format. (Kind of like putting my calculator into Hex mode) class Hex(int): def __repr__(self): return "0x%x"%self __str__=__repr__ # this certainly helps with printing if __name__=="__main__": print Hex(0x1abe11ed) ^ Hex(440720179) print Hex(Hex(0x1abe11ed) ^ Hex(440720179)) Ideally BOTH line of output should be

Are class initializers possible in C#?

情到浓时终转凉″ 提交于 2019-12-10 20:23:14
问题 In C# you have object initializers to initialize fields of objects at creation time without using a constructor. Now I'm wondering if there is an equivalent to classes which means that you can 'initialize' properties of classes when defining subclasses without actually using an override syntax but simply declaring what the value of a known property is. Example: public abstract class Car { public abstract string Name { get; } } // usual approach public class Mustang : Car { public overwrite

Are template arguments required everywhere when mentioning a template base class?

走远了吗. 提交于 2019-12-10 18:36:22
问题 Here's a simple template; template <class T> class tt { private: T x; public: tt() {x=0;}; Add(T p) {x += p;}; }; ... and then a subclass of it; class cc : public tt<int> { public: cc() : tt() {}; }; This compiles fine in VC, but not in C++ Builder (XE) where it gives a E2102 error. The C++ Builder compiler needs the following syntax on the constructor of the cc class to compile; cc() : tt<int>() {}; In fact, the C++ Builder compiler needs the template parameters repeated every time the tt

Instantiating anonymous inner classes in Java with additional interface implementation

丶灬走出姿态 提交于 2019-12-10 17:15:40
问题 Let's say I have the following two class/interface definitions: public abstract class FooClass { public abstract void doFoo(); } and public interface BarInterface { public void doBar(); } If I want to make an anonymous inner class that extends/implements both, do I need to do this: public abstract class BothClass extends FooClass implements BarInterface {} ... new BothClass() { public void doFoo() { System.out.println("Fooooooooo!!!!"); } public void doBar() { System.out.println("Baaaaaaaar!!

Adding methods to an existing class instance, or how to “subclass” an instance

北战南征 提交于 2019-12-10 17:06:43
问题 I'm using a package that gives me an object filled with a bunch of data that I don't want to bother to manually serialize and use to initialize another object. What I want to do is attach a bunch of extra methods onto the object for my own purposes. Ideally I'd like to magically subclass an instance , but that doesn't seem possible. Monkey-patching would likely 'work', but the internet says it's not great form, and because other parts of my code might actually use the native class elsewhere,

UIPopoverPresentationController should have a non-nil sourceView or barButtonItem set before the presentation occurs on iOS 9

一曲冷凌霜 提交于 2019-12-10 16:23:58
问题 I'm trying to show a popup using a custom UIPopoverPresentationController class. But it crashes with the error (<UIPopoverPresentationController: 0x7a772950>) should have a non-nil sourceView or barButtonItem set before the presentation occurs. Below is my button click code where the crash occurs. - (IBAction)showPopup:(UIButton *)sender { ViewController *contentViewController = [[ViewController alloc] init]; contentViewController.preferredContentSize = CGSizeMake(200, 200);

new in Xcode 6.3/iOS 8.3: using self alloc for convenience constructor causes build error

廉价感情. 提交于 2019-12-10 13:47:29
问题 This code did not change between Xcode 6.2 and 6.3, but the line containing [self alloc] now causes the error: Multiple methods named 'initWithType:' found with mismatched result, parameter type or attributes @implementation AGNetworkDataRequest + (instancetype)networkDataRequestWithType:(AGNetworkDataRequestType)type { AGNetworkDataRequest *r = [[self alloc] initWithType:type];//error here return r; } - (id)initWithType:(AGNetworkDataRequestType)type { //typical init code } //... If I Cmd

How do you properly write and use a View subclass in Android?

你说的曾经没有我的故事 提交于 2019-12-10 13:34:20
问题 I am trying to implement one of the solutions found here. My problem is that I'm not sure if I am implementing and using my subclass correctly. I am subclassing a WebView here: public class myWebView extends WebView{ public myWebView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onSizeChanged(int w, int h, int ow, int oh) { // TODO Auto-generated method stub scrollTo(xScroll - (widthScroll/2), yScroll - (heightScroll/2)); super

Automatically resizing NumPy recarray

落爺英雄遲暮 提交于 2019-12-10 10:42:59
问题 I'd like to create a subclass of numpy.recarray that automatically resizes when data is added to a row outside of its current length. The code below does most of what I want. class autorecarray(numpy.recarray): def __init__(self,*args,**kwargs): self._increment = 1 numpy.recarray.__init__(self,args,kwargs) def __setitem__(self,ind,y): try: numpy.recarray.__setitem__(self,ind,y) except IndexError: self.resize((self.__len__()+self._increment,),refcheck=False) self.__setitem__(ind,y) It works

Subclassing file by subclassing `io.TextIOWrapper` — but what signature does its constructor have?

浪子不回头ぞ 提交于 2019-12-10 03:16:55
问题 I'm trying to subclass io.TextIOWrapper following this post, although my aims are different. Starting off with this (NB: motivation): class MyTextIOFile(io.TextIOWrapper): def read(self, *args): cont = super().read(*args) return cont.replace("\x00", "") I'm trying to open a file using my constructor using In [81]: f = MyTextIOFile("file.csv") but this gives: --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython