面向对象基础
第一个面向对象
class Cat :
def eat ( self) :
print ( "小猫爱吃鱼" )
def drink ( self) :
print ( "小猫要喝水" )
tom = Cat( )
tom. eat( )
tom. drink( )
print ( tom)
addr = id ( tom)
print ( "%x" % addr)
新建两个猫对象
class Cat :
def eat ( self) :
print ( "小猫爱吃鱼" )
def drink ( self) :
print ( "小猫要喝水" )
tom = Cat( )
tom. eat( )
tom. drink( )
print ( tom)
lazy_cat = Cat( )
lazy_cat. eat( )
lazy_cat. drink( )
print ( lazy_cat)
lazy_cat2 = lazy_cat
print ( lazy_cat2)
设置对象属性
class Cat :
def eat ( self) :
print ( "%s 爱吃鱼" % self. name)
def drink ( self) :
print ( "%s 要喝水" % self. name)
tom = Cat( )
tom. name = "Tom"
tom. eat( )
tom. drink( )
print ( tom)
lazy_cat = Cat( )
lazy_cat. name = "大懒猫"
lazy_cat. eat( )
lazy_cat. drink( )
print ( lazy_cat)
在外界设置属性的问题
class Cat :
def eat ( self) :
print ( "%s 爱吃鱼" % self. name)
def drink ( self) :
print ( "%s 要喝水" % self. name)
tom = Cat( )
tom. eat( )
tom. drink( )
tom. name = "Tom"
初始化方法
class Cat :
def __init__ ( self) :
print ( "这是一个初始化方法" )
self. name = "Tom"
def eat ( self) :
print ( "%s 爱吃鱼" % self. name)
tom = Cat( )
print ( tom. name)
利用参数设置属性初始值
class Cat :
def __init__ ( self, new_name) :
print ( "这是一个初始化方法" )
self. name = new_name
def eat ( self) :
print ( "%s 爱吃鱼" % self. name)
tom = Cat( "Tom" )
print ( tom. name)
lazy_cat = Cat( "大懒猫" )
lazy_cat. eat( )
_del_方法
class Cat :
def __init__ ( self, new_name) :
self. name = new_name
print ( "%s 来了" % self. name)
def __del__ ( self) :
print ( "%s 我去了" % self. name)
tom = Cat( "Tom" )
print ( tom. name)
del tom
print ( "-" * 50 )
_str_方法
class Cat :
def __init__ ( self, new_name) :
self. name = new_name
print ( "%s 来了" % self. name)
def __del__ ( self) :
print ( "%s 我去了" % self. name)
def __str__ ( self) :
return "我是小猫[%s]" % self. name
tom = Cat( "Tom" )
print ( tom)
小明爱跑步
class Person :
def __init__ ( self, name, weight) :
self. name = name
self. weight = weight
def __str__ ( self) :
return "我的名字叫 %s 体重是 %.2f 公斤" % ( self. name, self. weight)
def run ( self) :
print ( "%s 爱跑步,跑步锻炼身体" % self. name)
self. weight -= 0.5
def eat ( self) :
print ( "%s 是吃货,吃完这顿再减肥" % self. name)
self. weight += 1
xiaoming = Person( "小明" , 75.0 )
xiaoming. run( )
xiaoming. eat( )
print ( xiaoming)
class Person :
def __init__ ( self, name, weight) :
self. name = name
self. weight = weight
def __str__ ( self) :
return "我的名字叫 %s 体重是 %.2f 公斤" % ( self. name, self. weight)
def run ( self) :
print ( "%s 爱跑步,跑步锻炼身体" % self. name)
self. weight -= 0.5
def eat ( self) :
print ( "%s 是吃货,吃完这顿再减肥" % self. name)
self. weight += 1
xiaoming = Person( "小明" , 75.0 )
xiaoming. run( )
xiaoming. eat( )
print ( xiaoming)
xiaomei = Person( "小美" , 45 )
xiaomei. eat( )
xiaomei. run( )
print ( xiaomei)
print ( xiaoming)
摆放家具
class HouseItem :
def __init__ ( self, name, area) :
self. name = name
self. area = area
def __str__ ( self) :
return "[%s] 占地 %.2f" % ( self. name, self. area)
bed = HouseItem( "席梦思" , 4 )
chest = HouseItem( "衣柜" , 2 )
table = HouseItem( "餐桌" , 1.5 )
print ( bed)
print ( chest)
print ( table)
class HouseItem :
def __init__ ( self, name, area) :
self. name = name
self. area = area
def __str__ ( self) :
return "[%s] 占地 %.2f" % ( self. name, self. area)
class House :
def __init__ ( self, house_type, area) :
self. house_type = house_type
self. area = area
self. free_area = area
self. item_list = [ ]
def __str__ ( self) :
return ( "户型:%s\n总面积:%.2f[剩余:%.2f]\n家具:%s"
% ( self. house_type, self. area,
self. free_area, self. item_list) )
def add_item ( self, item) :
print ( "要添加 %s" % item)
bed = HouseItem( "席梦思" , 4 )
chest = HouseItem( "衣柜" , 2 )
table = HouseItem( "餐桌" , 1.5 )
print ( bed)
print ( chest)
print ( table)
my_home = House( "两室一厅" , 60 )
my_home. add_item( bed)
my_home. add_item( chest)
my_home. add_item( table)
print ( my_home)
class HouseItem :
def __init__ ( self, name, area) :
self. name = name
self. area = area
def __str__ ( self) :
return "[%s] 占地 %.2f" % ( self. name, self. area)
class House :
def __init__ ( self, house_type, area) :
self. house_type = house_type
self. area = area
self. free_area = area
self. item_list = [ ]
def __str__ ( self) :
return ( "户型:%s\n总面积:%.2f[剩余:%.2f]\n家具:%s"
% ( self. house_type, self. area,
self. free_area, self. item_list) )
def add_item ( self, item) :
print ( "要添加 %s" % item)
if item. area > self. free_area:
print ( "%s 的面积太大了,无法添加" % item. name)
return
self. item_list. append( item. name)
self. free_area -= item. area
bed = HouseItem( "席梦思" , 40 )
chest = HouseItem( "衣柜" , 2 )
table = HouseItem( "餐桌" , 20 )
print ( bed)
print ( chest)
print ( table)
my_home = House( "两室一厅" , 60 )
my_home. add_item( bed)
my_home. add_item( chest)
my_home. add_item( table)
print ( my_home)
士兵突击
class Gun :
def __init__ ( self, model) :
self. model = model
self. bullet_count = 0
def add_bullet ( self, count) :
self. bullet_count += count
def shoot ( self) :
if self. bullet_count <= 0 :
print ( "[%s] 没有子弹了..." % self. model)
return
self. bullet_count -= 1
print ( "[%s] 突突突... [%d]" % ( self. model, self. bullet_count) )
ak47 = Gun( "AK47" )
ak47. add_bullet( 50 )
ak47. shoot( )
私有属性和方法
class Women :
def __init__ ( self, name) :
self. name = name
self. __age = 18
def __secret ( self) :
print ( "%s 的年龄是 %d" % ( self. name, self. __age) )
xiaofang = Women( "小芳" )
class Gun :
def __init__ ( self, model) :
self. model = model
self. bullet_count = 0
def add_bullet ( self, count) :
self. bullet_count += count
def shoot ( self) :
if self. bullet_count <= 0 :
print ( "[%s] 没有子弹了..." % self. model)
return
self. bullet_count -= 1
print ( "[%s] 突突突... [%d]" % ( self. model, self. bullet_count) )
class Soldier :
def __init__ ( self, name) :
self. name = name
self. gun = None
ak47 = Gun( "AK47" )
ak47. add_bullet( 50 )
ak47. shoot( )
xusanduo = Soldier( "许三多" )
xusanduo. gun = ak47
print ( xusanduo. gun)
class Gun :
def __init__ ( self, model) :
self. model = model
self. bullet_count = 0
def add_bullet ( self, count) :
self. bullet_count += count
def shoot ( self) :
if self. bullet_count <= 0 :
print ( "[%s] 没有子弹了..." % self. model)
return
self. bullet_count -= 1
print ( "[%s] 突突突... [%d]" % ( self. model, self. bullet_count) )
class Soldier :
def __init__ ( self, name) :
self. name = name
self. gun = None
def fire ( self) :
if self. gun is None :
print ( "[%s] 还没有枪..." % self. name)
return
print ( "冲啊...[%s]" % self. name)
self. gun. add_bullet( 50 )
self. gun. shoot( )
ak47 = Gun( "AK47" )
xusanduo = Soldier( "许三多" )
xusanduo. gun = ak47
xusanduo. fire( )
print ( xusanduo. gun)
伪私有属性和方法
class Women :
def __init__ ( self, name) :
self. name = name
self. __age = 18
def __secret ( self) :
print ( "%s 的年龄是 %d" % ( self. name, self. __age) )
xiaofang = Women( "小芳" )
print ( xiaofang. _Women__age)
xiaofang. _Women__secret( )