Python 面向对象编程基础

Python 面向对象编程基础

虽然Pthon是解释性语言,但是Pthon可以进行面向对象开发,小到 脚本程序,大到3D游戏,Python都可以做到。

一类:

 语法: class 类名: 类属性,方法等;

如下我们创建一个 Person类

>>> class Person:   #创建一个Person类
    name = 'tom'    #name是类属性,可以通过 Person.name访问,也可以通过对象.name访问
    age = 18     #定义一个变量,年龄为 18

    
>>> p1 = Person();
>>> print(p1.name,Person.age)
tom 18
>>> 

二:属性,

 Python的属性可以分为 类属性,和对象属性;其中类属性是类和对象默认都可以访问的,但是对象属性只能对象访问;如代码

>>> class Student:
    #创建学生类
    name = 'lili'
    sex = ''

>>> stu1 = Student();
>>> stu1.address = 'JiNan'
>>> print(stu1.name,stu1.address)
lili JiNan
>>> 

  如上述代码:stu1对象,有个 address 属性;即是对象创建的属性只能通过对象访问,不能通过类访问

>>> print(Student.address)
Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    print(Student.address)
AttributeError: type object 'Student' has no attribute 'address'
>>> 

 

上面定义的属性,默认是对象可以访问,子类可以继承的,但是我们可以用 在类对象前面 加上 __ 定义私有属性成员变量

>>> class People:
    name = 'mike'    #默认可访问属性
    __age = 19     #私有属性,外部不可访问

    
>>> p1 = People();
>>> print(p1.name,People)
mike <class '__main__.People'>
>>> print(p1.name,People.name)
mike mike
>>> print(p1.age,People.name)
Traceback (most recent call last):
  File "<pyshell#30>", line 1, in <module>
    print(p1.age,People.name)
AttributeError: 'People' object has no attribute 'age'
>>> 

二:方法定义,

   语法: def 方法名 (self) 至少会有一个参数,并且第一个参数是 self,其他参数,依次排列,最后加 :

>>> class Teacher:
    name = "miss wang"
    teach_type = 'php'
    def getName(self):
        return self.name
    def setTeach_type(self,teach_type):
        self.teach_type = teach_type
        return

>>> t1 = Teacher()
>>> print(t1.name,t1.getName())
miss wang miss wang
>>> t1.setTeach_type('Python')
>>> print(t1.teach_type)
Python
>>> 

构造方法和析构方法 __init__(self,...)   __del__(self,...)

  • __init__ 构造方法,在生成对象时会默认调用;支持重载
  • __def__ 析构方法,在程序最后释放资源用;支持重载
>>> class People:
    name = ''        #声明name变量
    def __init__(self,name):#构造方法初始化name
        self.name = name
        return

    
>>> p1 = People('Jerry')
>>> print(p1.name)
Jerry
>>> 

三:对象属性覆盖类属性

     在类中声明一个 name 属性,对象中声明一个 name 属性,那么对象属性会覆盖类属性 name 如代码

>>> class Student:
    name='Buick'    #类属性

    
>>> stu1 = Student();
>>> print(stu1.name)
Buick
>>> stu1.name= "TOM"    #改变对象stu1.name 为 TOM
>>> print(stu1.name)
TOM
>>> print(Student.name)    #类对象的name不变
Buick
>>> 

四:类方法的创建

  关键字: @classmethod  并且方法第一个参数一般以 cls

class Teacher:
    name = 'miss jin'
    def teachPhp(self):
        print(self.name,"teach php")
        return
    @classmethod
    def setName(cls,name):
        cls.name = name
        return

    
>>> t1 = Teacher()
>>> t1.teachPhp()
miss jin teach php
>>> Teacher.setName("Mickale")
>>> t1.teachPhp
<bound method Teacher.teachPhp of <__main__.Teacher object at 0x02318FB0>>
>>> t1.teachPhp()
Mickale teach php
>>> t1.setName("t1 set Name")     
>>> t1.teachPhp()
t1 set Name teach php
>>> Teacher.teachPhp()   #这里类无法调用非类方法 
Traceback (most recent call last):
  File "<pyshell#100>", line 1, in <module>
    Teacher.teachPhp()
TypeError: teachPhp() missing 1 required positional argument: 'self'
>>> 

五:静态方法

  关键字: @staticmethod 不需要第一参数 self了

>>> class People:
    name = 'Jerry'
    @staticmethod
    def getName():
        return People.name

    
>>> print(People.getName())
Jerry
>>> p1 = People()
>>> print(p1.getName())
Jerry
>>> 

 

参考:http://www.cnblogs.com/dolphin0520/archive/2013/03/29/2986924.html

posted @ 2014-04-17 23:03  cocoajin  阅读(436)  评论(0编辑  收藏  举报