面向对象编程基础-Python类(二)

2019-07-11  本文已影响0人  Dozing

1

对象既包含数据(变量,更习惯称之为特性,attribute),也包含代码(函数,也称为方法)。它是某一类具体事物的特殊实例

2

>>>class Person():
           def __init__(self):
           pass

__init__self就是实际Python类的定义形式.__init__()是Python中一个特殊的函数名,用于根据类的定义创建实例对象。self参数指向了这个正在被创建的对象本身

3

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

现在,用Person类创建一个对象,为name特性传递一个字符串参数:

hunter = Person('Elmer Fudd')

上面这短短的一行代码实际做了以下工作:

这个新对象与任何其他的对象一样,你可以把它当作列表,元组,字典或集合中的元素。也可以把它当作参数传递给函数,或者把它做为函数的返回结果。

刚刚传入的name参数此时又在哪儿呢?它作为对象的特性存储在了对象里,可以直接对它进行读写操作:

>>>print('The mighty hunter:',hunter.name)
The mighty hunter:Elmer Fudd

4

>>>class Car():
             def exclaim(self):
                   print('I'm a car!')
>>>car = Car()
>>>car.exclaim()
I'm a car!

上面的语句,Python在背后做了一下两件事情:

>>>Car.exclaim(car)
I'm a Car!

当然,我们没有理由使用这种臃肿的语法

5

在类的定义中,以self作为第一个参数的方法都是实例方法(instance method)

6 (Python编程入门到实践)例子

class Dog():
         def __init__(self,name,age):
         """初始化属性name和age"""
                   self.name = name
                   self.age = age

        def sit(self):
            print(self.name.title() + "it now sitting.")

        def roll_over(self):
            print(self.name.title() + "rolled over!")        

7

Python 使用self参数来找到正确的对象所包含的特性和方法

>>> class Person():
...     def __init__(self,name):
...             self.name = name
...     def age():
...             print('Age = 15')
...
>>> A = Person('Tom')
>>> A.name
'Tom'
>>> A.age()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: age() takes 0 positional arguments but 1 was given
>>> Person.name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Person' has no attribute 'name'
>>> Person.age()
Age = 15
>>> class Person():
...     Age = 15
...     def name(self):
...             self.Name = 'Tom'
...
>>> A = Person()
>>> A.Age
15
>>> A.name()
>>> A.Name
'Tom'
>>> Person.Age
15
>>> Person.name()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: name() missing 1 required positional argument: 'self'
>>>

实例对象可以访问类的属性(特性),但不能访问类的方法

>>> class Dog():
...     def __init__(self):
...             self.name = 'Dan'
...             self.age = 8
...
>>> A = Dog()
>>> A.name
'Dan'
>>> A.age
8
>>> class woft():
...     self.name = 'tom'
...     self.age = 15
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in woft
NameError: name 'self' is not defined
>>>

实例的初始化必须用到__init__方法

>>> class Dog():
...     color = 'Black'
...     def __init__(self):
...             self.name = 'Dan'
...             self.age = 15
...
>>> A = Dog()
>>> A.Size='Big'
>>> Dog.Sound='wow'
>>> A.name
'Dan'
>>> A.age
15
>>> A.Size
'Big'
>>> A.Sound
'wow'
>>> A.color
'Black'
>>> Dog.color
'Black'
>>> Dog.name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Dog' has no attribute 'name'
>>> Dog.Sound
'wow'
>>> Dog.age
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Dog' has no attribute 'age'
>>> Dog.Size
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Dog' has no attribute 'Size'
>>>
上一篇 下一篇

猜你喜欢

热点阅读