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

Python学习路程day7

时间:2016-03-07 01:30:11      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

多态

 1 class Animal:
 2     def __init__(self, name):    # Constructor of the class
 3         self.name = name
 4     def talk(self):              # Abstract method, defined by convention only
 5         raise NotImplementedError("Subclass must implement abstract method")
 6  
 7 class Cat(Animal):
 8     def talk(self):
 9         return Meow!
10  
11 class Dog(Animal):
12     def talk(self):
13         return Woof! Woof!
14  
15 animals = [Cat(Missy),
16            Dog(Lassie)]
17  
18 for animal in animals:
19     print animal.name + :  + animal.talk()

你要是觉得列表有点low,还可以定义成一个函数来写,比如:

 1 class Animal:
 2     def __init__(self, name):    # Constructor of the class
 3         self.name = name
 4     def talk(self):              # Abstract method, defined by convention only
 5         raise NotImplementedError("Subclass must implement abstract method")
 6 
 7 class Cat(Animal):
 8     def talk(self):
 9         return Meow!
10 
11 class Dog(Animal):
12     def talk(self):
13         return Woof! Woof!
14 
15 def animal(obj):
16     print (obj.talk())
17     
18 c= Cat("wuweizhen")
19 animal(c)

类的成员

类的成员可以分为三大类:字段、方法和属性

一、字段

简单的说就是类变量和实例变量

class Province:

    # 类变量
    country = 中国

    def __init__(self, name):

        # 实例变量
        self.name = name


# 直接访问实例变量
obj = Province(河北省)
print obj.name

# 直接访问类变量
Province.country

字段的定义和使用

应用场景: 通过类创建对象时,如果每个对象都具有相同的字段,那么就使用类变量

二、方法

方法包括:普通方法、静态方法和类方法,三种方法在内存中都归属于类,区别在于调用方式不同。

  • 普通方法:由对象调用;至少一个self参数;执行普通方法时,自动将调用该方法的对象赋值给self
  • 类方法:由调用; 至少一个cls参数;执行类方法时,自动将调用该方法的复制给cls
  • 静态方法:由调用;无默认参数;
技术分享
 1 class Foo:
 2 
 3     def __init__(self, name):
 4         self.name = name
 5 
 6     def ord_func(self):
 7         """ 定义普通方法,至少有一个self参数 """
 8 
 9         # print self.name
10         print 普通方法
11 
12     @classmethod
13     def class_func(cls):
14         """ 定义类方法,至少有一个cls参数 """
15 
16         print 类方法
17 
18     @staticmethod
19     def static_func():
20         """ 定义静态方法 ,无默认参数"""
21 
22         print 静态方法
23 
24 
25 # 调用普通方法
26 f = Foo()
27 f.ord_func()
28 
29 # 调用类方法
30 Foo.class_func()
31 
32 # 调用静态方法
33 Foo.static_func()
34 
35 方法的定义和使用
View Code
技术分享
 1 class Animal:
 2     def __init__(self,name):
 3         self.name = name
 4     hobbie = meat
 5     @classmethod     #类方法,不能访问实例变量
 6     def talk(self):
 7         print ("%s is talking..." % self.hobbie)
 8     @staticmethod    #静态方法,类变量和实例变量都不能访问
 9     def walk():
10         print("wu is walking...")
11     @property        #加了proeperty就不再是方法,会变成一个属性
12     def habit(self):
13         print("%s habit is xxoo" % self.name)
14 
15 d = Animal("wuweizhen")
16 d.talk()
17 d.walk()
18 d.habit
View Code

 

相同点:对于所有的方法而言,均属于类(非对象)中,所以,在内存中也只保存一份。

不同点:方法调用者不同、调用方法时自动传入的参数不同。

三、属性 

Python学习路程day7

标签:

原文地址:http://www.cnblogs.com/luolingfeng/p/5249186.html

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