人生苦短之Python类的一二三

       在Python中,类也是以class开头定义的。我们定义一个动物类,它有名字和年龄,在java变量有实例变量和局部变量,方法内的变量是局部变量,类里面的变量是实例变量。那么在Python中的类及其属性这些是如何定义使用的呢?

class Animal():
	pass

类的属性    

       我们知道类都是有属性的,在java中一般是在类中就定义了属性,而在python中是可以直接就给实例添加属性的。

>>> a = Animal()
>>> a.name = 'dog'
>>> print a.name
dog
>>> a
<__main__.Animal instance at 0x0000000002C5E708>
>>>

      但是这个属性仅作用于这个实例变量,当我们再次新建一个实例的时候,就没有这个属性了。

>>> b = Animal()
>>> print b.name

Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    print b.name
AttributeError: Animal instance has no attribute 'name'
>>> 

        那么如何创建一个类属性呢?可以在内部直接定义一个变量。

>>> class Student():
	name = 'Student'
	pass

>>> a = Student()
>>> print a.name
Student
>>> 

        实例化对象后可更改这个属性值。

>>> a.name = 'teacher'
>>> print a.name
teacher
>>> 

 

类的私有属性

        在java中我们知道可以通过private来控制访问限制,可以修饰变量,也可以修饰方法,使之只能在类的内部使用,那么在python中是如何实现的呢?通过在变量前添加‘__’来定义私有变量。

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

		
>>> s = Student('Bob')
>>> s.name

Traceback (most recent call last):
  File "<pyshell#69>", line 1, in <module>
    s.name
AttributeError: Student instance has no attribute 'name'
>>> s.__name

Traceback (most recent call last):
  File "<pyshell#70>", line 1, in <module>
    s.__name

          既然私有变量无法直接访问到,那么我们如何使用呢?当时是setter,getter方法,在类的内部为私有变量添加setter和getter方法我们就可以访问和修改它们了。这里就不做描述了。

  限制类的属性

          在python中既然是可以随意给实例添加属性的,那如果我们想限制只能添加给定的属性呢?需要用到__slots__

>>> class  Animal():
	__slots__ = ('name','age')

	
>>> a = Animal()
>>> a.name = 'dog'
>>> a.age = 24
>>> a.name
'dog'
>>> a.age
24
>>> a.color = red

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    a.color = red
NameError: name 'red' is not defined
>>> 

  构造方法

          在java中有默认的构造方法,也可以根据我们的需要创建构造方法,实例化对象时根据需要创建符合我们需求的对象。在Python中也是有构造方法的。使用__init__,传入self及需要的变量。

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

>>> a = Animal()


Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    a = Animal()
TypeError: __init__() takes exactly 3 arguments (1 given)
>>> a = Animal('dog',23)

          值得注意的是,当我们写了类的构造方法的时候,实例化对象的时候必须使用构造方法来实例化对象,否则就会报错误。

   

类方法

          在类中不光有属性还有方法,类方法就是在类中直接定义方法。

>>> class Animal():
	def __init__(self,name,age):
		self.name = name
		self.age = age
        def printName(self):
		print(self.name)

>>> a = Animal('dog',22)
>>> a.printName
<bound method Animal.printName of <__main__.Animal instance at 0x0000000002C64488>>
>>> a.printName()
dog
>>> 

            面向对象的三大特性,封装,继承,多态,封装不用多说了,在很多地方都有体现。我们来看下python中的继承和多态。    

>>> class Animal():
    def __init__(self,name,age):
        self.name = name
        self.age = age
        def printName(self,sex):
        print(self.name,sex)

        
>>> a = Animal('dog',22)
>>> a.printName('aa')
('dog', 'aa')
>>> class Dog(Animal):
    pass

>>> b = Dog()

Traceback (most recent call last):
  File "<pyshell#54>", line 1, in <module>
    b = Dog()
TypeError: __init__() takes exactly 3 arguments (1 given)

>>> b = Dog('2哈',12)
SyntaxError: invalid syntax
>>> b = Dog('hashiqi',12)
>>> b
<__main__.Dog instance at 0x0000000002C64848>
>>>

        我们定义一个Dog类继承于Animal,当我们实例话的时候发现它的构造方法也是继承于父类,需要传入名字和年龄,但是我们想让它拥有自己的构造方法怎么办呢?重写__init__方法。

>>> class Dog(Animal):
	def __init__(self,name,color):
		self.name = name
		self.color = color

		
>>> b = Dog('2ha','red')
>>> 

        这样就体现了面向对象的继承和多态。

原文地址:https://www.cnblogs.com/dreamyu/p/7129213.html