subclass

Cannot handle exception type thrown by implicit super constructor

[亡魂溺海] 提交于 2019-12-04 11:47:25
I have a Cage class: public class Cage<T extends Animal> { Cage(int capacity) throws CageException { if (capacity > 0) { this.capacity = capacity; this.arrayOfAnimals = (T[]) new Animal[capacity]; } else { throw new CageException("Cage capacity must be integer greater than zero"); } } } I am trying to instantiate an object of Cage in another class main method: private Cage<Animal> animalCage = new Cage<Animal>(4); I get the error: "Default constructor cannot handle exception type CageException thrown by implicit super constructor. Must define an explicit constructor." Any ideas? :o( This means

Redefining Pythons builtin datatypes

ε祈祈猫儿з 提交于 2019-12-04 11:40:38
Is it possible to redefine which object the brackets [] use? I can subclass the list object, but how to I make the interpreter use my subclass in place of the buildin list object? Is it possible? (I'm pretty sure I'm using the wrong terms for the question- feel free to edit) >>> class mlist(list): ... def __init__(self): ... list.__init__(self) ... def __getitem__(self, item): ... return list.__getitem__(self, item) * 2 ... >>> testlist = mlist() >>> testlist.append(21) >>> testlist[0] 42 >>> list = mlist() # maybe setting the 'list' type will do it? >>> testlist = [] >>> testlist.append(21) >

Reflection: cast an object to subclass without use instanceof

落花浮王杯 提交于 2019-12-04 11:23:18
I have this simple interface/class: public abstract class Message {} public class Message1 extends Message {} public class Message2 extends Message {} And an utility class: public class Utility { public void handler(Message m) { System.out.println("Interface: Message"); } public void handler(Message1 m) { System.out.println("Class: Message1"); } public void handler(Message2 m) { System.out.println("Class: Message2"); } } Now, the main class: public static void main(String[] args) { Utility p = new Utility(); Message1 m1 = new Message1(); p.handler(m1); Message m = (Message) m1; p.handler(m); }

Java/Android: anonymous local classes vs named classes

你。 提交于 2019-12-04 11:17:18
问题 I would like to ask what is the good practice on using anonymous classes vs. named inner classes? I am writing an Android application, which includes many UI elements (buttons, text fields, etc). For many of them I need some kind of listeners, so in onCreate of the application I have bunch of quite small anonymous classes like: someButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { // do something... } } ); Each of of such anonymous class is 5 - 20 lines big

How can I get a UIWebView's UIScrollView delegate methods called from within a standard UIViewController?

独自空忆成欢 提交于 2019-12-04 10:44:51
So I just have a standard UIViewController with a UIWebView in it that displays a pdf. For the app functionality, I have need to be able to respond to the UIWebView's nested UIScrollView events like scrollViewWillBeginDragging, scrollViewDidScroll, etc. The only way I can get access to the scrollView is to (it seems like a hack) go in and get it by the subviews array: for (id subview in webView.subviews){ if ([[subview class] isSubclassOfClass: [UIScrollView class]]) { UIScrollView * s = (UIScrollView*)subview; s.delegate = self; s.tag = 1; scrollView = s; } } But that seems to introduce more

How to subclass an OrderedDict?

我们两清 提交于 2019-12-04 09:55:45
问题 Subclassing a Python dict works as expected: >>> class DictSub(dict): ... def __init__(self): ... self[1] = 10 ... >>> DictSub() {1: 10} However, doing the same thing with a collections.OrderedDict does not work: >>> import collections >>> class OrdDictSub(collections.OrderedDict): ... def __init__(self): ... self[1] = 10 ... >>> OrdDictSub() (…) AttributeError: 'OrdDictSub' object has no attribute '_OrderedDict__root' Thus, the OrderedDict implementation uses a private __root atribute, which

Can @ManagedPropery and @PostConstruct be placed in a base class?

南楼画角 提交于 2019-12-04 08:49:58
I'm using a hierarchy of classes and what I would optimally try to do is have @ManagedBean 's that inherit a class that have @ManagedProperty members and @PostConstruct methods. Specifically, will this work? : public class A { @ManagedProperty private C c; @PostConstruct public void init() { // Do some initialization stuff } public C getC() { return c; } public void setC(C c) { this.c = c; } } @ManagedBean @SessionScoped public class B extends A { // Content... } Thanks in Advance! The @ManagedProperty is inherited and will just work that way. The @PostConstruct will also be inherited,

I'm getting an infinite loop with Swift when calling super.viewDidLoad() in two nested subclasses

谁说我不能喝 提交于 2019-12-04 07:48:06
I'm working on an iOS app written in Swift. I have a subclass of UITabBarController, and then a nested subclass: class HWTabBarController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() ... } } class MainTabBarController: HWTabBarController { override func viewDidLoad() { super.viewDidLoad() ... } } This works fine in the iOS simulator, and even when I'm debugging the app on my iPhone. But it crashes when I archive the app and send it to my phone with TestFlight. My crash logs are filled with this infinite loop: 22 HDWR 0x00145e10 @objc HDWR.MainTabBarController

How to access Abstract superclass instance variable

限于喜欢 提交于 2019-12-04 06:56:17
问题 So I have two classes: Property and Houses . Property is the abstract super class and Houses is its subclass. Here is the code for Property public abstract class Property{ String pCode; double value; int year; public Property(String pCode, double value , int year){ this.pCode = pCode; this.value = value; this.year = year; } public Property(){ pCode = ""; value = 0; year = 0; } public abstract void depreciation(); //Accessors private String getCode(){ return pCode; } private double getValue(){

Manage a UIPickerView from an External Class - Using Swift

别说谁变了你拦得住时间么 提交于 2019-12-04 06:46:44
I cannot seem to find the connection to have an external class manage a view in a ViewController. I'm new to iOS and have spent considerable looking for a solution. Simple Example: Subclass of UIPickerView I create a file that is a subclass of UIPickerView and have it conform to the PickerView delegate and datasource. class MyPickerView: UIPickerView, UIPickerViewDelegate, UIPickerViewDataSource { //In here I conform to all the required methods...no problems there } Main View Controller with Outlet for the PickerView In my MainViewController, I create an outlet for my picker view. Also, in the