Python面向对象的编程|以女朋友为例!
一个类有两个特点:属性、行为 举一个例子,假如这个对象是你女朋友,那么, 属性包括:名字、年龄、身高、生日等 行为包括:买包、吃饭、逛街等。 Python作为面向对象的语言,主要优点是代码的重用性,也就是DRY(Don’t Repeat Yourself)。Python中面向对象遵循3个基本选择: 1、继承(Inheritance):通过既有的类来创建新类,新类继承既有类的属性和方法 2、封装(Encapsulation):对于对象来说,隐藏本类内部的细节 3、多态(Polymorphism):能够处理不同类型的传入数据 类(class) 一个类就是一个对象的蓝图或者模型。它描述了对象的特征。对于一个类我们可以如下定义: class GirlFriend: pass 我们通过关键词class来定一个叫做GirlFriend的类。这儿的类是泛指,下面我们会介绍对象。 对象(object) 对象是类的实例化。如下,比如你找了一个新女朋友Lisa: Lisa = GirlFriend() Lisa这一个对象就是GirlFriend这个类的实例化。你不在仅仅泛泛地谈论女朋友,而是确实找到了一个叫Lisa的女孩子做女朋友。 实例: class GirlFriend: #类属性 sex = "Female" #实例化属性 def __init__(self,name,age): self.name = name self.age = age #你有一个叫Lisa的女朋友 Lisa = GirlFriend('Lisa',27) #然后你还有一个叫Anna的女朋友 Anna = GirlFriend('Anna',28) print("I am {},{} years old!".format(Lisa.name,Lisa.age)) print("I am {},{} years old!".format(Anna.name,Anna.age)) 输出结果为: I am Lisa,27 year old! I am Anna,28 year old! 方法 方法是在类里面定义的函数,通常用来定义一个对象的行为。 class GirlFriend: #实例化属性 def __init__(self,age): self.name = name self.age = age def eating(self,food): print("I am eating {}".format(food)) def buying(self,item): print("I want to buy a {}".format(item)) Lisa = GirlFriend('Lisa',27) Lisa.eating("Apple") Lisa.buying("Cucci") 上述输出结果为: I am eating Apple I want to buy a Cucci Lisa的行为包括eating和buying。 继承 通过现有的类创建新的类。这个新创建的类叫做“子类”,原有的类称为“父类”。比如年龄超过20岁的女朋友为可结婚的女朋友,我们称之为“可结婚女友”。这是我们可以通过女朋友的类来定义可结婚女友的类。 #父类 class GirlFriend: #实例化属性 def __init__(self,age): self.age = age def eating(self,item): print("I want to BUY a {}".format(item)) #子类 class MarriageGF(GirlFriend): def __init__(self,name): GirlFriend.__init__(self,20) #注意这儿是怎么调用父类的 self.name = name print("Marriage Girlfriend is ready!") def marriage(self,time): print("I am {},I going to marry on {}!".format(self.name,time)) def buying(self,item): print("I want to HAVE a {}.".format(item)) Lily = MarriageGF('Lily') Lily.age Lily.name Lily.eating('egg') Lily.buying('LV') Lily.marriage('today') 上述运行结果: >>> Lily = MarriageGF('Lily') Marriage Girlfriend is ready! >>> Lily.age 20 >>> Lily.name 'Lily' >>> Lily.eating('egg') I am eating egg >>> Lily.buying('LV') I want to HAVE a LV. >>> Lily.marriage('today') I am Lily,I going to marry on today! 可以看出,MarriageGF只是GirlFriend的一个特殊的类,只不过是将age限定在类20。我们创建了一个可结婚女友,Lily,她继承了GirlFriend的的属性和方法,比如GirlFriend中的eating方法虽然没有在MarriageGF中定义,但是Lily依旧能够调用该方法,同时,如果父类和子类的方法冲突,如buying,那么Lily的buying方法将会按照子类方法来运行,父类的buying方法会被覆盖。同样地,通过父类定义的对象不能够调用子类的方法。简而言之,对象的方法只会向上一级查询,不会向下一级查询! 封装 在Python中,我们可以通过在私有属性前面加双下划线前缀,来防止从类外部访问私有属性。比如,我们定一个女朋友,其体重只有自己知道,不允许从外部调用体重。 class GirlFriend: #实例化属性 def __init__(self,name): self.name = name self.age = 27 self.__weight = 55 def OnDiet(self): if self.__weight > 60: print("I am overweight!") else: print("I am good figure!") Lisa = GirlFriend('Lisa') Lisa.age Lisa.__weight Lisa.OnDiet() 运行结果: >>> Lisa.age 27 >>> Lisa.__weight Traceback (most recent call last): File " 可以看到,通过在双下划线前缀定一个属性(__weight)不能被外部访问,只能够在类内部访问。 多态性 面向对象的编程中,针对不同的数据形式使用同一个接口。 class GirlFriend: def sex(self): print("I am female!") class BoyFriend: def sex(self): print("I am male!") #定义共同接口 def spouse_sex(spouse): spouse.sex() Lily = GirlFriend() Tom = BoyFriend() spouse_sex(Lily) spouse_sex(Tom) 输出结果为: >>> spouse_sex(Lily) I am female! >>> spouse_sex(Tom) I am male! 上面我们定一个两个类,一个是女朋友、一个是男朋友,他们都有共同的方法sex(),但是这两种方法在不同的类中的作用不同。为了实现多态,我们重新定一个了一个新接口spouse_sex(),通过这种方法,我们整合了两个类中的sex()函数。 总结 进群:548377875? 即可获取数十套PDF哦! 面向对象编程的三大特点:继承、封装和多态。面向对象的最大优势是:代码变得简单高效;代码可以重用;数据更加安全。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |