subclass

why java polymorphism not work in my example

风流意气都作罢 提交于 2019-11-26 11:24:53
I have these 4 java clases: 1 public class Rect { double width; double height; String color; public Rect( ) { width=0; height=0; color="transparent"; } public Rect( double w,double h) { width=w; height=h; color="transparent"; } double area() { return width*height; } } 2 public class PRect extends Rect{ double depth; public PRect(double w, double h ,double d) { width=w; height=h; depth=d; } double area() { return width*height*depth; } } 3 public class CRect extends Rect{ String color; public CRect(double w, double h ,String c) { width=w; height=h; color=c; } double area() { return width*height;

Subclassing tuple with multiple __init__ arguments

醉酒当歌 提交于 2019-11-26 11:22:05
The following code works: class Foo(tuple): def __init__(self, b): super(Foo, self).__init__(tuple(b)) if __name__ == '__main__': print Foo([3, 4]) $ python play.py Result: play.py:4: DeprecationWarning: object.__init__() takes no parameters super(Foo, self).__init__(tuple(b)) (3, 4) But not the following: class Foo(tuple): def __init__(self, a, b): super(Foo, self).__init__(tuple(b)) if __name__ == '__main__': print Foo(None, [3, 4]) $ python play.py Result: Traceback (most recent call last): File "play.py", line 7, in <module> print Foo(None, [3, 4]) TypeError: tuple() takes at most 1

Is calling super in a category the same as calling it in a subclass?

旧巷老猫 提交于 2019-11-26 11:17:43
问题 Does calling [super init] do the same thing in a category as a subclass? If not, what\'s the difference? 回答1: In order to understand this, it's probably important to understand the way an object is stored during runtime. There is a class object 1 , which holds all the method implementations, and separately, there is a structure with the storage for the instance's variables. All instances of a class share the one class object. When you call a method on an instance, the compiler turns that into

Why can&#39;t I subclass datetime.date?

我与影子孤独终老i 提交于 2019-11-26 11:17:25
问题 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

How do I check if an object&#39;s type is a particular subclass in C++?

大城市里の小女人 提交于 2019-11-26 10:39:37
问题 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) 回答1: 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 {...};

How do I check if a type is a subtype OR the type of an object?

送分小仙女□ 提交于 2019-11-26 09:44:39
To check if a type is a subclass of another type in C#, it's easy: typeof (SubClass).IsSubclassOf(typeof (BaseClass)); // returns true However, this will fail: typeof (BaseClass).IsSubclassOf(typeof (BaseClass)); // returns false Is there any way to check whether a type is either a subclass OR of the base class itself, without using an OR operator or using an extension method? Lasse Vågsæther Karlsen Apparently, no. Here's the options: Use Type.IsSubclassOf Use Type.IsAssignableFrom is and as Type.IsSubclassOf As you've already found out, this will not work if the two types are the same, here

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

佐手、 提交于 2019-11-26 09:29:01
问题 This question already has answers here : Check if a Class Object is subclass of another Class Object in Java (7 answers) Closed 3 years ago . 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 =

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

依然范特西╮ 提交于 2019-11-26 09:16:53
问题 How to I test if a is a subclass of b ? Class<?> a = A.class; Class<?> b = B.class; 回答1: Are you looking for: Super.class.isAssignableFrom(Sub.class) 回答2: 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

Understanding upper and lower bounds on ? in Java Generics

百般思念 提交于 2019-11-26 08:59:54
问题 I am really having a tough time understanding the wild card parameter. I have a few questions regarding that. ? as a type parameter can only be used in methods. eg: printAll(MyList<? extends Serializable>) I cannot define classes with ? as type parameter. I understand the upper bound on ? . printAll(MyList<? extends Serializable>) means: \" printAll will print MyList if it has objects that implement the Serialzable interface. \" I have a bit of an issue with the super . printAll(MyList<?

Calling superclass from a subclass constructor in Java

為{幸葍}努か 提交于 2019-11-26 08:25:14
问题 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