Python对象和类

Python 里的所有数据都是以对象形式存在的,对象是类的实例。

定义类(class)

使用class来定义一个类。

比如,定义一个cat类,如下:

class Cat():
  def __init__(self):   self.name = name

创建两个cat类的实例cat1,cat2,如下:

cat1 = Cat('mimi')
cat2 = Cat('momo')

类中的__init__为初始化函数,在实例创建的时候执行。它的第一个参数必须是self.

 

继承

一个类可以继承另一个类,被继承的类可以称为'父类'或者'基类'。使用继承得到的新类会自动获得旧类中的所有方法,而不需要进行任何复制。在新类中,可以定义新的方法,也可以对继承的方法修改,修改后会覆盖原有的方法。

在python中,所有的类都继承了object类。

下面是一个简单的类继承的例子。

class Cat():
  def __init__(self):
      self.name = name
  def play():
    print("I like play")
class BossCat(Cat): def play(ball):
    print("I like play %s", %ball)

BossCat的play方法覆盖了Cat类的play方法,所以,这两个类各自的对象执行play时会有不一样的表现。

 

使用super方法

子类中的__init__()方法(如果定义)会覆盖父类中__init__()。如果子类的__init__()方法要继承父类的__init__()方法的参数就可以使用super方法,如下:

>>> class Person():
    def __init__(self,name):
        self.name = name

        
>>> class EmailPerson(Person):
    def __init__(self,name,email):
        super().__init__(name)
        self.email = email

 

属性的访问和设置

python中所有特性都是公开的,如果想要在类中实现一些私有属性的话,可以:

1 getter 方法和setter 方法

比如:

>>> class Person(object):
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def get_name(self):
        return self.name

    def set_name(self,name):
        self.name = name

        
>>> perter= Person('peter',28)
>>> perter.name
'peter'

>>> perter.name = 'perter'
>>> perter.name
'perter'
>>> 

 

2 使用@property获取属性, @xxx.setter来设置属性值。

比如:

class Student(object):

    @property
    def birth(self):
        return self._birth

    @birth.setter
    def birth(self, value):
        self._birth = value

    @property
    def age(self):
        return 2014 - self._birth

>>> S1 = Student()
>>> S1.birth = 1989
>>> S1.birth
1989
>>> S1.age
25
>>> S1.birth = 1990
>>> S1.birth
1990
>>> S1.age
24
>>> S1.age = 26
Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    S1.age = 26
AttributeError: can't set attribute

3 使用'__'来定义内部私有/隐藏的属性。

如下:直接访问__定义的属性时会报错。

>>> class Person(object):
    def __init__(self,input_name,age):
        self.__name = input_name
        self.__age = age

    @property
    def name(self):
        return self.__name
    @name.setter
    def name(self,input_name):
        self.__name = input_name

        
>>> Peter = Person('Peter',29)
>>> Peter.name
'Peter'

>>> Peter.__name
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    Peter.__name
AttributeError: 'Person' object has no attribute '__name'

 

 

实例方法/类方法/静态方法

实例方法(instance method): 以self作为第一个参数的方法。当调用方法时,python会将调用该方法的对象作为self参数传入。

类方法(class method):使用@classmethod修饰的方法。注意,类方法的第一个参数是类本身cls。

>>> class TeamA():
    num = 0
    def __init__(self):
        TeamA.num += 1
        
    @classmethod
    def counter(cls):
        print("instance number is: %s" %cls.num)

        
>>> a1=TeamA()
>>> TeamA.counter()
instance number is: 1
>>> a2 = TeamA()
>>> TeamA.counter()
instance number is: 2
>>> 

静态方法(static method): 用@staticmethod 修饰。

 

posted @ 2016-11-01 00:18  LCdre  阅读(311)  评论(0编辑  收藏  举报