zoukankan      html  css  js  c++  java
  • python基础(三)——类的研究

    class Employee:         //定义类 以冒号结束
       '所有员工的基类'      //帮助信息
       empCount = 0
       def __init__(self, name, salary):    //调用时初始化,属性有name和salary
          self.name = name
          self.salary = salary
          Employee.empCount += 1
    
       def displayCount(self):
         print "Total Employee %d" % Employee.empCount      //%格式化打印格式
    
       def displayEmployee(self):
          print "Name : ", self.name,  ", Salary: ", self.salary
    
    "创建 Employee 类的第一个对象"
    emp1 = Employee("Zara", 2000)
    "创建 Employee 类的第二个对象"
    emp2 = Employee("Manni", 5000)
    emp1.displayEmployee()
    emp2.displayEmployee()
    print "Total Employee %d" % Employee.empCount
    emp1.age = 7  # 添加一个 'age' 属性
    emp1.age = 8  # 修改 'age' 属性
    del emp1.age  # 删除 'age' 属性
    hasattr(emp1, 'age')    # 如果存在 'age' 属性返回 True。
    getattr(emp1, 'age')    # 返回 'age' 属性的值
    setattr(emp1, 'age', 8) # 添加属性 'age' 值为 8
    delattr(empl, 'age')    # 删除属性 'age'
    
    
    //内置类属性
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    class Employee:
       '所有员工的基类'
       empCount = 0
        def __init__(self, name, salary):
          self.name = name
          self.salary = salary
          Employee.empCount += 1
       def displayCount(self):
         print "Total Employee %d" % Employee.empCount
       def displayEmployee(self):
          print "Name : ", self.name,  ", Salary: ", self.salary
    print "Employee.__doc__:", Employee.__doc__             //帮助文档
    print "Employee.__name__:", Employee.__name__           //类名
    print "Employee.__module__:", Employee.__module__       //模块
    print "Employee.__bases__:", Employee.__bases__         //类的所有父类构成元素
    print "Employee.__dict__:", Employee.__dict__           //类的属性
    
    
    //
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    class Parent:        # 定义父类
       parentAttr = 100
       def __init__(self):
          print "调用父类构造函数"
       def parentMethod(self):
          print '调用父类方法'
       def setAttr(self, attr):
          Parent.parentAttr = attr
       def getAttr(self):
          print "父类属性 :", Parent.parentAttr
    
    class Child(Parent): # 定义子类
       def __init__(self):
          print "调用子类构造方法"
    
       def childMethod(self):
          print '调用子类方法 child method'
    
    c = Child()          # 实例化子类
    c.childMethod()      # 调用子类的方法
    c.parentMethod()     # 调用父类方法
    c.setAttr(200)       # 再次调用父类的方法
    c.getAttr()          # 再次调用父类的方法
    输出结果
    调用子类构造方法
    调用子类方法 child method
    调用父类方法
    父类属性 : 200
    
    //继承多个类
    class A:        # 定义类 A
    .....
    class B:         # 定义类 B
    .....
    class C(A, B):   # 继承类 A 和 B
    .....
    
    //方法重写
    #!/usr/bin/python
    class parent():
        def mymethod(self):
            print 'this is a test'
        def bp(self):
            print 'how are you'
    class child(parent):
        def __init__(self):
            print 'I am bp'
        def mymethod(self):
            print 'this is not a test'
    c=child()
    c.mymethod()
    c.bp()
    输出结果
    I am bp
    this is not a test
    how are you
    
    
    //私有方法和属性
    class JustCounter:
        __secretCount = 0  # 私有变量
        publicCount = 0    # 公开变量
    
        def count(self):
            self.__secretCount += 1
            self.publicCount += 1
            print self.__secretCount
    
    counter = JustCounter()
    counter.count()
    counter.count()
    print counter.publicCount
    print counter.__secretCount  # 报错,实例不能访问私有变量
    输出结果
    1
    2
    2
    Traceback (most recent call last):
      File "test.py", line 17, in <module>
        print counter.__secretCount  # 报错,实例不能访问私有变量
    AttributeError: JustCounter instance has no attribute '__secretCount'
  • 相关阅读:
    程序员的7中武器
    需要强化的知识
    微软中国联合小i推出MSN群Beta 不需任何插件
    XML Notepad 2006 v2.0
    Sandcastle August 2006 Community Technology Preview
    [推荐] TechNet 广播 SQL Server 2000完结篇
    《太空帝国 4》(Space Empires IV)以及 xxMod 英文版 中文版 TDM Mod 英文版 中文版
    IronPython 1.0 RC2 更新 1.0.60816
    Microsoft .NET Framework 3.0 RC1
    《Oracle Developer Suite 10g》(Oracle Developer Suite 10g)V10.1.2.0.2
  • 原文地址:https://www.cnblogs.com/biaopei/p/7730581.html
Copyright © 2011-2022 走看看