python编程进阶之类和对象用法实例分析

本文实例讲述了python类和对象用法。分享给大家供大家参考,具体如下:

前面我们都是用python面向过程编程,现在来用python创建类和对象,面向对象编程。类和对象是面向对象编程的两个主要方面。类创建一个新类型,而对象这个类的 实例 。这类似于你有一个int类型的变量,这存储整数的变量是int类的实例(对象)。在python中,类和实例中的变量称为域,类和实例中的函数称为方法,域和方法都是类和实例的属性

类的定义

在定义类或者它的函数的时候,如果没有参数的话,需要把参数写为self,这样在外部调用的时候能清楚调用的是哪个实例的函数。这个self表示的是当前的实例,与C++中的self和Java中的this是一样的。

空类

#!/usr/bin/python
# Filename: simplestclass.py
class Person:
  pass # An empty block
p = Person()
print p

我们使用class语句后跟类名,创建了一个新的类。这后面跟着一个缩进的语句块形成类体。在这个例子中,我们使用了一个空白块,它由pass语句表示。这样编译的时候就会跳过。这是一个空类。

接下来,我们使用类名后跟一对圆括号来创建一个对象/实例。因为这个类不需要任何的参数,所以创建的时候也不需要参数。

$ python simplestclass.py
<__main__.Person instance at 0xf6fcb18c>

打印这个对象,我们可以看到这个对象所属的类,以及它的内存地址。

__init__函数

在上面的例子中,我们没有写任何东西,但是还是会有一个默认的构造函数。在python中也有类似构造函数的东西,就是__init__函数。在创建对象的时候会自动调用这个函数。

#!/usr/bin/python
# Filename: class_init.py
class Person:
  def __init__(self, name):
    self.name = name
  def sayHi(self):
    print 'Hello, my name is', self.name
p = Person('Swaroop')
p.sayHi()

输出:

$ python class_init.py
Hello, my name is Swaroop

当新建一个person对象时,因为__init__中需要有name参数,所以需要提供一个参数。可以看到这个类中有一个自己的域,在构造的时候把name的值赋给对象里的name。
可以看到,sayHi并不需要参数,但是在定义函数时仍然要有参数self

__del__函数

就如同init方法一样,还有一个特殊的方法del,它在对象消逝的时候被调用。相当于析构函数。对象消逝即对象不再被使用,它所占用的内存将返回给系统作它用。但是很难保证这个方法究竟在什么时候运行。如果你想要指明它的运行,你就得使用del语句。代码例子和下面的在一起。

使用类的变量

Python中所有的类成员(包括数据成员)都是公共的,所有的方法都是有效的 。

只有一个例外:如果你使用的数据成员名称以双下划线前缀比如__privatevar,Python的名称管理体系会有效地把它作为私有变量。

这样就有一个惯例,如果某个变量只想在类或对象中使用,就应该以单下划线前缀。而其他的名称都将作为公共的,可以被其他类/对象使用。

#!/usr/bin/python
# Filename: objvar.py
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 'There is nobody 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()

输出:

$ python objvar.py
(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.
Abdul Kalam says bye.
There are still 1 people left.
Swaroop says bye.
There is nobody here.

这是一个很长的例子。这里,population属于Person类,因此是一个类的变量。name变量属于对象(它使用self赋值)因此是对象的变量。

观察可以发现init方法用一个名字来初始化Person实例。在这个方法中,我们让population增加1,这是因为我们增加了一个人。同样可以发现,self.name的值根据每个对象指定,这表明了它作为对象的变量的本质。

记住,你只能使用self变量来引用同一个对象的变量和方法。这被称为 属性引用。

当一个对象被清除的时候,del被调用,人数减少1并输出谁走了。一开始Abdul Kalam被自动清除了,后来Swaroop也被自动清除了。清除的顺序应该跟对象闲置的时间有关,当闲置时间达到某个值就清除掉。

继承

python的继承和C++还有JAVA没有什么不同,就是构造函数和其他函数里记得要在参数里加上self。函数的重载也是和C++,JAVA一样的。

例子:

#!/usr/bin/python
# Filename: inherit.py
class SchoolMember:
  '''Represents any school member.'''
  def __init__(self, name, age):
    self.name = name
    self.age = age
    print '(Initialized SchoolMember: %s)' % self.name
  def tell(self):
    '''Tell my details.'''
    print 'Name:"%s" Age:"%s"' % (self.name, self.age),
class Teacher(SchoolMember):
  '''Represents a teacher.'''
  def __init__(self, name, age, salary):
    SchoolMember.__init__(self, name, age)
    self.salary = salary
    print '(Initialized Teacher: %s)' % self.name
  def tell(self):
    SchoolMember.tell(self)
    print 'Salary: "%d"' % self.salary
class Student(SchoolMember):
  '''Represents a student.'''
  def __init__(self, name, age, marks):
    SchoolMember.__init__(self, name, age)
    self.marks = marks
    print '(Initialized Student: %s)' % self.name
  def tell(self):
    SchoolMember.tell(self)
    print 'Marks: "%d"' % self.marks
t = Teacher('Mrs. Shrividya', 40, 30000)
s = Student('Swaroop', 22, 75)
print # prints a blank line
members = [t, s]
for member in members:
  member.tell() # works for both Teachers and Students

输出:

$ python inherit.py
(Initialized SchoolMember: Mrs. Shrividya)
(Initialized Teacher: Mrs. Shrividya)
(Initialized SchoolMember: Swaroop)
(Initialized Student: Swaroop)

Name:"Mrs. Shrividya" Age:"40" Salary: "30000"
Name:"Swaroop" Age:"22" Marks: "75"

schoolmenber是父类,学生和老师是子类,继承的方法为在类定义中把参数设为schoolmenber。可以看到,当我们继承一个类的时候,首先调用的是父类的构造函数,然后才是子类的。

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

若文章对您有帮助,帮忙点个赞!

0
-3
发布时间 2020-02-21 15:01:25
0 条回复(回复会通过微信通知作者)
点击加载更多评论
登录 后再进行评论
(微信扫码即可登录,无需注册)