码迷,mamicode.com
首页 > 编程语言 > 详细

Python语言及其运用_第六章_对象和类

时间:2017-08-03 22:44:03      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:object   push   round   python   setter   .text   魔术方法   指定   family   

【主要内容】主要总结书中关于类和对象的简单程序,用于说明类的基本使用方法

                        注意:6.类中变量的私有保护    9.魔术方法

【基本知识】

1.类的基本定义

1 class Person():
2     def __init__(self,name):    #初始化方法,注意类中方法的第一个参数为表示自身的self
3              self.name = name
4 hunter = Person("Elmer Fudd")
5 print(hunter.name)

2.继承

1 class Car():
2          def exclaim(self):
3                   print("I‘m a car!")
4 
5 class Yugo(Car):
6          pass
7 give_me_a_yugo = Yugo()
8 give_me_a_yugo.exclaim()
9 #输出:I‘m a car!

3.覆盖

 1 class Car():
 2          def exclaim(self):
 3                   print("I‘m a car!")
 4 
 5 class Yugo(Car):
 6          def exclaim(self):
 7                   print("I‘m a yugo!")
 8 give_me_a_car = Car()
 9 give_me_a_yugo = Yugo()
10 give_me_a_car.exclaim()
11 give_me_a_yugo.exclaim()
12 #输出:I‘m a car!
13 #          I‘m a yugo!

4.添加新方法

 1 class Car():
 2          def exclaim(self):
 3                   print("I‘m a car!")
 4 
 5 class Yugo(Car):
 6          def exclaim(self):
 7                   print("I‘m a yugo!")
 8          def need_a_push(self):
 9                   print("A little help here?")
10 
11 give_me_a_car = Car()
12 give_me_a_yugo = Yugo()
13 #give_me_a_car.need_a_push()
14 give_me_a_yugo.need_a_push()

 

5.使用super调用父类的函数

   下面的例子中,子类将父类的__init__()方法覆盖,需要显示调用

 1 class Person():
 2     def __init__(self,name):
 3              self.name = name
 4 
 5 class EmailPerson(Person):
 6          def __init__(self,name,email):
 7                   super().__init__(name)
 8                   self.email = email
 9 
10 bob = EmailPerson("Bob Frapples","bob@frapples,com")
11 print(bob.name)
12 print(bob.email)

6.类的私有保护性

方法一、定义get和set方法后利用property()函数设置属性

 1 class Duck():
 2          def __init__(self, input_name):
 3                   self.hidden_name = input_name
 4          def set_name(self, input_name):
 5                   print("inside the setter")
 6                   self.hidden_name = input_name
 7          def get_name(self):
 8                   print("inside the getter")
 9                   return self.hidden_name
10          name = property(get_name,set_name)
11 
12 fowl = Duck("howard")
13 fowl.name
14 fowl.get_name()
15 fowl.set_name("Dafft")

通过property函数设置set和get函数可以设置新的name属性
但是,仍然可以通过hidden_name调用

方法二、使用装饰器

@property,用于指示getter方法
@name.setter,用于指示setter方法

 1 class Duck():
 2          def __init__(self, input_name):
 3                   self.hidden_name = input_name
 4          @property
 5          def name(self):
 6                   print("inside the getter")
 7                   return self.hidden_name
 8          @name.setter
 9          def name(self, input_name):
10                   print("inside the setter")
11                   self.hidden_name = input_name
12 fowl = Duck("howard")
13 fowl.name = "Donald"
14 print(fowl.hidden_name)

注意:仍然可以通过hidden_name调用

方法三、私有命名
#变量由连续两个下划线开头(__)

 1 class Duck():
 2          def __init__(self,input_name):
 3                   self.__name = input_name
 4          @property
 5          def name(self):
 6                   return self.__name
 7          @name.setter
 8          def name(self,input_name):
 9                   self.__name = input_name
10 
11 fowl = Duck("Marry")
12 print(fowl.name)
13 fowl.name = "Jack"
14 print(fowl.name)

注意:无法访问fowl.__name,但是可以通过fowl._Duck__name访问。

7.函数的类型

(1)实例方法:以self为第一个参数的方法。调用时会将调用该方法对象作为self参数传入。(参见上述函数)

(2)类方法:作用于整个类。用前缀@classmethod指定,第一个参数为类本身(cls)

 1 class A():
 2          count = 0
 3          def __init__(self):
 4                   A.count += 1
 5          @classmethod
 6          def kids(cls):
 7                   print("A has" , cls.count, "little objects.")
 8 
 9 a1 = A()
10 a2 = A()
11 a3 = A()
12 A.kids()

(3)静态方法:@staticmethod不影响类也不影响方法

1 class CoyoteWeapon():
2          @staticmethod
3          def commercial():
4                   print("This is a static method")
5 
6 CoyoteWeapon.commercial()

【思考】2、3其实就类似于C++中的static方法

8.多态

 1 class Quote():
 2          def __init__(self,person,word):
 3                   self.person = person
 4                   self.word = word
 5          def who(self):
 6                   return self.person
 7          def says(self):
 8                   return self.word+.
 9 
10 class QuestionQuote(Quote):
11          def says(self):
12                   return self.word+?
13          
14 class ExclamationQuote(Quote):
15          def says(self):
16                   return self.word+!
17 
18 class BabblingBrook():
19          def who(self):
20                   return "777"
21          def says(self):
22                   return "888"
23 
24 def printQuote(obj):
25          print(obj.who(), says:, obj.says())
26 
27 hunter1 = Quote("Quote1","Quote1")
28 hunter2  = QuestionQuote("QuestionQuote1","QuestionQuote1")
29 hunter3 = ExclamationQuote("ExclamationQuote1","ExclamationQuote1")
30 babblingbrook = BabblingBrook()
31 printQuote(hunter1)
32 printQuote(hunter2)
33 printQuote(hunter3)
34 printQuote(babblingbrook)

注:BabblingBrook不是子类但仍可以调用

9.魔术方法:类似于C++中的operator方法

1 class Word():
2          def __init__(self,text):
3                   self.text = text
4          def __eq__(self,word2):
5                   return self.text.lower() == word2.text.lower()
6 
7 first = Word("HA")
8 second = Word("ha")
9 print(first == second)

 

Python语言及其运用_第六章_对象和类

标签:object   push   round   python   setter   .text   魔术方法   指定   family   

原文地址:http://www.cnblogs.com/daixianqiang414/p/7282218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!