Python类的用法介绍

第一形式
# !/usr/bin/env python
# coding=utf-8

class Person(object): #object表示继承自object类,Python3中可省略次内容
    """
    This is a sample of Class
    """
    breast = 90  #类的属性 是静态变量
   
    def __init__(self, name): #初始化方法  self为对象实例本身
        self.name = name
       
    def get_name(self):  #类的方法
        return self.name
   
    def color(self,color):
        d = {}
        d[self.name] = color;
        return d
   
if __name__ == "__main__":
    girl = Person("songjia")
    print girl.name
    girl.name ="liu"
    print girl.get_name()
    print girl.color("white")
    girl.breast = 80 #修改实例的属性
    print girl.breast
    print Person.breast #类属性不会随实例属性的改变而改变
    Person.breast = 100
    print Person.breast
    print girl.breast
第二种形式
>>> __metaclass__ = type
>>> class CC:
...     pass
...
>>> cc = CC()
>>> cc.__class__
<class '__main__.CC'>
>>> type(cc)
<class '__main__.CC'>
实例

girl = Person("songjia")
类属性

>>> class A(object):    #Python 3: class A:
...         x = 7  #类的属性
...
下面列出类的几种特殊属性的含义:

C.__name__ :以字符串的形式,返回类的名字,注意这时候得到的仅仅是一个字符串,它不是一个类对象
C.__doc__ :显示类的文档
C.__base__ :类C的所有父类。如果是按照上面方式定义的类,应该显示 object ,因为以上所有类都继承了它。等到学习了“继承”,再来看这个属性,内容就丰富了
C.__dict__ :以字典形式显示类的所有属性
C.__module__ :类所在的模块
实例属性

>>> class A(object):    #Python 3: class A:
...         x = 7  #类的属性
...
>>> foo = A()
>>> foo.x #实例属性
类中变量引用可变数据

>>> class B(object):
...     y = [1, 2, 3]
   
   >>> B.y         #类属性
[1, 2, 3]
>>> bar = B()
>>> bar.y       #实例属性
[1, 2, 3]

>>> bar.y.append(4)
>>> bar.y
[1, 2, 3, 4]
>>> B.y
[1, 2, 3, 4]

>>> B.y.append("aa")
>>> B.y
[1, 2, 3, 4, 'aa']
>>> bar.y
[1, 2, 3, 4, 'aa']
当类中变量引用的是可变对象是,类属性和实例属性都能直接修改这个对象,从而影响另一方的值。

访问限制

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

class Person(object):
    """
    This is a sample of Class
    """
   
    def __init__(self, name): #初始化方法  self为对象实例本身
        self.__name = name #__xx双下划线表示类的私有变量
       
    def get_name(self):  #类的方法
        return self.__name #类的内部可以访问
   
if __name__ == "__main__":
    girl = Person("songjia")
    print girl.get_name()
    print girl._name #无法访问类的私有变量
文档字符串

在函数、类或者文件开头的部分写文档字符串说明,一般采用三重引号。这样写的最大好处是能够用help()函数看。

"""This is python lesson"""

def start_func(arg):
   """This is a function."""
   pass

class MyClass:
   """This is my class."""
   def my_method(self,arg):
       """This is my method."""
       pass
继承

单继承

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

class Person(object):        #Python 3: class Person:
    def __init__(self, name):
        self.name = name
   
    def height(self, m):
        h = dict((["height", m],))
        return h

    def breast(self, n):
        b = dict((["breast", n],))
        return b

class Girl(Person):  #继承
    def get_name(self):
        return self.name

if __name__ == "__main__":
    cang = Girl("liuguoquan")
    print cang.get_name()        #Python 3: print(cang.get_name()),下同,从略
    print cang.height(160)
    print cang.breast(90)
调用覆盖的方法

class Girl(Person):
   def __init__(self, name):
       #Person.__init__(self, name) #调用父类的方法
       super(Girl, self).__init__(name) #调用父类的方法常用写法
       self.real_name = "Aoi sola"

   def get_name(self):
       return self.name
      
if __name__ == "__main__":
   cang = Girl("canglaoshi")
   print cang.real_name
   print cang.get_name()
   print cang.height(160)
   print cang.breast(90)

执行结果为:

    Aoi sola
    canglaoshi
    {'height': 160}
    {'breast': 90}
多继承

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

class Person(object):        #Python 3: class Person:
    def eye(self):
        print "two eyes"

    def breast(self, n):
        print "The breast is: ",n

class Girl(object):        #Python 3: class Gril:
    age = 28
    def color(self):
        print "The girl is white"

class HotGirl(Person, Girl): #多重继承
    pass

if __name__ == "__main__":
    kong = HotGirl()
    kong.eye()
    kong.breast(90)
    kong.color()
    print kong.age
   
two eyes
The breast is:  90
The girl is white
28
多重继承的顺序-广度优先

class K1(object):        #Python 3: class K1:
   def foo(self):
       print "K1-foo"    #Python 3: print("K1-foo"),下同,从略

class K2(object):        #Python 3: class K2:
   def foo(self):
       print "K2-foo"
   def bar(self):
       print "K2-bar"

class J1(K1, K2):
   pass

class J2(K1, K2):
   def bar(self):
       print "J2-bar"

class C(J1, J2):
pass
  
if __name__ == "__main__":
print C.__mro__
m = C()
m.foo()
m.bar()
  
K1-foo
J2-bar
代码中的 print C.__mro__ 是要打印出类的继承顺序。从上面清晰看出来了。如果要执行 foo() 方法,首先看 J1 ,没有,看 J2 ,还没有,看 J1 里面的 K1 ,有了,即C==>J1==>J2==>K1; bar() 也是按照这个顺序,在 J2 中就找到了一个。

这种对继承属性和方法搜索的顺序称之为“广度优先”。

Python 2的新式类,以及Python 3中都是按照此顺序原则搜寻属性和方法的。

方法

绑定方法

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

class Person(object):        #Python 3: class Person:
    def eye(self):
        print "two eyes"

    def breast(self, n):
        print "The breast is: ",n

class Girl(object):        #Python 3: class Gril:
    age = 28
    def color(self):
        print "The girl is white"

class HotGirl(Person, Girl): #多重继承
    pass

if __name__ == "__main__":
    kong = HotGirl() #实例化实现了方法和实例的绑
    kong.eye() #调用绑定方法
非绑定方法

在子类中,父类的方法就是 非绑定方法 ,因为在子类中,没有建立父类的实例,却要是用父类的方法。

静态方法和类方法

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

__metaclass__ = type

class StaticMethod: #静态方法
    @staticmethod
    def foo():
        print "This is static method foo()."

class ClassMethod: #类方法
    @classmethod
    def bar(cls): #类方法必须有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()
   
This is static method foo().
This is static method foo().
********
This is class method bar().
bar() is part of class: ClassMethod
This is class method bar().
bar() is part of class: ClassMethod
在python中:

@staticmethod 表示下面的方法是静态方法
@classmethod 表示下面的方法是类方法
多态和封装

多态

class Cat:
   def speak(self):
       print "meow!"

class Dog:
   def speak(self):
       print "woof!"

class Bob:
   def bow(self):
       print "thank you, thank you!"
   def speak(self):
       print "hello, welcome to the neighborhood!"
   def drive(self):
       print "beep, beep!"

def command(pet):
   pet.speak()

pets = [ Cat(), Dog(), Bob() ]

for pet in pets:
   command(pet)
Python中的多态特点,Python不检查传入对象的类型,这种方式被称之为“隐式类型”(laten typing)或者“结构式类型”(structural typing),也被通俗的称为“鸭子类型”(duck typeing),Python是弱类型语言。

Java会检查传入对象的类型,所以是强类型语言。

封装和私有化

要了解封装,离不开“私有化”,就是将类或者函数中的某些属性限制在某个区域之内,外部无法调用。

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

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

class ProtectMe(object):        #Python 3: class ProtectMe:
    def __init__(self):
        self.me = "qiwsir"
        self.__name = "kivi" #私有变量

    def __python(self): #私有方法
        print "I love Python."       

    def code(self):
        print "Which language do you like?"
        self.__python()

