类 usage && inheritance:我做错了吗?
Classes usage && inheritance: am I doing wrong?
我刚开始在 Python 2.7.
中尝试 OOP
这是我目前为我的项目编写的代码示例。
这只是 'template' - 不是来自应用程序的真实代码,但此应用程序将使用相同的 classes "scheme",如下例所示。
我想知道 - 我在这里做错了什么? 类 初始化,一些错误的继承用法,其他的错误?
它有效,但是 - 可能是我在某些 'ideological' 点上有误?
文件:main.py
- 程序主体; cl1.py
- 第一 class; cl2.py
- 第二 class.
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
您不应在其方法中即时创建 class 的属性。 class 应该知道它在初始化时从超级 class 继承了哪些属性。您应该只在 __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
的新 class 向 First
添加一个额外的 属性 class,您应该始终首先继承super class 在 subclass 的构造函数中的属性:
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
...
希望对您有所帮助。
我会在这段代码中快速命名您的问题:
你的命名变量就像一、二、cl1、cl2。 那些是无意义的,而且很难理解,尤其是像你这样的初学者。想到一些现实世界的例子并尝试为此构建一个结构对你来说要容易得多。
就我个人而言,我总是 class 关于我玩的游戏,这样一来,一切对我来说似乎都是合乎逻辑且易于学习的。
示例:您正在玩使命召唤。然后你会有以下classes:士兵,平民,武器,地图等等。
士兵将有方法:
- Soldier.Run()
- Soldier.Shoot()
- Soldier.ReloadWeapon()
AmericanSoldier 和 GermanSoldier 都将继承基础 Soldier class 并且编写代码完全合乎逻辑(而且很有趣)!
我还没有学习一种新的编程语言,使用 "Var_1, Var_2" 自闭症指南的愚蠢示例,"Class1"。
我刚开始在 Python 2.7.
中尝试 OOP这是我目前为我的项目编写的代码示例。 这只是 'template' - 不是来自应用程序的真实代码,但此应用程序将使用相同的 classes "scheme",如下例所示。
我想知道 - 我在这里做错了什么? 类 初始化,一些错误的继承用法,其他的错误?
它有效,但是 - 可能是我在某些 'ideological' 点上有误?
文件:main.py
- 程序主体; cl1.py
- 第一 class; cl2.py
- 第二 class.
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
您不应在其方法中即时创建 class 的属性。 class 应该知道它在初始化时从超级 class 继承了哪些属性。您应该只在 __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
的新 class 向 First
添加一个额外的 属性 class,您应该始终首先继承super class 在 subclass 的构造函数中的属性:
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
...
希望对您有所帮助。
我会在这段代码中快速命名您的问题:
你的命名变量就像一、二、cl1、cl2。 那些是无意义的,而且很难理解,尤其是像你这样的初学者。想到一些现实世界的例子并尝试为此构建一个结构对你来说要容易得多。
就我个人而言,我总是 class 关于我玩的游戏,这样一来,一切对我来说似乎都是合乎逻辑且易于学习的。
示例:您正在玩使命召唤。然后你会有以下classes:士兵,平民,武器,地图等等。 士兵将有方法:
- Soldier.Run()
- Soldier.Shoot()
- Soldier.ReloadWeapon()
AmericanSoldier 和 GermanSoldier 都将继承基础 Soldier class 并且编写代码完全合乎逻辑(而且很有趣)!
我还没有学习一种新的编程语言,使用 "Var_1, Var_2" 自闭症指南的愚蠢示例,"Class1"。