python 中类 ( class )实验
class Person: #类名称 a = 0 #初始化一个起始值 def __init__(self,name,height,weight): #类的初始化,第一个参数必须是 self,后面参数分别表示为 名称,身高,体重 self.name = name #访问对应的变量 self.height = height self.weight = weight Person.a += 1 #每调用一次Person类,起始变量 a 加 1 def ShowPersion(self): #定义一个显示人的类内容方法,必须包含 self print ("Name :", self.name,"Height: ", self.height, "Weight: ", self.weight) # 给类实例赋值 Person1 = Person("金辉",175,60) Person2 = Person("任宇",173,70) Person3 = Person("小军",180,80) Person4 = Person("sun",180,80) Person4 = Person("mm",180,80) print (Person1.weight) Person1.ShowPersion() print(Person2.height) Person3.ShowPersion() print(Person.a) 输出结果内容: 60 Name : 金辉 Height: 175 Weight: 60 173 Name : 小军 Height: 180 Weight: 80 5