superclass

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); }

python super calling child methods

笑着哭i 提交于 2019-12-04 08:38:08
There are numerous questions on the usage of super() but none of them appears to answer my question. When calling super().__init__() from a subclass, all method calls in the super-constructor are actually taken from the subclass . Consider the following class structure: class A(object): def __init__(self): print("initializing A") self.a() def a(self): print("A.a()") class B(A): def __init__(self): super().__init__() # add stuff for B self.bnum=3 # required by B.a() def a(self): print("B.a(), bnum=%i"%self.bnum) b=B() which fails with initializing A Traceback (most recent call last): File

Python super() behavior not dependable

房东的猫 提交于 2019-12-04 07:56:13
问题 For some reason, the super() method is not always behaving as expected, opting to return: TypeError('super(type, obj): obj must be an instance or subtype of type)' I understand what the error means . I do not understand why it is coming up as an error. Here's the snippet of code that is breaking. All objects in the system are new style objects. What's really interesting is that this error does not always show up. I don't know what's causing it. The super() method in Retrieval is passing the

Subclass with additional method in Java

冷暖自知 提交于 2019-12-04 06:24:16
问题 I have a problem with a subclass in Java. I have the following class: MainIterator implements Iterator{ //... Methods from the interface Iterator public void foo(){ //Do something... } } and in my main class I call: Iterator i = new MainIterator(); i.foo(); which does not work, because Iterator does not implement the function foo() . How is it possible to get this working without a typecast of i ? i has to be from type Iterator. 回答1: MainIterator i = new MainIterator(); The Java compiler only

Can I mock a superclass's constructor with Mockito/Powermock?

一笑奈何 提交于 2019-12-04 04:57:47
Is it possible using Mockito and optionally Powermock to mock a superclass S such that any calls to the superclass to S (including calls to the S() constructor) are mocked? So using the below example, if I replace S with MockS using Mockito, will the call to super() use the constructor in MockS ? class S { S() { // Format user's hard drive, call 911, and initiate self-destruct } } class T extends S { T() { super(); } } class Test { @Mock private S mockS; new T(); // T's call to super() should call the mock, not the destructive S. } I've seen questions about mocking individual methods in S or

Changing the value of superclass instance variables from a subclass

▼魔方 西西 提交于 2019-12-04 04:03:27
I've found that I can do it this way in the child class: ParentClass.variable = value; But I've been told that it's better practice to use get/set methods and not give direct access to variables outside a class. Though this was for when I had an instance of the class in another class, not for subclasses and superclasses. So is there a better way of doing this, and which way is generally considered best practice? You have a lot of options. super.field = x You have to have access to the field to do this field = x You have to have access to the field to do this. You also can't have another field

why should we use Exception as a superclass, why not BaseException

こ雲淡風輕ζ 提交于 2019-12-04 03:31:33
问题 In python, whenever we are writing User-defined exception, we have to extend it from class Exception . my question is why can't we extend it from BaseException which is super-class of exception hierarchy and Exception is also subclass of BaseException . 回答1: BaseException includes things like KeyboardInterrupt and SystemExit , which use the exception mechanism, but which most people shouldn't be catching. It's analogous to Throwable in Java, if you're familiar with that. Things that derive

What is the best practice for finding out all superclasses of a Perl class?

会有一股神秘感。 提交于 2019-12-04 03:05:44
问题 Is there a standard CPAN way of finding out all the superclasses of a Perl class (or better yet entire superclass tree, up to UNIVERSAL)? Or is the best practice to simply examine @{"${$class}::ISA"} for each class, class's parents etc...? 回答1: There is no "standard way" because this is not a standard thing you want to do. For anything other than visualization it is an OO red flag to want to inspect your inheritance tree. In addition to Class::ISA, there is mro::get_linear_isa(). Both have

General 'map' function for Scala tuples?

橙三吉。 提交于 2019-12-04 02:44:03
I would like to map the elements of a Scala tuple (or triple, ...) using a single function returning type R. The result should be a tuple (or triple, ...) with elements of type R. OK, if the elements of the tuple are from the same type, the mapping is not a problem: scala> implicit def t2mapper[A](t: (A,A)) = new { def map[R](f: A => R) = (f(t._1),f(t._2)) } t2mapper: [A](t: (A, A))java.lang.Object{def map[R](f: (A) => R): (R, R)} scala> (1,2) map (_ + 1) res0: (Int, Int) = (2,3) But is it also possible to make this solution generic, i.e. to map tuples that contain elements of different types

Calling parent's __call__ method within class

瘦欲@ 提交于 2019-12-04 01:06:14
问题 I'd like to call a parent's call method from inherited class Code looks like this #!/usr/bin/env python class Parent(object): def __call__(self, name): print "hello world, ", name class Person(Parent): def __call__(self, someinfo): super(Parent, self).__call__(someinfo) p = Person() p("info") And I get, File "./test.py", line 12, in __call__ super(Parent, self).__call__(someinfo) AttributeError: 'super' object has no attribute '__call__' And I can't figure out why, can somebody please help me