【Python】面向对象的编程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lc250123/article/details/85228582

Table of Contents

面向对象编程


面向对象编程


  • 简介

类和对象是面向对象编程的两个主要方面。类 创建一个新类型,而 对象 是这个类的 实例 。

类的对象可以使用属于类的变量和函数,类的变量和函数有两种:属于每个实例/类的对象或属于类本身。就像 C++ 的类的静态变量和函数一样。

类使用class关键字创建。类的域和方法被列在一个缩进块中。不同于 C++,如果你有一个不需要参数的方法,你还是得给这个方法定义一个self参数。

  • self

Python 有一个变量指对象本身,按照惯例它的名称是self。和 C++ 的 this 指针一样。

  • 对象的方法

定义使用如下:

class Person:
    def sayHi(self):
        print 'Hello, how are you?'

p = Person()
p.sayHi()
  • __init__方法

__init__方法在类的一个对象被建立时,马上运行。这个方法可以用来对你的对象做一些你希望的 初始化 。类似于 C++ 中的构造函数。

class Person:
    def __init__(self, name):
        self.name = name
  • 类与对象的变量
class Person:
    '''Represents a person.'''
    population = 0
    __privateVar = 0
    _protectedVar = 0

    def __init__(self, name):
        '''Initializes the person's data.'''
        self.name = name
        Person.population += 1

    def __del__(self):
        '''I am dying.'''
        Person.population -= 1


p = Person("Jack")
Person.population = 100
p._protectedVar = 33

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

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

(1)_xx 以单下划线开头的表示的是 protected 类型的变量。即保护类型只能允许其本身与子类进行访问。若内部变量标示,如: 当使用 from M import 时,不会将以一个下划线开头的对象引入 。protected 类型的变量和方法 在类的实例中可以获取和调用。

(2)__xx 双下划线的表示的是私有类型的变量。只能允许这个类本身进行访问了,连子类也不可以用于命名一个类属性。

同样,注意 __del__ 方法与 C++ 中的 析构函数 的概念类似。

  • 继承

面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过 继承 机制。一个子类型在任何需要父类型的场合可以被替换成父类型,即对象可以被视作是父类的实例,这种现象被称为多态现象。这点类似于 C++。

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

输出:

(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"

如果在继承元组中列了一个以上的类,那么它就被称作 多重继承 。

class A( B, C ):
    ...

 

 

 

 

猜你喜欢

转载自blog.csdn.net/lc250123/article/details/85228582