将一个类传递给另一个类(Python) [英] Passing a class to another class (Python)

查看:447
本文介绍了将一个类传递给另一个类(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在上课时遇到了一些麻烦,而且不确定如何解决我的问题。我已经阅读了文档,但无法解决那里遇到的任何问题。

I'm having some trouble with classes at the minute, and I not sure of how to solve my problem. I've read the docs and I can't connect anything said there with the problem I'm having.

我正在尝试为游戏编写一些简单的类。我有一个武器课程和一个人物课程。我正在尝试将武器传递给Person类(我希望这是有道理的),以便Person(Bob)可以使用该武器。我在访问Weapon类中的方法和属性时遇到问题。我考虑过将Person设置为Weapon的子类,以便可以轻松调用该方法,但对我而言似乎并不直观。 。

I'm trying to make some simple classes for a game. I have a Weapon class and a Person class. I'm trying to pass a Weapon to the Person class (I hope this makes sense), so that the Person (Bob) can use the weapon. I'm having trouble accessing the methods and attributes in the Weapon class. I've considered making Person a child class of Weapon so that it can call the method easily, but that doesn't seem intuitive to me . . .

class Weapon:
    def __init__(self, weapon_name, weapon_damage):
        self.weapon_name = weapon_name
        self.weapon_damage = weapon_damage

    def display_weapon_name(self):
        print('Weapon Name: %s' %self.weapon_name)


class Person:

    def __init__(self, person_name, health, ranged_weapon):
        self.person_name = person_name
        self.health = health
        Weapon.ranged_weapon = ranged_weapon

    def display_person_info(self):
        print('Name: %s' %self.person_name)
        print('Ranged Weapon :%s' %Weapon.display_weapon_name)

def ranged_attack(self, ranged_weapon, target):
    target.health -=ranged_weapon.weapon_damage
    print("Weapon: %s" %ranged_weapon.weapon_name)
    print(target.person_name + "'s Health: "+str(target.health))

pistol = Weapon("Pistol", 40)
bob = Person("Bob", 100, pistol)

bob.display_person_info()

运行此命令会给我: / p>

Running this gives me:

Name: Bob
Ranged Weapon :<function Weapon.display_weapon_name at 0x02E23030>

运行:

bob.ranged_attack(pistol, bob)

礼物:

Weapon: Pistol
Bob's Health: 60

我的问题是,我是否正确地将Weapon对象传递给Person类?似乎很奇怪,用_init__而不是self.ranged_weapon编写Weapon.ranged_weapon。

My questions are, am I passing the Weapon object correctly to the Person class? It seems weird writing Weapon.ranged_weapon in _init__ rather than self.ranged_weapon.

如何获取display_weapon_info以显示字符串 Weapon Name:Pistol,而不是参考?当我在ranged_attack中调用它时,它似乎起作用了,但是在显示信息中却没有。

How can I get the display_weapon_info to show the string 'Weapon Name: Pistol', rather than the reference? It seems to work when I call it in ranged_attack, but not in the display info.

真的很感谢我对此提供的任何帮助。抱歉,如果以前曾问过类似的问题,但我找不到与我的问题有关的任何东西。

Really appreciate any help I can get with this. Apologies if a similar question has been asked before, but I couldn't find anything I could relate to my issue.

Rich

推荐答案

人员实际上并不需要引用武器直接上课;它只需要保存对作为 ranged_weapon 参数传递的内容的引用,并知道它可以使用该对象做什么。该代码隐式地假定 ranged_weapon 武器的实例,但将与任何与武器的瞬间。

Person doesn't actually need to reference the Weapon class directly; it just needs to save a reference to whatever is passed as the ranged_weapon argument and know what it can do with that object. The code implicitly assumes that ranged_weapon is an instance of Weapon, but will work with any object that is suitably similar to an instant of Weapon.

class Person:

    def __init__(self, person_name, health, ranged_weapon):
        self.person_name = person_name
        self.health = health
        self.weapon = ranged_weapon

    def display_person_info(self):
        print('Name: %s' %self.person_name)
        # display_weapon_name already calls print; but
        # you probably don't need this method at all.
        self.weapon.display_weapon_name()
        # Instead, do this (actually, you already do this
        # in ranged_attack())
        # print('Weapon: %s' % self.weapon.weapon_name)


    def ranged_attack(self, ranged_weapon, target):
        target.health -= self.weapon.weapon_damage
        print("Weapon: %s" % self.weapon.weapon_name)
        print(target.person_name + "'s Health: "+str(target.health))

这篇关于将一个类传递给另一个类(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