subclass

Why shouldn't I subclass a UIButton?

一笑奈何 提交于 2019-11-26 02:58:01
问题 I\'ve asked a few questions on stack overflow about subclassing a UIButton , and a couple of people have informed me that I shouldn\'t subclass a UIButton . What are the negatives of subclassing a UIButton ? And I know it\'s vague, but what are other alternatives to subclassing a UIButton ? 回答1: The Cocoa frameworks take the approach that the Object Composition pattern is more appropriate than traditional class hierarchy. In general, this means that there is likely to be a property on

why java polymorphism not work in my example

谁都会走 提交于 2019-11-26 02:25:07
问题 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;

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

孤街浪徒 提交于 2019-11-26 00:57:25
问题 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? 回答1: Apparently, no. Here's the options: Use Type.IsSubclassOf Use Type.IsAssignableFrom is and as Type.IsSubclassOf

How do you find all subclasses of a given class in Java?

旧城冷巷雨未停 提交于 2019-11-25 22:30:43
问题 How does one go about and try to find all subclasses of a given class (or all implementors of a given interface) in Java? As of now, I have a method to do this, but I find it quite inefficient (to say the least). The method is: Get a list of all class names that exist on the class path Load each class and test to see if it is a subclass or implementor of the desired class or interface In Eclipse, there is a nice feature called the Type Hierarchy that manages to show this quite efficiently.