oop

python inheritance: choose parent class using argument

北城余情 提交于 2021-01-27 14:48:29
问题 I'm having trouble designing some classes. I want my user to be able to use the Character() class by passing in an argument for the type of character (e.g. fighter/wizard). Dummy code: class CharClass(): def __init__(self, level): self.level = level class Fighter(CharClass): # fighter stuff pass class Wizard(CharClass): # wizard stuff pass class Character(): #? def __init__(self, char_class): # should inherit from Fighter/Wizard depending on the char_class arg pass For example, after calling:

Circular dependency in Spring injection - Is this bad design?

房东的猫 提交于 2021-01-27 13:40:39
问题 I am stuck with following issue : I am trying to create beans as follows: @Bean public abc createABC() { return new ABC(--, def(),--); } ` @Bean public DEF def() { return new DEF(--, createABC(),-- } Any suggestions to get around this problem without chaging to setter based injection. Is it the indicative of bad design? In my situation this dependency is must. Please provide your viewpoints on this 回答1: it the indicative of bad design? Absolutely. If ABC depends on DEF and DEF depends on ABC

Circular dependency in Spring injection - Is this bad design?

淺唱寂寞╮ 提交于 2021-01-27 13:25:56
问题 I am stuck with following issue : I am trying to create beans as follows: @Bean public abc createABC() { return new ABC(--, def(),--); } ` @Bean public DEF def() { return new DEF(--, createABC(),-- } Any suggestions to get around this problem without chaging to setter based injection. Is it the indicative of bad design? In my situation this dependency is must. Please provide your viewpoints on this 回答1: it the indicative of bad design? Absolutely. If ABC depends on DEF and DEF depends on ABC

Python abstract property() “Can't instantiate abstract class [] with abstract methods”, but I did

有些话、适合烂在心里 提交于 2021-01-27 13:19:33
问题 I'm trying to create a base class with a number of abstract python properties, in python 3.7. I tried it one way (see 'start' below) using the @property, @abstractmethod, @property.setter annotations. This worked but it doesn't raise an exception if the subclass doesn't implement a setter. That's the point of using @abstract to me, so that's no good. So I tried doing it another way (see 'end' below) using two @abstractmethod methods and a 'property()', which is not abstract itself but uses

How to limit protected member to be accessible from only first level child C#

筅森魡賤 提交于 2021-01-27 13:03:35
问题 I have 3 level classes. TopClass MiddleClass BottomClass And MiddleClass is derived from TopClass and BottomClass is derived from MiddleClass . I want to have a property in TopClass which could only be accessible from TopClass and MiddleClass not the BottomClass . As I am using protected access modifier, the protected property in TopClass is accessible from BottomClass . Here are my classes to give better insight. public class TopClass { private string ThisIsOnlyAccessibleForTopClass { get;

java initialize base class fields in subclass constructor

非 Y 不嫁゛ 提交于 2021-01-27 13:01:35
问题 This is a very basic question about subclasses in java, I still don't get it... Suppose I have a superclass with three fields and with only the default constructor: public class Superclass { public int a; public int b; public int c; } and I want to add a field x. I cannot change Superclass , so I make a subclass: public class Subclass extends Superclass { public int x; public Subclass(Superclass s) { super(); // what to do?? } } I now want to generate a Subclass object from an existing

Java classes and interface combination

白昼怎懂夜的黑 提交于 2021-01-27 12:53:17
问题 I'm trying to create couple of Java class to perform certain work. Let's say I want to get the task done by calling my classes like this: FirebaseAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInWithCredential:success"); FirebaseUser user = task.getResult()

How to think about polymorphism with subtyping

我们两清 提交于 2021-01-27 12:09:40
问题 The Liskov Substitution Principle states: Invariants of the supertype must be preserved in a subtype. I'm particularly interested with the intersection of this principle and polymorphism. In particular subtype-polymorphism though, in fact, this seems to be the case with parametric polymorphism and Haskell type classes. So, I know that functions are subtypes when their arguments are contravariant and their return types covariant. We can assume that methods are just functions with an implicit

How to think about polymorphism with subtyping

╄→尐↘猪︶ㄣ 提交于 2021-01-27 12:05:50
问题 The Liskov Substitution Principle states: Invariants of the supertype must be preserved in a subtype. I'm particularly interested with the intersection of this principle and polymorphism. In particular subtype-polymorphism though, in fact, this seems to be the case with parametric polymorphism and Haskell type classes. So, I know that functions are subtypes when their arguments are contravariant and their return types covariant. We can assume that methods are just functions with an implicit

Python change self to inherited class

こ雲淡風輕ζ 提交于 2021-01-27 07:32:56
问题 I have this kind of structure: class Foo: def __init__(self, val1): self.val1 = val1 def changeToGoo(self) HOW??? class Goo(Foo): def __init__(self, val1, val2): super(val1) self.val2 = val2 a = Foo(1) a.changeToGoo() 'a' is now an instance of Foo now i would like to change it to be an instance of Goo, by using the method "changeToGoo", and add the other value. How can this be done in Python? I have tried: self.__class__ = Goo but when I check: type(a) it's still Foo, and not Goo. 回答1: In