if __name__ == "__main__":
    p = ProtectMe()
    print p.me
    print p.code()
如何将一个方法变成属性调用?

可以使用 property 函数。

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

class ProtectMe(object):        #Python 3: class ProtectMe:
    def __init__(self):
        self.me = "qiwsir"
        self.__name = "kivi" #私有变量

    def __python(self): #私有方法
        print "I love Python."       

    @property
    def code(self):
        print "Which language do you like?"
        self.__python

if __name__ == "__main__":
    p = ProtectMe()
    print p.me
    print p.code #调用方法名即可

时间: 2024-08-30 06:56:39

Python类的用法介绍的相关文章

Python类的用法实例浅析

  本文实例讲述了Python类的用法.分享给大家供大家参考.具体如下: 先看一段代码: ? 1 2 3 4 5 6 7 8 9 #!/usr/bin/env python class Test: def __init__(self,msg="hello"): self.wel=msg print "init" def go(self,name,do): print self.wel+"go! "+name+" "+do d=

python类继承用法实例分析

  本文实例讲述了python类继承用法.分享给大家供大家参考.具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

python类继承用法实例分析_python

本文实例讲述了python类继承用法.分享给大家供大家参考.具体方法如下: #!/usr/bin/python # Filename: inherit.py class SchoolMember: '''Represents any school member.''' def __init__(self, name, age): self.name = name self.age = age print'(Initialized SchoolMember: %s)'% self.name def

php excel操作类phpExcel用法介绍

下面是总结的几个使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/Writer/Excel5.php'; 用于输出.xls的 创建一个excel $objPHPExcel = new PHPExcel(); 保存excel-2007格式 $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); //或者$o

Ruby 组织对象用的类的用法介绍

类与实例 2016年9月5日 下午7:35 *** 一个类里面会定义一些方法,类存在的理由就是要被实例化,也就是去创建一个类的实例得到一个对象.一个实例化的动作,像这样: obj = Object.new Object 是 Ruby 内置的一个类,在类上使用点形式,就是 Object 与 new 之间的那个点.你就是发送了一个信息给类.类会对这个信息做出响应,就像对象可以响应信息一样.类也是对象.new 方法是一个构造器,也就是类里面的可以加工与返回新实例的方法. 使用 class 关键词可以去

python中函数用法介绍

 代码如下 复制代码 def sumOf(a, b):     return a + b 1. 函数形参 函数中的参数名称为'形参',调用函数时传递的值为'实参' 2. 局部变量 在函数内定义的变量与函数外具有相同名称的其他变量没有任何关系,即变量名称对于函数来说是局部的.这称为变量的作用域. global语句, 为定义在函数外的变量赋值时使用global语句.  代码如下 复制代码 def func():     global x     print "x is ", x     x

Python中的with...as用法介绍

  这篇文章主要介绍了Python中的with...as用法介绍,本文直接给出用法实例,需要的朋友可以参考下 这个语法是用来代替传统的try...finally语法的. 代码如下: with EXPRESSION [ as VARIABLE] WITH-BLOCK 基本思想是with所求值的对象必须有一个__enter__()方法,一个__exit__()方法. 紧跟with后面的语句被求值后,返回对象的__enter__()方法被调用,这个方法的返回值将被赋值给as后面的变量.当with后面的

python类继承与子类实例初始化用法分析_python

本文实例讲述了python类继承与子类实例初始化用法.分享给大家供大家参考.具体分析如下: [ 先贴参考书籍原文(中文英文对照)] __init__方法介绍:If a base class has an __init__() method the derived class's __init__() method must explicitly call it to ensure proper initialization of the base class part of the instan

python类装饰器用法实例

  本文实例讲述了python类装饰器用法.分享给大家供大家参考.具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 #!coding=utf-8 registry = {} def register(cls): registry[cls.__clsid__] = cls return cls @register class Foo(object): __clsid__ = '123-456' def bar(self): pass print registry 运行结果如下: {'