python 类

发布时间:2024-12-12 15:21

学习Python基础语法:https://www.runoob.com/python/python-tutorial.html #生活技巧# #工作学习技巧# #编程学习资源#

class Role(object):

num=123

n_list=[]

def __init__(self,name,role,weapon,life_value=100,money=15000):

self.name=name

self.role=role

self.__weapon=weapon

def __del__(self):

print("%sover"%self.name)

def shut(self):

print("wawawa")

def __lift_shut(self):

print("疼疼疼")

def use_weapon(self):

print(self.__weapon)

self.__lift_shut()

name1=Role("1","2","3")

name1.n_list=[]

name1.n_list.append("123")

print(name1.name)

name2=Role("1a","2","3")

name2.n_list.append("123")

print(name2.n_list)

del name1,name2

name3=Role("gongwei","busyness","money")

name3.use_weapon()

print(name3._Role__weapon)

name3._Role__lift_shut()

class Role1(Role):

def __init__(self,name,role,weapon,mimi,life_value=100,money=15000):

super(Role1,self).__init__(name,role,weapon,life_value=100,money=15000)

self.mimi=mimi

m=Role1("a","b","c","d")

print(m.name)

class mf(object):

def makefrind(self,obj):

print("%s make frient with %s"%(self.name,obj.name))

self.age.append(obj)

class reman(object):

def __init__(self,name,age):

self.name=name

self.age=[""]

class man(reman,mf):

def eat(self):

print("eating")

class woman(reman,mf):

def do(self):

print("doing")

a1=man("a",1)

a2=woman("b",2)

a1.makefrind(a2)

a2.name="B"

print(a1.age[1].name)

class A:

def __init__(self):

print("ok")

python 的鸭子模型:

class  a():

    def say(self)

          print("ok")

class  b():

    def say(self)

          print("ok")

class  c():

    def tell(self)

          print("ok")

class  g():

    def say(self)

          print("ok")

    def tell(self)

          print("ok")

当用一个接口调用

def jiekou(f):

    f.say()

a,b都可以,但是c也能传进去,但是会报错,报的 是c没有这个方法,f也能调用,很明显f不属于a,b只是f有say这个方法,所以就认定是a这一类,这就是鸭子模型

网址:python 类 https://www.yuejiaxmz.com/news/view/453870

相关内容

python
Python-Docx:Word与Python的完美结合
Python标准模块——gzip
Python中GBK, UTF
Python自动化任务
Python笔记——Python中is和==的区别
Python怎么实现定时任务?python自动化定时方法
python panda
Python可以做什么?你所不知道的Python——生活中的奇妙用途
python写小软件

随便看看