ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Python基础8 - 面向对象程序设计:(2)类

Python基础8 - 面向对象程序设计:(2)类 目录一. 定义类二. 创建类的实例三. 创建__init__( )方法四. 创建类的成员并访问一创建方法并访问行为、函数二创建变量并访问属性1. 类属性2. 实例属性五. 访问限制一函数访问二外部访问一. 定义类class 类名class Human:#人类 pass二. 创建类的实例class Human: #人类 pass People Human() #创建人类的一个实例People print(People) # __main__.Human object at 0x000002414B40FCB0三. 创建__init__( )方法创建类之后通常会创建一个__init( )__方法__init( )__中必须包含一个self参数self参数是一个指向实例本身的引用用以访问类中的属性和方法。调用时会自动传递实际参数这个self可以理解为后面创建的具体实例。class Human: 人类 def __init__(self): print(人类的任意对象) People Human() #创建人类的实例 # 这个self就是指实例 小明除了self参数外还可以自定义其他参数。class Human: 人类类 def __init__(self, stand, hand, language): print(人类的任意对象拥有以下特征) print(stand) print(hand) print(language) stand_1 双脚行走身体站立 hand_1 双手灵巧可以制造工具改造自然 language_1 拥有复杂的语言系统 People Human(stand_1, hand_1, language_1) #创建人类的实例People四. 创建类的成员并访问一创建方法并访问行为、函数创建def 方法名称(self, 参数1, 参数2, ... ,参数n)访问类名.实例名(实参1, 实参2, ... ,实参n)class Human: 人类 def __init__(self, stand, hand, language): print(人类的任意对象拥有以下特征) print(stand) print(hand) print(language) def Introduce(self,state): print(大家好我的名字叫{}今年{}岁.format(state[0], state[1])) stand_1 双脚行走身体站立 hand_1 双手灵巧可以制造工具改造自然 language_1 拥有复杂的语言系统 state_1 (小明,19) People Human(stand_1, hand_1, language_1) #创建人类的实例 People.Introduce(state_1) #调用实例方法二创建变量并访问属性数据成员指的是类中定义的变量分为类属性、实例属性。1. 类属性定义在类中在函数外可以被类中函数共享也可以被子类、子类中的函数继承。# 第一种类属性 class Human: 人类 stand 双脚行走身体站立 # 类属性stand hand 双手灵巧可以制造工具改造自然 # 类属性hand language 拥有复杂的语言系统 # 类属性language def __init__(self): print(人类的任意对象拥有以下特征) print(self.stand) # 将类属性stand传给实例属性 print(self.hand) # 将类属性hand传给实例属性 print(self.language) # 将类属性language传给实例属性 def Introduce(self,state): print(大家好我的名字叫{}今年{}岁.format(state[0], state[1])) People Human() #创建人类的实例 print(外部访问类属性实例名.类属性x,People.stand) print(外部访问类属性实例名.类属性x,People.hand) print(外部访问类属性实例名.类属性x,People.language) print(外部访问类属性类名.类属性x,Human.stand) print(外部访问类属性类名.类属性x,Human.hand) print(外部访问类属性类名.类属性x,Human.language)# 第二种类属性 class Human: 人类 def __init__(self,stand 双脚行走身体站立,hand 双手灵巧可以制造工具改造自然,language 拥有复杂的语言系统): Human.stand stand # 属性在类名之下是类属性stand Human.hand hand # 属性在类名之下是类属性hand Human.language language # 属性在类名之下是类属性language def Introduce(self,state): print(大家好我的名字叫{}今年{}岁.format(state[0], state[1])) People Human() #创建人类的实例 print(外部访问类属性实例名.类属性x,People.stand) print(外部访问类属性实例名.类属性x,People.hand) print(外部访问类属性实例名.类属性x,People.language) print(外部访问类属性类名.类属性x,Human.stand) print(外部访问类属性类名.类属性x,Human.hand) print(外部访问类属性类名.类属性x,Human.language)2. 实例属性对任意实例self的属性。class Human: 人类 def __init__(self): self.stand 双脚行走身体站立 # 属性在实例self之下实例属性beak self.hand 双手灵巧可以制造工具改造自然 # 属性在实例self之下实例属性hand self.language 拥有复杂的语言系统 # 属性在实例self之下实例属性language # print(人的任意对象拥有以下特征) # print(self.stand) # print(self.hand) # print(self.language) def Introduce(self,state): print(大家好我的名字叫{}今年{}岁.format(state[0], state[1])) People Human() #创建人类的实例 print(外部访问实例属性实例名.类属性x,People.stand) print(外部访问实例属性实例名.类属性x,People.hand) print(外部访问实例属性实例名.类属性x,People.language) try: print(外部访问实例属性类名.类属性x,Human.language) except AttributeError: print(外部无法通过类名.类属性x访问实例属性type object Human has no attribute language)实例属性可以通过实例名称被修改。class Human: 人类 def __init__(self): self.stand 双脚行走身体站立 # 类属性stand def Introduce(self,state): print(大家好我的名字叫{}今年{}岁.format(state[0], state[1])) People_1 Human() #创建人类实例People1 People_2 Human() #创建人类实例People2 People_2.stand 不同国家的生存环境不同#实例2的实例属性stand print(People_1.stand) print(People_2.stand)五. 访问限制为了保证类的属性或方法不会被外部修改可以在属性或方法前面添加双下划线__名或首尾都添加双下划线从而限制访问权限。一函数访问类名.__属性名class Human1: 人类 __stand 双脚行走身体站立 # 私有类属性stand hand 双手灵巧可以制造工具改造自然 # 类属性hand def __init__(self): print(Human1.__stand) # 实例方法中访问私有类属性 def Introduce(self,state): print(大家好我的名字叫{}今年{}岁.format(state[0], state[1])) People1 Human1() #创建人类的实例 print(People1._Human1__stand) # 外部访问私有类属性stand实例名._类名__类属性 try: print(People1.__stand) # 外部无法访问私有类属性stand except AttributeError: print(限制访问Human1 object has no attribute __stand) try: print(People1.stand) # 外部无法访问私有类属性stand except AttributeError: print(限制访问Human1 object has no attribute stand) # 双脚行走身体站立 # 双脚行走身体站立 # 限制访问Human1 object has no attribute __stand # 限制访问Human1 object has no attribute stand二外部访问实例名.__类名__属性名class Human2: 人类 def __init__(self): self.__stand 双脚行走身体站立 # 实例属性beak self.__hand 双手灵巧可以制造工具改造自然 # 实例属性hand self.language 拥有复杂的语言系统 # 实例属性language def Introduce(self,state): print(大家好我的名字叫{}今年{}岁.format(state[0], state[1])) People2 Human2() #创建人类的实例 print(人类的任意对象拥有以下特征) print(People2._Human2__stand) # 外部访问私有实例属性stand实例名._类名__类属性 print(People2._Human2__hand) # 外部访问私有实例属性hand实例名._类名__类属性 print(People2.language) # 外部访问实例属性language实例名.类属性 try: print(People2.__stand) # 外部无法访问私有实例属性stand except AttributeError: print(限制访问Human2 object has no attribute __stand) try: print(People2.stand) # 外部无法访问私有实例属性stand except AttributeError: print(限制访问Human2 object has no attribute stand) # 人类的任意对象拥有以下特征 # 双脚行走身体站立 # 双手灵巧可以制造工具改造自然 # 拥有复杂的语言系统 # 限制访问Human2 object has no attribute __stand # 限制访问Human2 object has no attribute stand
返回列表