python面向对象编程

简介

  • 面向过程:根据操作数据的函数或语句块来设计程序。
  • 面向对象:把数据和功能结合起来,用成为对象的东西包裹起来组织程序。
  • 类和对象:类创建一个新类型,对象是这个类的实例。a是int类型的变量,则存储整数的变量是int类的对象(实例)。
  • 域:属于一个对象或类的变量被称为域。
  • 类的方法:对象可以使用属于类的函数来具有功能,这样的函数被称为类的方法。
    域和方法可以合称为类的属性。
  • self:例如一个类名为MyClass和对应的一个实例MyObject。当调用这个对象的方法MyObject.method(arg1,arg2)时,会由python自动转化为MyClass.method(self,arg1,arg2)。

类的函数→类的方法→对象的方法→调用

创建类

  • class+类名
class Myclass:
    pass
o = Myclass()
print(o)

输出:<__main__.Myclass object at 0x000001B6799C2F28>。即已经在__main__模块中创建了一个Myxlass类的实例,存储对象的地址也显示出来。python在任何空位存储对象。

类/对象可以拥有像函数一样的方法,这些方法与函数的区别只是一个额外的self变量

对象

创建对象

  • (对象名 = )类名+()
class Female:
    def feature(self):
        print("beautful")

class male:
    def feature(self):
        print("strong")

Zoe = Female()
Zoe.feature() #same as Female().feature()
Tom = male()
Tom.feature()

输出:beautiful strong

方法

__init__方法在类的一个对象被创建后,马上运行。这个方法可以用来对创建的对象做有用的初始化。

class Female:
    def __init__(self,age):
        self.age = age
    def feature(self):
        print("beautful")

class male:
    def __init__(self,age):
        self.age = age
    def feature(self):
        print("strong")
#两种方法
Female(23).feature()
Tom = male(23)
Tom.feature()

输出:beautiful strong

相当于类别的初始属性

类与对象的变量

类的变量(域)共享
对象的变量(域)不共享

#原书实例
class Person:
    """Represents a person."""
    population = 0
    def __init__(self, name):
        '''Initializes the person's data.'''
        self.name = name
        print ('Initializing %s'% self.name)
        # When this person is created, he/she
        #  adds to the population
        Person.population += 1
    def __del__(self):
        '''I am dying.'''
        print ('%s says bye.'% self.name)
        Person.population -= 1
        if Person.population == 0:
            print( 'I am the last one.')
        else:
            print('There are still %d people left.'% Person.population)
    def sayHi(self):
        '''Greeting by the person.
        Really, that's all it does.'''
        print ('Hi, my name is %s.'% self.name)

    def howMany(self):
        '''Prints the current population.'''
        if Person.population == 1:
            print ('I am the only person here.')
        else:
            print ('We have %d persons here.'% Person.population)

swaroop = Person('Swaroop')
swaroop.sayHi()
swaroop.howMany()
kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()
swaroop.sayHi()
swaroop.howMany()

输出:

Initializing Swaroop
Hi, my name is Swaroop.
I am the only person here.
Initializing Abdul Kalam
Hi, my name is Abdul Kalam.
We have 2 persons here.
Hi, my name is Swaroop.
We have 2 persons here.
Swaroop says bye.
There are still 1 people left.
Abdul Kalam says bye.
I am the last one.


1.此代码中,Person.population是类的变量;self.name是对象的变量。其实很简单,看·的前面是什么就知道是类的变量还是对象的变量。
2.当对象不再被使用时,del方法运行,但是很难保证这个方法究竟在 什么时候 运行。

类的变量→全局变量 & 对象的变量→局部变量

继承

基本类/超类,导出类/子类
父类改变影响子类
子类改变不影响其他类

class people:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        print('initializing people %s' % self.name)

    def details(self):
        print('name %s,age %s'%(self.name, self.age))

class adult(people):
    def __init__(self, name, age, height):
        people.__init__(self, name, age)
        self.height = height
        print('initializing adult %s' % self.name)

    def details(self):
        people.details(self)
        print('height %d' % self.height)

class child (people):
    def __init__(self, name, age, gender):
        people.__init__(self, name, age)
        self.gender = gender
        print('initializing child %s' % self.name)

    def details(self):
        people.details(self)
        print('gender %s' % self.gender)

a = adult('tony', '23', 172)
b = child('alex', '12', 'girl')

a.details()
b.details()

输出:

initializing people tony
initializing adult tony
initializing people alex
initializing child alex
name tony,age 23
height 172
name alex,age 12
gender girl
#如何不换行输出
  1. 为了使用继承,我们把基本类的名称作为一个元组跟在定义类时的类名称之后
  2. 如果在继承元组中列了一个以上的类,那么它就被称作多重继承
  3. Python总是首先查找对应类型的方法,在这个例子中就是如此。如果它不能在导 出类中找到对应的方法,它才开始到基本类中逐个查找。基本类是在类定义的时候,在元组之 中指明的。

附录

力荐:python简明中文教程
本文是此书第十一章的总结

你可能感兴趣的:(python面向对象编程)