python模块的使用方法_类的创建与调用

python模块的使用方法_类的创建与调用

 

eml = employee.Employee() #默认调用模块的__init__.py
employee文件夹就是模块,与main.py主程序在同一个项目的文件夹

main.py
# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import product
import sys, pprint
#import employee
import employee.task


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    product2 = product.Product()
    product2.set_name_func('tree')

    get_msg = product2.get_name()
    print('set_name:', get_msg)
    print('global:', product2.public_name)
    import os
    print('当前路径:', os.getcwd())#获取当前路径
    #sys.path.append(os.getcwd()+'\\employee') #增加路径类似于include目录包含文件
    pprint.pprint(sys.path) #打印多行
    eml = employee.Employee() #默认调用模块的__init__.py
    eml.set_wwid('987654')
    print('wwid:', eml.get_wwid())
   # print('wwid2:', eml.wwid)

    task2 = employee.task.Employee_task()
    task2.set_run('true')
    print('employee_action:', task2.get_run())

    task2.set_walk('fast')
    print('employee_action:', task2.get_walk())








# See PyCharm help at https://www.jetbrains.com/help/pycharm/

__init__.py

class Employee:
    __wwid = ''  # 两个__下划线表示private私有变量
    __name = ''
    wwid = 'cd987' # public公有变量

    def set_wwid(self, wwid2):
        self.__wwid = wwid2
        # print('__wwid_module:', self.__wwid)

    def get_wwid(self):
        # print('__wwid_module_get:',self. __wwid)
        return self.__wwid

task.py

class Employee_task:
    __run = ''
    __walk = ''
    def set_run(self,run2):
        self.__run=run2
    def set_walk(self,walk2):
        self.__walk=walk2
    def get_run(self):
        return self.__run
    def get_walk(self):
        return self.__walk
#sys.path.append(os.getcwd()+'\\employee') #增加路径类似于include目录包含文件
pprint.pprint(sys.path) #打印多行

运行效果:

 

posted @ 2024-01-01 21:03  txwtech  阅读(17)  评论(0编辑  收藏  举报