简体   繁体   English

简单驯服计算器项目

[英]simple taming calculator project

class tame_dilo:

    torpor = 250

    def __init__(self, name, effect):
        self.name = name
        self.effect = effect

    def attack(self):
        self.torpor = self.torpor - self.effect

dilo = tame_dilo('dilo', 25)

dilo.attack()
print(dilo.torpor)

class tame_sable(tame_dilo):
    torpor = 500

sable = tame_sable('sable', 25)

sable.attack()
print(sable.torpor)

I just started learning some oop on python and I decide to do this little project to practice a little. 我刚刚开始学习python的一些技巧,因此决定做这个小项目来进行一些练习。
What I want to know is, if Im using the proper way to relate the name of the creature with its torpor by using inheritance and some polymorphism to define a diferent torpor according to the creatur class. 我想知道的是,Im是否使用正确的方式通过使用继承和某种多态性根据创造者类别将生物的名称与生物的名称联系起来。

And also i want to know what would be the proper method so the user can change the effect of the attack method like if you were using better equitment to knock the creature. 我也想知道什么是正确的方法,以便用户可以更改攻击方法的效果,就像您使用更好的设备来敲打生物一样。

A dilo and a sable are a type of tame. 迪洛和黑貂是驯服的一种。 They are instances, not classes. 它们是实例,而不是类。

Therefore, you need one class capable of holding different attributes. 因此,您需要一个能够容纳不同属性的类。

Also, assuming torpor is health, or energy, I'm not sure why the attack function is affecting itself. 另外,假设炸锅是健康或能量,我不确定为什么攻击功能会影响自身。 Shouldn't an instance be attacking something else? 实例不应该攻击其他东西吗?

class Tame:

    def __init__(self, name, effect, torpor):
        self.name = name
        self.effect = effect
        self.torpor = torpor

    def attack(self, other):
        other.torpor -= self.effect

Now you create named instances 现在创建命名实例

dilo = Tame('dilo', 25, 250)
sable = Tame('sable', 25, 500)
dilo.attack(sable)
print(sable.torpor)

To change the effect of a tame, just update it 要更改驯服的效果,只需对其进行更新

dilo.effect += 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM