python 第九课

私有属性:

class test:
    #私有属性   均不能在类外面被调用
    __privateattr = 0  #只能在当前类中调用
    def __init__(self,name):
        self.name = name
    #方法
    def show(self):
        self.__privateattr = 1
        print("这是show方法...%d,姓名:%s"%(self.__privateattr,self.name))

t = test("张三")
t.show()
#私有方法
class Dog:
    #私有方法  比较重要的  用判断达到条件才能用
    def __send_msg(self):
        print("------正在发送短信-------")
    #公共方法
    def send_msg(self,new_money):
        if new_money>100:
            self.__send_msg()
        else:
            print("余额不足,请先充值 再发送短信")
dog = Dog()
dog.send_msg(1000)
多态:

class Dog(object):
    def print_self(self):
        print("大家好!")
class Xiaotq(Dog):
    def print_self(self):
        print("hello")
def introduce(temp):  #多态在定义   只有在调用的时候才知道用谁
    temp.print_self()
dog1 = Dog()
dog2 = Xiaotq()
introduce(dog1)  # 多态的调用方法
introduce(dog2)
实例:
class bird:
    def __init__(self,name):
        self.name =name
    def fight(self):
        print("%s:弹射飞"%self.name )
    def attack(self):
        print("分裂攻击..." )
    def cry(self):
        print("嗷---!")
class fenlie(bird):
    def attack(self):
        print("分裂攻击" )
class huojian(bird):
    def attack(self):
        print("加速冲撞" )
class hongse(bird):
    def attack(self):
        print("普通攻击" )
    def cry(self):
        print("喳喳")
class zhadan(bird):
    def attack(self):
        print("爆炸攻击")
    def cry(self):
        print("喳喳")
class pangzi(bird):
    def attack(self):
        print("扔蛋攻击" )
    def cry(self):
        print("不叫" )
class duo:  #*******
    def num(self,temp):
        temp.fight()
        temp.attack()
        temp.cry()
fen=fenlie("分裂鸟")
huo=huojian("火箭鸟")
hong=hongse("红色鸟")
zha=zhadan("炸弹鸟")
pang=pangzi("胖子鸟")
d = duo()  #最后的调用
d.num(huo)
# 类属性  和 实例属性
class Tool(object):
    #类属性  属于类成员,属于对象共有的
    num = 0
    name = []
    #方法
    def __init__(self,new_name):
        self.name = new_name
        Tool.name.append(self.name)
        Tool.num += 1
tool1=Tool("铁锹")
tool2=Tool("工兵铲")
tool3=Tool("水桶")
print(Tool.num)
print(Tool.name)
# 实例方法   类方法   静态方法
class Game(object):
    # 类属性
    num = 0
    #实例方法
    def __init__(self):
        #实例属性
        self.name = "laowang"
    #类方法
    @classmethod
    def add_num(cls):
        cls.num = 100
    # 静态方法   是类方法和实例方法都没有关系就,用静态方法来做
    @staticmethod
    def print_menu():
        print("------------------")
        print("   穿越火线V11.1")
        print("1.开始游戏   ")
        print("2.结束游戏   ")
        print("------------------")
game = Game()
#Game.add_num()# 可以通过类的名字调用类方法
game.add_num()# 还可以通过这个类创建出来的对象 去调用这个类方法
print(Game.num)
#Game.print_menu()# 通过类 去调用静态方法
game.print_menu() #通过实例对象 去调用静态方法


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值