天高云淡 发表于 2018-8-9 07:08:08

python中类的学习笔记(源码版)

#定义一个类(define a>class Cat:  #属性(attribution)
  #方法(methods)
  def eat(self):
  print("cat is eating a fish.")
  def drink(slef):
  print("cat is drinking milk.")
  def introduce(self):
  print("%s's age is %d."%(self.chinese_name,self.age))
  #创建一个对象(creating an object)
  tom = Cat()
  #调用一个对象的方法(method to invoke an object)
  tom.eat()
  tom.drink()
  #蠢办法添加属性(stupid method to add attributions)
  tom.chinese_name = "汤姆"
  tom.age = 18
  #获取对象的属性(the first way to getproperties of objects )
  tom.introduce()
  #创建多个对象,这里创建第二个对象blue_cat(create multiple objects,and the second creates bule cat)
  blue_cat = Cat()
  blue_cat.chinese_name = "蓝猫"
  blue_cat.age = 8
  blue_cat.introduce()
页: [1]
查看完整版本: python中类的学习笔记(源码版)