class Bar: def objB(self,args): print(self,args) print(self,self.name,self.age,args) obj = Bar() obj.name = "alex" obj.age = "25" obj.objB(123) print("------------------------------------") obj1 = Bar() obj1.name = "jon" obj1.age = "26" obj1.objB(212)
class Bar: #定义类 def objB(self,falgs): #方法,第一个参数就是self print(self,self.name,self.age,falgs) #self 就是调用那个对象就是那个对象 return 1 #(这个返回值返回给对象) obj = Bar() #创建对象 obj.name = "alex" #在对象中添加参数(obj是指向Bar对象的内存空间) obj.age = "25" obj.objB(123) #对象.函数名(参数) print("------------------------------------") obj1 = Bar() obj1.name = "jon" obj1.age = "26" o = obj1.objB(212) #对象.函数名(参数) print(o) #获取返回值