便民 发表于 2018-8-9 09:10:27

Python中类的属性、方法及内置方法

#!/usr/bin/env python  #-*- coding:utf-8-*-
  class People(object):
  color = 'yellow'
  __age = 30   #私有属性
  class Chinese(object):
  name ="I am a Chinese."
  def __str__(self):

  return "This is People>  def __init__(self,c='white'):   #类实例化时自动执行
  print ("initing...")
  self.color = c
  self.think()
  f = open('test.py')
  def think(self):
  self.color = "black"
  print "I am a %s "% self.color
  print ("I am a thinker")
  print self.__age
  def__talk(self):
  print "I am talking with Tom"
  @classmethod #调用类的方法
  def test(self):

  print ("this is>  @staticmethod#调用类的方法
  def test1():
  print ("this is static method")
  def __del__(self):
  print ("del....")
  self.f.close()
  print gc.collect()   如果是0是没有回收的。
  jack = People('green')
  ren = People()            #实例化外部类
  print ren.color      #通过对象访问属性是初始化后的值
  print People.color    #通过类访问还是原来的值
页: [1]
查看完整版本: Python中类的属性、方法及内置方法