subclass

Why can't I subclass datetime.date?

江枫思渺然 提交于 2019-11-27 04:37:35
Why doesn't the following work (Python 2.5.2)? >>> import datetime >>> class D(datetime.date): def __init__(self, year): datetime.date.__init__(self, year, 1, 1) >>> D(2008) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: function takes exactly 3 arguments (1 given) I wanted to create a class that was just like datetime.date , but with a different __init__ function. Apparently my function never gets called. Instead the original datetime.date.__init__ is called and fails because that expects 3 arguments and I am passing in one. What's going on here? And is this

How do I check if an object's type is a particular subclass in C++?

拟墨画扇 提交于 2019-11-27 03:48:29
I was thinking along the lines of using typeid() but I don't know how to ask if that type is a subclass of another class (which, by the way, is abstract) You really shouldn't. If your program needs to know what class an object is, that usually indicates a design flaw. See if you can get the behavior you want using virtual functions. Also, more information about what you are trying to do would help. I am assuming you have a situation like this: class Base; class A : public Base {...}; class B : public Base {...}; void foo(Base *p) { if(/* p is A */) /* do X */ else /* do Y */ } If this is what

javax.el.PropertyNotFoundException when submitting ui:repeat with conditionally rendered properties of different subclasses

六月ゝ 毕业季﹏ 提交于 2019-11-27 02:58:48
问题 In my backing-bean I have a collection of objects of different subclasses sharing a common interface. Inside the view, an ui:repeat iterates over this collection. Inside this loop, different properties have to be rendered depending on the concrete implementation of the interface. I reduced the problem to the following backing bean: @Named @SessionScoped public class DummyBean implements Serializable { private List<Type> objects = new ArrayList<Type>(); public void add1() { objects.add(new

coding variable values into classes using R

百般思念 提交于 2019-11-27 02:58:05
问题 I have a set of data in which I need to code values of certain variables (numeric) into 3 classes. My data set is similar to this but has 60 more variables: anim <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15) wt <- c(181,179,180.5,201,201.5,245,246.4,189.3,301,354,369,205,199,394,231.3) data <- data.frame(anim,wt) > data anim wt 1 1 181.0 2 2 179.0 3 3 180.5 4 4 201.0 5 5 201.5 6 6 245.0 7 7 246.4 8 8 189.3 9 9 301.0 10 10 354.0 11 11 369.0 12 12 205.0 13 13 199.0 14 14 394.0 15 15 231.3 I need to

Java Polymorphism How to call to super class method for subclass object

我的未来我决定 提交于 2019-11-27 02:45:22
问题 Here is an example of what I am trying to ask superclass Name.java public class Name{ protected String first; protected String last; public Name(String firstName, String lastName){ this.first = firstName; this.last = lastName; } public String initials(){ String theInitials = first.substring(0, 1) + ". " + last.substring(0, 1) + "."; return theInitials; } and then the subclass is ThreeNames.java public class ThreeNames extends Name{ private String middle; public ThreeNames(String aFirst,

Subclass of class with synthesized readonly property cannot access instance variable in Objective-C

假装没事ソ 提交于 2019-11-27 02:05:00
问题 In the superclass MyClass : @interface MyClass : NSObject @property (nonatomic, strong, readonly) NSString *pString; @end @implementation MyClass @synthesize pString = _pString; @end In the subclass MySubclass @interface MySubclass : MyClass @end @implementation MySubclass - (id)init { if (self = [super init]) { _pString = @"Some string"; } return self; } The problem is that the compiler doesn't think that _pString is a member of MySubclass , but I have no problem accessing it in MyClass .

Calling superclass from a subclass constructor in Java

蹲街弑〆低调 提交于 2019-11-27 01:59:31
I am trying to create a constructor that takes a field as a parameter, then puts it in a field that is stored in a superclass. Here is the code I am using public crisps(String flavour, int quantity) { this.flavour = super.getFlavour(); this.quantity = quantity; } In the superclass I have initialised the field with private String flavour; and I have an accessor method public String getFlavour() { return flavour; } I am getting an error " flavour has private access in the superclass ", but I believe this shouldn't matter as I am calling the accessor method that returns it to the field? What you

Avoid specifying all arguments in a subclass

岁酱吖の 提交于 2019-11-27 01:50:26
问题 I have a class: class A(object): def __init__(self,a,b,c,d,e,f,g,...........,x,y,z) #do some init stuff And I have a subclass which needs one extra arg (the last W ) class B(A): def __init__(self.a,b,c,d,e,f,g,...........,x,y,z,W) A.__init__(self,a,b,c,d,e,f,g,...........,x,y,z) self.__W=W It seems dumb to write all this boiler-plate code, e.g passing all the args from B 's Ctor to the inside call to A 's ctor, since then every change to A 's ctor must be applied to two other places in B 's

How to check if a subclass is an instance of a class at runtime? [duplicate]

一曲冷凌霜 提交于 2019-11-27 00:47:59
This question already has an answer here: Check if a Class Object is subclass of another Class Object in Java 7 answers In an android app test suite I have a class like this where B is a view: public class A extends B { ... etc... } now I have a list of view objects which may contain A objects but in this case I only care if they're subclasses or "instances of" B . I'd like to do something like: ArrayList<View> viewList = getViews(); Iterator<View> iterator = viewList.iterator(); while (iterator.hasNext() && viewList != null) { View view = iterator.next(); if (view.getClass().isInstance(B

How to test if one java class extends another at runtime?

↘锁芯ラ 提交于 2019-11-27 00:00:55
How to I test if a is a subclass of b ? Class<?> a = A.class; Class<?> b = B.class; Are you looking for: Super.class.isAssignableFrom(Sub.class) Rob Hruska If you want to know whether or not a Class extends another, use Class#isAssignableFrom(Class) . For your example, it would be: if(B.class.isAssignableFrom(A.class)) { ... } If you're interested in whether or not an instance is of a particular type, use instanceof : A obj = new A(); if(obj instanceof B) { ... } Note that these will return true if the class/instance is a member of the type hierarchy and are not restrictive to direct