学习Python③:Python中的类、对象(继承和多态)、模块、包

  今天学习了Python的类和对象,包括继承和多态,还有模块和包的使用,通过代码学习,记录下来以备后查。

# 类和object   所有的数据类型都是对象,
import datetime                                      # 内置模块
from sys import path,version_info                    # 内置模块,只引用sys模块中的path和version_info,如果是*代表引用所有的
if 'G:\\Python\\demo\\demo' not in path:             # 确保当前开发路径名在sys.path中
    path.append('G:\\Python\\demo\\demo')
print(version_info)
from BaseFunction.CommFunction import Sum as MySum   # 自己写的模块Sum,在CommFunciton.py文件中,新的使用名称为MySum

class Student:
    count=0
    @classmethod                                     # 定义类的方法
    def StatisticalMembers(cls):                     # cls代表类本身
        print("目前有{}个成员。".format(cls.count))
    @staticmethod                                    # 静态方法
    def SayHello(name):
        print("{},Student类有{}个实例.".format(name,Student.count))
    def __init__(self,Name,Age,Hobby,Grade):         # 构造函数:self代表类本身
        Student.count=Student.count+1                # 统计有多少个实例化对象
        self.__Name=Name
        self.__Age=Age
        self.__Hobby=Hobby
        self.__Grade=Grade                           # __Grade是私有属性(变量名前面加__),只有在类的内部可以访问和修改。
    def SelfIntroduction(self):                # 第一个参数必须是self
        print("Hello, my name is {}, I'm {} years old,my hobby is {}, I'm in the {} grade.".format(self.__Name,self.__Age,self.__Hobby,self.__Grade))
    @property                                        # 定义属性
    def Name(self):                                  # 获取__Name属性
        return self.__Name.upper()
    @Name.setter                                     # 设置__Name属性
    def Name(self,Name):
        self.__Name=Name
    def Age(self):
        return self.__Age
    def Hobby(self):
        return self.__Hobby
    def Grade(self):
        return self.__Grade

HaoR=Student(Name='HaoR',Age=12,Hobby='mathematics',Grade=7)
HaoR._Student__Grade=12                               # 通过内部的参数访问受保护的参数__Grade,前面加_Student
# HaoR.SelfIntroduction()
Student.StatisticalMembers()
# 继承和多态
class NewStudent(Student):
    def SelfIntroduction(self):
        print("Hello, my name is {}, today is {}.".format(self.Name,datetime.date.today()))
XinR = NewStudent('XinR', 19, 'singing', 19)
NewStudent.StatisticalMembers()
XinR.Name = 'WeiW'
# XinR.SelfIntroduction()
def SelfIntroduction(HaoR):
    HaoR.SelfIntroduction()
for item in [HaoR,XinR]:
    SelfIntroduction(item)
    print(isinstance(XinR, Student), '\n', isinstance(HaoR, NewStudent))        # 判断是否是继承
Student.SayHello("Python 3.7")

# 模块module和包
print(MySum(19,3,5,7,39))
'''
返回的结果:
sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0)
目前有1个成员。
目前有2个成员。
Hello, my name is HaoR, I'm 12 years old,my hobby is mathematics, I'm in the 12 grade.
True 
 False
Hello, my name is WEIW, today is 2021-01-10.
True 
 False
Python 3.7,Student类有2个实例.
73
'''

  CommFunction.py里面的函数:

def Sum(*args):
    Result=0
    for item in args:
        Result=Result+item
    return Result

if __name__=='__main__':               # 别人引用的时候就不出现下面的代码
    print('测试:',Sum(1,2,3))

 

你可能感兴趣的:(Python,python,编程语言)