Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28711839
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Python/Ruby

2010-04-17 16:46:11

1、整理__str__ 说明:
class DoubleRep(object):
    def __str__(self):
        return "a __str__"
dr = DoubleRep()
print dr

就是说:当你直接打印一个对象的时候就会使用__str__里面的内容了。

构造函数、析构函数
__init__(self)
__del__(self)         将该关闭的资源在析构函数中做相关处理!

class counter:
    count = 0         类变量。相当于JAVA中的static                
    def __init__(self):
      self.__class__.count += 1
class Student
  def __init__(self)
      self.name = "abc"  类的成员变量


只读属性(类必须从object继承,否则就不是只读的)
class Parrot(object):                [从此类继承]
    def __init__(self):
        self._voltage = 100000       [只读]
    @property
    def voltage(self):
        """Get the current voltage."""
        return self._voltage


私有变量
class Student:
    def __init__(self):
        self.__name="abc"                   [有点怪怪的]
很简单就是通过__ 两个下划线开头,但不是结尾的。就是私有了


私有方法
class Student:
    def __Getage(self):
        pass






阅读(534) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-06-03 10:57:01

__str__ 也是类自省的一种说法。可以看一下这个类是做什么的