Python全系列 教程
3567个小节阅读:5930.7k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
类也是对象(属于元类的对象)
xxxxxxxxxx
#打印字符串(字符串是对象)
print("HelloWorld")
#打印类名,类同样为一个对象
print(Person)
使用动态创建类:
xxxxxxxxxx
type(类名,由父类名称组成的元组(可以为空),包含属性的字典(名称和值))
xxxxxxxxxx
案例1:使用type创建类
Myclass = type("MyClass3",(),{})
m1 = Myclass()
print(type(m1))
xxxxxxxxxx
案例2:使用type创建带有属性(方法)的类
def show(self):
print("---num---%d"%self.num)
# 使用元类(type)创建类
Test = type("Test",(),{"show":show})
t = Test()
t.num = 100
t.show()
xxxxxxxxxx
案例3:使用type动态创建一个继承指定类的类
class Animal():
def __init__(self,color="Yellow"):
self.color = color
def eat(self):
print("吃死你")
Dog = type("Dog",(Animal,),{})
dog = Dog()
dog.eat()
print(dog.color)