#方式一、为对象初始化自己独有的特征
class People:
country='China'
x=1
def run(self):
print('----->', self)
# 实例化出三个空对象
obj1=People()
obj2=People()
obj3=People()
# 为对象定制自己独有的特征
obj1.name='egon'
obj1.age=18
obj1.sex='male'
obj2.name='lxx'
obj2.age=38
obj2.sex='female'
obj3.name='alex'
obj3.age=38
obj3.sex='female'
# print(obj1.__dict__)
# print(obj2.__dict__)
# print(obj3.__dict__)
# print(People.__dict__)
#方式二、为对象初始化自己独有的特征
class People:
country='China'
x=1
def run(self):
print('----->', self)
# 实例化出三个空对象
obj1=People()
obj2=People()
obj3=People()
# 为对象定制自己独有的特征
def chu_shi_hua(obj, x, y, z): #obj=obj1,x='egon',y=18,z='male'
obj.name = x
obj.age = y
obj.sex = z
chu_shi_hua(obj1,'egon',18,'male')
chu_shi_hua(obj2,'lxx',38,'female')
chu_shi_hua(obj3,'alex',38,'female')
#方式三、为对象初始化自己独有的特征
class People:
country='China'
x=1
def chu_shi_hua(obj, x, y, z): #obj=obj1,x='egon',y=18,z='male'
obj.name = x
obj.age = y
obj.sex = z
def run(self):
print('----->', self)
obj1=People()
# print(People.chu_shi_hua)
People.chu_shi_hua(obj1,'egon',18,'male')
obj2=People()
People.chu_shi_hua(obj2,'lxx',38,'female')
obj3=People()
People.chu_shi_hua(obj3,'alex',38,'female')
# 方式四、为对象初始化自己独有的特征
class People:
country='China'
x=1
def __init__(obj, x, y, z): #obj=obj1,x='egon',y=18,z='male'
obj.name = x
obj.age = y
obj.sex = z
def run(self):
print('----->', self)
obj1=People('egon',18,'male') #People.__init__(obj1,'egon',18,'male')
obj2=People('lxx',38,'female') #People.__init__(obj2,'lxx',38,'female')
obj3=People('alex',38,'female') #People.__init__(obj3,'alex',38,'female')
# __init__方法
# 强调:
# 1、该方法内可以有任意的python代码
# 2、一定不能有返回值
class People:
country='China'
x=1
def __init__(obj, name, age, sex): #obj=obj1,x='egon',y=18,z='male'
# if type(name) is not str:
# raise TypeError('名字必须是字符串类型')
obj.name = name
obj.age = age
obj.sex = sex
def run(self):
print('----->', self)
# obj1=People('egon',18,'male')
obj1=People(3537,18,'male')
# print(obj1.run)
# obj1.run() #People.run(obj1)
# print(People.run)