【问题标题】:Classes usage && inheritance: am I doing wrong?类使用&&继承:我做错了吗?
【发布时间】:2015-04-23 00:25:16
【问题描述】:

我刚刚开始在 Python 2.7 中尝试 OOP。

这是我目前为我的项目编写的代码示例。 这只是“模板”——不是真正的应用程序代码,但该应用程序将使用相同的类“方案”,如下例所示。

我想知道 - 我在这里做错了什么?类初始化,一些错误的继承使用,其他错误?

它有效,但是 - 可能我对某些“意识形态”观点有误吗?

文件:main.py - 程序主体; cl1.py - 一等cl2.py - 第二类。

main.py:

#!/usr/bin/env python

import cl1, cl2

print('\nStarted Main script.\n')

cl1 = cl1.First()
cl1.one('This is first_arg')

cl2 = cl2.Second()
cl2.two('This is second arg')

cl1.py:

class First:

    def one(self, first_arg):

        self.third_arg = 'This is third arg from First class'

        self.first_arg = first_arg
        print('This is method \'one\' from class First.\nWork with first_arg = %s\n' % self.first_arg)

cl2.py:

from cl1 import First

class Second(First):

    def two(self, second_arg):

        self.second_arg = second_arg
        print('This is method \'two\' from class Second.\nWork with second_arg = %s\n' % self.second_arg)

        self.one('I came from Second.two() to First.one()')

        print('I came from First.one() as self.third_arg to Second.two(): %s\n' % self.third_arg)

结果:

$ ./main.py

Started Main script.

This is method 'one' from class First.
Work with first_arg = This is first_arg

This is method 'two' from class Second.
Work with second_arg = This is second arg

This is method 'one' from class First.
Work with first_arg = I came from Second.two() to First.one()

I came from First.one() as self.third_arg to Second.two(): This is third arg from First class

【问题讨论】:

  • 您的实际问题是什么? ideological 是什么意思?从这里开始,在我看来,您从另一个类继承并在其中定义了额外的方法。怎么了?
  • 将它发布到 CR 可能会更好?..

标签: python class oop python-2.7


【解决方案1】:

您不应该在其方法中on-the-fly 创建类的属性。一个类在初始化时应该知道它从超类继承了哪些属性。您应该只在__init__ 中创建实例变量,这是一种特殊的构造方法。

class First(object):
    def __init__(self, first_arg):
        self.first_arg = first_arg

    @property
    def one(self):
        return self.first_arg

    @one.setter
    def one(self, value):
        self.first_arg = value

>>> first = First(5)
>>> print first.one
5
>>> first.one = 10
>>> print first.one
10

如果您想通过创建一个名为Second 的新类来为First 类添加额外的属性,您应该始终首先在子类的构造函数中继承超类的属性:

class Second(First):
    def __init__(self, first_arg, second_arg):
        super(Second, self).__init__(first_arg) # now you have "self.first_arg"
        self.second_arg = second_arg

    @property
    def two(self):
        return self.second_arg

    @two.setter
    def two(self, value):
        self.second_arg = value

>>> second = Second(7, 10)
>>> print second.one
7
>>> print second.two
10
>>> second.two = 20
>>> second.one = 15
...

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我会在这段代码中快速说出你的问题:

    您的命名变量类似于一、二、cl1、cl2。 这些都是毫无意义的,很难理解,尤其是对于像你这样的初学者。你更容易想到一些现实世界的例子并尝试为此构建一个结构。

    就我个人而言,我总是创建有关我玩的游戏的课程,因此所有内容对我来说似乎都合乎逻辑且易于学习。

    示例:您正在玩《使命召唤》。然后你会有以下类:士兵、平民、武器、地图等等。 士兵会有方法:

    • Soldier.Run()
    • Soldier.Shoot()
    • Soldier.ReloadWeapon()

    AmericanSoldier 和 GermanSoldier 都将继承基本的 Soldier 类,并且编写代码是合乎逻辑的(而且很有趣)!

    我还没有学习一种新的编程语言,使用“Var_1,Var_2”的自闭症指南的愚蠢示例,它是“Class1”的一部分。

    【讨论】:

    • 谢谢。我总是尝试在我的代码中为 vars 提供“重要”的名称。这是这样的名称,只是因为这是“快速示例”代码。但你是绝对正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2023-03-31
    • 2012-03-03
    • 2010-11-14
    • 1970-01-01
    • 2011-04-21
    相关资源
    最近更新 更多