python的面向对象基础

来源:互联网 发布:淘宝商家不见了怎么办 编辑:程序博客网 时间:2024/05/07 09:32

python的面向对象基础

1.类和对象基础

类具有抽象性、封装性和多态性。
  • 类的抽象性:类是对具有共同方法和属性的一类对象的描述。
  • 类的封装性:类将属性和方法封装,外部都是不可见的,只有通过类提供的接口才能与属于类的实例对象进行信息交换。
  • 类的继承性:类可以从已有的类派生。派生出的类具有父类的方法和属性。
  • 类的多态性:类可以根据不同的参数类型调用不同的方法。同一个方法可以处理不同类型的参数。实际上,python的内部已实现了多态,在python中,使用类不需要考虑太多不同类型之间数据的处理问题。


2.python中类使用

2.1属性和方法

类的属性=类内部的变量
类的方法=类内部定义的函数
类内部的属性若以双下划线开始,则其为类的私有属性,不能在类的外部被使用或者访问。
在类内部使用def可以为类定义一个方法,与函数定义不同的是,类的方法必须包含参数self,且self必须为第一参数。
同理,类内部的方法若以双下划线开始,则其为类的私有方法。
class Book:          #定义Book类    __author = ''    #定义Book类的私有属性    __name = ''    __page = ''    __press = ''    price = 0        #定义Book类的公有属性    def show(self):                       print(self.__author)        print(self.__name)    def set(self, author, name):  #定义公有方法(设置其私有属性__name和__author)        self.__name = name        self.__author = author    def __check(self, item):      #定义私有方法        if item == '':            return 0        else:            return 1b = Book()                       #生成类的实例bb.set('Tom', 'Py Class Demo')    #调用公有方法set,向其传递参数b.show()                         #调用公有方法show,输出a = Book()                       #生成类的实例aa.__check()                      #调用私有方法__check(),将会报错
输出:
Tom
Py Class Demo
    a.__check()
AttributeError: 'Book' object has no attribute '__check'

2.2类的专有方法

方法名描述__init__构造函数__del__析构函数__add__加运算__mul__乘运算__cmp__比较运算__len__获得长度__repr__打印/转换__call__函数调用__setitem__按索引赋值__getitem__按索引取值

python的专有方法是对类进行操作的特殊方法。例如在类实例化时,将调用__init__方法。
class Book:    __author = ''    __name = ''    __page = ''    __press = ''    price = 0    def __init__(self, author, name): #调用__init__方法,在生成实例时,为__author和__name赋初值        self.__name = name        self.__author = author    def show(self):        print(self.__author)        print(self.__name)    def set(self, author, name):        self.__name = name        self.__author = author    def __check(self, item):        if item == '':            return 0        else:            return 1a = Book('Alice', 'Demo of Python Class')  #实例化a,并赋予初值a.show()a.set('Bob', 'Changed the Name ')          #调用set方法,重设私有属性_author和__namea.show()
输出:
Alice
Demo of Python Class
Bob
Changed the Name 

2.3类的继承

新类可以继承父类的公有属性和公有方法,但是不能继承父类的私有属性和私有方法
class Book:    __author = ''    __name = ''    __page = ''    __press = ''    price = 0    def __init__(self, author, name):         self.__name = name        self.__author = author    def show(self):        print(self.__author)        print(self.__name)    def set(self, author, name):        self.__name = name        self.__author = author    def __check(self, item):        if item == '':            return 0        else:            return 1class student(Book):                      #创建student类,它继承父类Book    __class = ''    __grade = ''    __sname = ''    def ShowStudent(self):        self.show()cc = student('Jack', 'Name attribute From Class Book') #实例化cc,student类继承了父类Book的__init__方法cc.ShowStudent()                                       #调用student类的ShowStudent方法,输出cc.show()                                              #调用父类的show方法,输出
输出:
Jack
Name attribute From Class Book
Jack
Name attribute From Class Book

2.4在类中重载方法

通过继承而创建的类,其父类的方法不一定能满足类的需求。新类实际上只需修改部分功能,为了避免重新命名方法,可以用重载来解决。例如,新的类需要重新初始化,此时就可以通过重载__init__方法来实现。
如果重载父类的方法,但又要在类中先使用父类的该方法,则可以使用父类名+'.'+方法名的形式调用。
class human:     #定义父类human    __age = 0    __sex = ''    __height = ''    __weight = ''    __name = ''    def __init__(self, age, sex, height, weight):        self.__age = age        self.__sex = sex        self.__height = height        self.__weight = weight    def setname(self, name):        self.__name = name    def show(self):        print(self.__name)        print(self.__age)        print(self.__sex)        print(self.__height)        print(self.__weight)class student(human):    #定义student类,继承human    __classes = ''    __grade = ''    __num = ''    #重载__init__方法    def __init__(self, classes, grade, num, age, sex, height, weight):        #调用父类human的__init__方法,初始化human类的属性        human.__init__(self, age, sex, height, weight)        self.__classes = classes        self.__grade = grade        self.__num = num    #重载show方法    def show(self):        #调用父类human的show方法        human.show(self)        print(self.__classes)        print(self.__grade)        print(self.__num)a = student('3班', '7年级', 'NO.0712', 19, 'male', 180, 70)  #生成student类的实例aa.setname('David')                                #调用父类human的setname方法a.show()                                          #调用重载后的show方法,输出
输出:
David
19
male
180
70
3班
7年级
NO.0712

参考文献
杨佩鹿等著.python宝典[M]. 电子工业出版社, 2014


0 0