客观世界有若干类,类之间有一定的结构关系 使用类名创建一个对象时,会自动执行以下操作: 可在定义的过程中将对象初始化 每当创建一个对象时,该函数会自动执行,并且self必须为第一个参数 练习: 方法重写 子类调用父类__init__()方法 多继承
面向对象
类
类的结构
类的定义
class 类名 [可选参数]: 定义初始化内容 定义初始化方法
class创建类的关键字对象
initinit专门为对象添加属性的方法,初始化属性的方法del()方法进行销毁del()方法str()方法输出特定的信息
str()方法必须返回一个字符串__xxx__()这样的函数为内置函数面向对象的特征
消息与方法
对象的三个内置方法
__init(self)__>>> class Ball: def __init__(self,name): #name为形参 self.name=name #在类的内部添加属性 def kick(self): print("I'm %s. Nice to meet you!"%self.name) >>> a=Ball('Bad') >>> a.kick() I'm Bad. Nice to meet you! >>> b=Ball() Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> b=Ball() TypeError: __init__() missing 1 required positional argument: 'name' >>> self
>>> class Ball: def setName(self,name): self.name=name def kick(self): print("I'm %s. Nice to meet you!"%self.name) >>> a=Ball() >>> a.setName("Bad") >>> b=Ball() >>> b.setName("Jay") >>> a.kick() I'm Bad. Nice to meet you! >>> b.kick() I'm Jay. Nice to meet you!
>>> class Person: def __init__(self): print("I AM BAD BOY!") >>> class Teacher(Person): #继承Person类 pass >>> class Student(Person): #继承Person类 pass >>> jack=Teacher() #调用父类方法 I AM BAD BOY! >>> jerry=Student() I AM BAD BOY! >>>
定义好类以后,定义一个对象进行测试>>> class Student: def __init__(self,name,age,scores): self.name=name self.age=age self.scores=scores def get_name(self): return self.name def get_age(self): return self.age def get_scores(self): return max(self.scores) #获得最大值 >>> stu1=Student("Bad",21,[85,86,87]) >>> print("Name:%s"%(stu1.get_name())) Name:Bad >>> print("Age:%s"%(stu1.get_age())) Age:21 >>> print("The highest score in the three courses is %s"%(stu1.get_scores())) The highest score in the three courses is 87 总结
面向对象的三大特征
继承
>>> class Mylist(list): #定义类 pass >>> list=Mylist() >>> list.append(3) >>> list.append(4) >>> list.append(5) >>> list [3, 4, 5] class ClassName(baseclasslist): "类的帮助信息" statement
ClassName用于指定类名Baseclasslist基类、父类名,类之间用逗号隔开。不指定则使用Python对象的根类objectstatement类的主体,由变量、方法和属性定义语句组成>>> class Parent: #定义父类 def hello(self): print("I AM Bad!") >>> class child(Parent): #定义子类 pass >>> a=Parent() #访问父类 >>> a.hello() I AM Bad! >>> b=child() #调用父类方法 >>> b.hello() I AM Bad!
super())>>> class Fruit: color="red" def harvest(self,color): print("The fruit is "+color) print("The fruit is already harvested") print("The fruit is "+Fruit.color+"!") >>> class Orange(Fruit): color="orange" def __init__(self): print("nI AM Orange") def harvest(self,color): print("The Orange is"+color) print("The Orange is already harvested") print("The Orange is"+Fruit.color+"!")
>>> class Fruit: def __init__(self,color="green"): Fruit.color=color def harvest(self): print("The fruit is "+Fruit.color+"!") >>> class Apple(Fruit): def __init__(self): print("I AM Apple!") >>> apple=Apple() I AM Apple! >>> apple.harvest() Traceback (most recent call last): File "<pyshell#93>", line 1, in <module> apple.harvest() File "<pyshell#87>", line 5, in harvest print("The fruit is "+Fruit.color+"!") AttributeError: type object 'Fruit' has no attribute 'color' >>>
>>> class Fruit: def __init__(self,color="green"): Fruit.color=color def harvest(self): print("The fruit is "+Fruit.color+"!") >>> class Apple(Fruit): def __init__(self): print("I AM Apple!") super().__init__() #使用super()调用父类__init__()方法 >>> apple=Apple() I AM Apple! >>> apple.harvest()
__mro__方法搜索顺序print(类名.__mro__)>>> class A: def sample(self): print("I AM BAD!") def demo(self): print("Hello World!") >>> class B: def sample(self): print("I AM BAD BOY!") def demo(self): print("You Are Han Han!") >>> class C(A,B): pass >>> c=C() >>> c.sample() I AM BAD! >>> c.demo() Hello World! # 查看方法的查找顺序 >>> print(C.__mro__) #方法搜索顺序,查看mro (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>) >>> 多态
>>> class A: def fun(self): print("I AM BAD!") >>> class B: def fun(self): print("I AM BOY!") >>> a=A() >>> b=B() >>> a.fun() I AM BAD! >>> b.fun() I AM BOY! 封装
__name__(前后双下划线)定义特殊方法,一般是Python定义的名字_name(单下划线)表示protected(保护)类型,只允许类本身与子类进行访问__name(双下划线)private(私有)类型,只允许定义该方法的类本身进行访问
>>> class Person: #定义Person类 name="Bad" >>> p=Person() #定义对象 >>> p.name 'Bad' >>> class Person(): #定义私有类 __name="Bad" >>> p=Person() #定义对象 >>> p.__name #访问 Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> p.__name AttributeError: 'Person' object has no attribute '__name' >>> p.name #访问 Traceback (most recent call last): File "<pyshell#24>", line 1, in <module> p.name AttributeError: 'Person' object has no attribute 'name' >>> >>> class Person: #定义私有类 __name="Bad" def getname(self): return self.__name >>> p=Person() >>> p.getname() 'Bad' >>> p._Person__name 'Bad'
以上内容均属原创,如有不详或错误,敬请指出。
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算
官方软件产品操作指南 (170)