zoukankan      html  css  js  c++  java
  • Python学习系列(八)( 面向对象基础)

     Python学习系列(八)( 面向对象基础)
    一,面向对象
    1,:属于一个对象或类的变量。有两种类型,即实例变量—属于每个实例/类的对象;类变量—属于类本身。
    2,类的方法:对象也可以使用属于类的函数来具有功能,这样的函数称之为类的方法方法合称为类的属性。类使用class关键字创建,类的属性被列在一个缩进块中。
    3,self:类的方法与普通的函数只有一个特别的区别----他们必须有一个额外的第一个参数名称,但是在调用的时候不能为其赋值,Python会提供。这个特别的变量指对象本身,按照惯例命名为self。self等价于C++中的self指针和Java,C#中的this
    创建一个类:
    1 class Person:
    2     pass
    3  
    4 p=Person()
    5 print p
    6 >>> ============= RESTART ================================
    7 >>>
    8 <__main__.Person instance at 0x02A99B98>
    View Code
    4,使用对象的方法:
    1)
    1 class Person:
    2     def sayHi(self):
    3         print 'Hello,how are you?'
    4  
    5 p=Person()
    6 p.sayHi()
    View Code
    2)使用__init__方法
    1 class Person:
    2     def __init__(self,name):
    3         self.name=name
    4        
    5     def sayHi(self):
    6         print 'Hello,my name is',self.name
    7  
    8 p=Person('John')
    9 p.sayHi()
    View Code

    3)使用类与对象的变量

     1 class Person:
     2     '''Represents a person.'''
     3     population=0
     4     def __init__(self,name):
     5         '''Initializes the person's data.'''
     6         self.name=name
     7         print '(Initializing %s)'%self.name
     8         Person.population +=1
     9        
    10     def __del__(self):
    11         '''I'm dying.'''
    12         print '%s says bye.'%self.name
    13         Person.population -=1
    14         if Person.population==0:
    15             print '''I'm the last one.'''
    16         else:
    17             print 'There are still %d people left.'%Person.population
    18            
    19     def sayHi(self):
    20         '''Greeting by the person.
    21  
    22         Really,that's all it does.'''
    23         print 'Hello,my name is%s.'%self.name
    24  
    25     def howMany(self):
    26         '''Prints the current population.'''
    27         if Person.population==1:
    28             print '''I'm the only one here.'''
    29         else:
    30             print 'We have %d person here.'%Person.population
    31  
    32    
    33 p=Person('John')
    34 p.sayHi()
    35 p.howMany()
    36  
    37 p1=Person('John1')
    38 p1.sayHi()
    39 p1.howMany()
    40  
    41 p.sayHi()
    42 p.howMany()
    View Code

    属性参考:使用self变量来参考同一个对象的变量和方法。

    二、继承
    1,继承是用来描述类之间的类型和子类型关系。
    2,多态现象:一个子类型在任何需要父类型的场合可以替换父类型,即对象可以被视作父类的实例。
    3,基本类超类)和子类导出类
    4,示例:
     1 class SChoolMember: #教师与学生的公有属性,基类
     2     '''Represents any SChoolMember.'''
     3     def __init__(self,name,age):
     4         '''Initializes the person's data.'''
     5         self.name=name
     6         self.age=age
     7         print '(Initializing SChoolMember %s)'%self.name
     8  
     9     def tell(self):
    10         '''Tell my details.'''
    11         print '''Name:%s
    Age:%s'''%(self.name,self.age)
    12  
    13 class Teacher(SChoolMember): #教师子类
    14     '''Represents any Teacher.'''
    15     def __init__(self,name,age,salary):
    16         '''Initializes the person's data.'''
    17         SChoolMember.__init__(self,name,age) #继承
    18         self.salary=salary
    19         print '(Initializing Teacher %s)'%self.name
    20  
    21     def tell(self):
    22         '''Tell my details.'''
    23         SChoolMember.tell(self) #继承
    24         print '''Salary:%d'''%self.salary
    25  
    26 class Student(SChoolMember): #学生子类
    27     '''Represents any Student.'''
    28     def __init__(self,name,age,marks):
    29         '''Initializes the person's data.'''
    30         SChoolMember.__init__(self,name,age) #继承
    31         self.marks=marks
    32         print '(Initializing Student %s)'%self.name
    33  
    34     def tell(self):
    35         '''Tell my details.'''
    36         SChoolMember.tell(self) #继承
    37         print '''Marks:%d'''%self.marks
    38  
    39  
    40 t=Teacher('Mrs.Jhon',40,4000)
    41 s=Student('zhangbc',23,90)
    42 members=[t,s]
    43 for member in members:
    44     member.tell()
    45     print ''
    View Code
    三、小结
          时隔快一年了,动笔生疏了,想想还是学习比较踏实吧。本篇幅较短,重在理解,后续逐渐完善。

  • 相关阅读:
    python可视化---plot()函数
    蜂鸣器的相关知识及简单应用
    将一个文件夹中多个视频的视频帧保存在多个文件夹下
    键盘按键控制程序的简单案例
    Tkinter模块的详细总结
    控制LED灯发光
    python socket 套接字编程 单进程服务器 实现多客户端访问
    python 报错RuntimeError: dictionary changed size during iteration
    HTTP请求行、请求头、请求体详解(转)
    python UDP套接字通信
  • 原文地址:https://www.cnblogs.com/zhangbc/p/3948084.html
Copyright © 2011-2022 走看看