Loading

Python学习笔记12—类

典型的类和调用方法:

#!/usr/bin/env Python
# coding=utf-8
__metaclass__ = type      #新式类
class Person:             #创建类
    def __init__(self, name): #初始化函数
        self.name = name
def getName(self): #类中的方法(函数) return self.name
def color(self, color): print "%s is %s" % (self.name, color)
girl
= Person('wangguniang') #实例化 name = girl.getName() #调用方法(函数) print "the person's name is: ", name girl.color("white") #调用方法(函数) print "------" print girl.name #实例的属性

运行结果:

 

self 的属性数据,也不一定非得是由参数传入的,也可以在构造函数中自己设定

#/bin/env/python
#coding:utf-8
__metaclass__ = type
class Person:
    def __init__(self, name,general="male"):
        self.name = name
        self.email = "zy5724@163.com"
        self.general = general


info = Person("keven")
print "info.name=",info.name
print "info.email=",info.email
print "info.general=",info.general

运行结果:

 

继承

#!/usr/bin/env python
# coding=utf-8
__metaclass__ = type
class Person:
    def speak(self):
        print "I love you."
    def setHeight(self, n):
        self.length = n
    def breast(self, n):
        print "My breast is: ",n

class Girl(Person):
    def setHeight(self):
        print "The height is:1.70m ."        #方法重写

if __name__ == "__main__":
    cang = Girl()
    cang.setHeight()
    cang.speak()
    cang.breast(90)

运行结果:

多重继承:

#/usr/bin/env python
__metaclass__ = type

class Person:
    def speak(self):
        print "Hello,world!!!"
    def name(self,n):
        print "My name is: ",n

class Man:
    def role(self,r):
        print "I'm a ",r

class Boy(Person,Man):
    def food(self):
        print "I like drink milk."

if __name__ == "__main__":
    keven = Boy()
    keven.speak()
    keven.name("keven")
    keven.role("student")
    keven.food()

运行结果:

继承的特点,即将父类的方法和属性全部承接到子类中;如果子类重写了父类的方法,就使用子类的该方法,父类的被遮盖。

 

super 函数

super 函数的参数,第一个是当前子类的类名字,第二个是 self,然后是点号,点号后面是所要调用的父类的方法。

#/usr/bin/env python
__metaclass__ = type

class Person:
    def speak(self):
        print "Hello,world!!!"
    def name(self,n):
        print "My name is: ",n

class Man:
    def role(self,r):
        print "I'm a ",r

class Boy(Person,Man):
    def food(self):
        print "I like drink milk."
    def speak(self):
        print "I'm a boy."
        super(Boy,self).speak()     

if __name__ == "__main__":
    keven = Boy()
    keven.speak()
    keven.name("keven")
    keven.role("student")
    keven.food()

其中“super(Boy,self).speak() ”也可以通过”Person.speak(self)”调用speak方法。

执行结果:

 

静态方法和类方法

  • @staticmethod 表示下面的方法是静态方法
  • @classmethod 表示下面的方法是类方法
  • 静态方法中的参数没有self,
  • 类方法的参数也没有 self,但是必须有 cls 这个参数
# coding=utf-8
__metaclass__ = type
class StaticMethod:
    @staticmethod
    def foo():
        print "This is static method foo()."

class ClassMethod:
    @classmethod
    def bar(cls):
        print "This is class method bar()."
        print "bar() is part of class:",cls.__name__

if __name__ == "__main__":
    static_foo = StaticMethod()  #实例化
    static_foo.foo()             #实例调用静态方法
    StaticMethod.foo()           #通过类来调用静态方法
    print "****************"
    class_bar = ClassMethod()    #实例化
    class_bar.bar()
    ClassMethod.bar()

执行结果:

 

封装和私有化

python 中私有化的方法也比较简单,就是在准备私有化的属性(包括方法、数据)名字前面加双下划线。

用了@property 之后就可以调用被封装的私有属性了。

#!/usr/bin/env python
#coding=utf-8
__metaclass__ = type

class ProtectMe:
    def __init__(self):
        self.me = "Keven"
        self.__name = "Rock"     #封装

    @property                    #调用私有属性
    def name(self):
        return self.__name

if __name__ == "__main__":
    p = ProtectMe()
    print p.name

 

posted @ 2016-09-12 18:06  头痛不头痛  阅读(224)  评论(0编辑  收藏  举报