inheritance

Should Player inherit or own a Level?

一世执手 提交于 2020-01-05 10:30:18
问题 I've been trying to learn OOP for the last few weeks as much as I can, and I've learned alot, but I'm not certain of this one, what should my class hierarchy look like? Imagine two classes, here's a Level -class: class Level(object): def __init__(self, level, exp): self.level = level self.exp = exp @property def required_exp(self, level=None): return 80 + 40 * (level or self.level) def add_exp(self, exp): self.exp += exp while self.exp >= self.required_exp: self.exp -= self.required_exp self

Python: Change class type with decorator and keep it's methods

时光总嘲笑我的痴心妄想 提交于 2020-01-05 10:10:14
问题 I want to create a class which could be used inside different Applications and their APIs to create UIs. Therefor I created a module called ui.py . Inside this module is the following: from PyQt4 import QtGui def CreateGui(uiFile, parent=None): print "Ui build.." def GetUiObject(uiClass): pUI = uiClass.PARENT class ParentUI(pUI): def __init__(self, uiFile): CreateGui(uiFile, self) def __call__(self, cls): for func in uiClass.__dict__: setattr(cls, func, uiClass.__dict__[func]) return ParentUI

Reading and writing a vector of classes

安稳与你 提交于 2020-01-05 08:43:50
问题 I have an inheritance hierarchy with three/four levels. And within each level will hold at least one or more classes with different attributes that make that object of a class unique (and of course, inheriting attributes from the level above). Each object of a class may have different attributes to another therefore my question is, how can I read and write each object to a file and differentiate the attributes? I do apologise if I have not worded this very well but will be very appreciative

Why Dart does not inherit constructor?

廉价感情. 提交于 2020-01-05 08:01:11
问题 I am coming from PHP world and am curious why developers choose the way not adding constructor (with arg) to inheritance. From my view it violates DRY principle by repeating a lot of code, depending on structure. I did little research - PHP, Ruby, Python inherits constructor. Java, C#, C++ not. C++0x have new feature explicitly defining inheritances. So is there any advantage for programmer not having constructor inherited and explicitly write constructor again and again? 回答1: I think you'd

In Lua, what is the difference between functions that use “:” and functions that do not? [duplicate]

我只是一个虾纸丫 提交于 2020-01-05 08:00:16
问题 This question already has answers here : Difference between . and : in Lua (3 answers) Closed 3 years ago . Let's say we have two function declarations: function MyData:New end and function New(MyData) end What is the difference between them? Does using : have any special purpose when it comes to inheritance and OOP? Can I only call functions declared with : by using : ? I'm coming from using only C# -- so if there's any comparison to be made, what would it be? 回答1: You should search SO as

protobuf-net deserialize base class to inherited class

≯℡__Kan透↙ 提交于 2020-01-05 07:21:20
问题 I have base class which is serialized. [ProtoContract] public class Web2PdfEntity { [ProtoMember(1)] public string Title { get; set; } [ProtoMember(2)] public string CUrl { get; set; } } I would like to deserialize Web2PdfEntity class to Web2PdfServer which is inherited from Web2PdfEntity. public class Web2PdfServer : Web2PdfEntity { public void MyServerMethod {} public void MyServerMethod2{} } I have tried to use code below to deserialize class, unfortunately the properties are not set. var

static const variables in abstract baseclass

纵饮孤独 提交于 2020-01-05 07:17:13
问题 I've got a abstract baseclass, this is used for deriving some classes. Some properties of these classes are shared among all classes, and these should be unmodifiable. To make a variable shared among all 10 classes I'll make it static. class ABC{ public: static int *anArray; int index; static int tot_index; virtual void print()=0; ABC(){index=tot_index++;}; virtual ~ABC(){}; }; This works fine, tot_index will contain the number of classes instantiated, and the index is the unique indentifier

“Attempting to use incompatible return type” with class implementing Serializable

坚强是说给别人听的谎言 提交于 2020-01-05 06:14:48
问题 I have the following interface: Slicer public interface Slicer { Optional<Map<String, ? extends Serializable>> pointer(); } of which I have an implementation: DynamoDbSlicer public abstract class DynamoDbSlicer implements Slicer { @Override public abstract Optional<Map<String, AttributeValue> pointer(); } where AttributeValue is from the AWS SDK and defined as: public final class AttributeValue implements SdkPojo, Serializable, ToCopyableBuilder<AttributeValue.Builder, AttributeValue> { Note

“Attempting to use incompatible return type” with class implementing Serializable

混江龙づ霸主 提交于 2020-01-05 06:14:25
问题 I have the following interface: Slicer public interface Slicer { Optional<Map<String, ? extends Serializable>> pointer(); } of which I have an implementation: DynamoDbSlicer public abstract class DynamoDbSlicer implements Slicer { @Override public abstract Optional<Map<String, AttributeValue> pointer(); } where AttributeValue is from the AWS SDK and defined as: public final class AttributeValue implements SdkPojo, Serializable, ToCopyableBuilder<AttributeValue.Builder, AttributeValue> { Note

Assigning child class to a parent class typed property

↘锁芯ラ 提交于 2020-01-05 05:45:14
问题 I do have the following 2 base clases: class BaseModel {} class BaseService{ protected model:BaseModel; } Now I want to implement BaseHelper and BaseService for a specific use case and assign a derived class to my property. class MyModel extends BaseModel{ constructor(param:string){ super(); } } class MyService extends BaseService { model = MyModel; } However, this gives me the error Type 'typeof MyModel' is not assignable to type 'BaseModel'. Important: I want to attach the class MyModel,