51Testing软件测试论坛

标题: Day4-5测试积点任务 [打印本页]

作者: 测试积点老人    时间: 2018-3-21 10:11
标题: Day4-5测试积点任务
问题
python两个类如何相互调用方法?
如图项目中有两个文件,每个文件中代码如下:
  1. <font face="微软雅黑" size="3">test1:
  2. __author__ = 'dage'

  3. from test2 import *
  4. class Test1(object):

  5.     print ('hello world!')
  6.     print('hello!')
  7.     def result(self):
  8.         t2 = Test2()
  9.         cc = t2.addnumber(2, 3)

  10.         print (cc)

  11. t1 = Test1()
  12. t1.result()

  13. test2:
  14. class Test2(object):
  15.     print (u'链接手机并安装执行位置的!')
  16.     def addnumber(self,a,b):
  17.         c = a + b
  18.         return c

  19.     def printt(self):
  20.         print ('hi')

  21. if __name__ == '__main__':
  22.     s = Test2()
  23.     s.printt()
  24. </font>
复制代码


类:class Test1(object):
可以写成:class Test1:
还可以写成:class Test1(父类):


调用类方法:
1、import 引入
2、实例化   tt = Test1()
3、调用:tt.addnumber()


addnumber(self,a,b) 调用的时候,不需要考虑self


作者: 清晨一缕阳光    时间: 2018-3-22 10:20
Python不懂!期待大神回答,学习下!
作者: jingzizx    时间: 2018-3-22 10:32
学习
作者: 梦想家    时间: 2018-3-22 10:59
期待python大神
作者: 自动化小白    时间: 2018-3-22 11:01
类的内部调用:<方法名>(参数列表)。
作者: qqq911    时间: 2018-3-22 11:09
class Car:#外部类
    class Door:#内部类
        def open(self):
            print('open door')
        
    class Wheel:
        def run(self):
            print('car run')
            
if __name__=="__main__":
    car=Car()#实例化外部类
    backDoor=Car.Door()#实例化内部类 第一种方法

    frontDoor=car.Door()#因为car已经实例化外部类,再次实例化Car的内部类 第二种方法
    backDoor.open()
    frontDoor.open()
    wheel=car.Wheel()#car已经实例化外部类,Wheel()再次实例化内部类
    wheel.run()#调用内部类的方法




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2