【发布时间】: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