# Enter a codeclass Animal(object): def __init__(self,age,name,location): self.__age = age self.__name = name self.__location = location #Defined the fuction that can get the details def get_age(self): return self.__age def get_name(self): return self.__name def get_location(self): return self.__location def get_details(self): print('name:{},age:{},location:{}'.format(self.get_name,self.get_age,self.get_location)) #Defined the fuction that can modify the instance def set_age(self,age): self.__age = age def set_name(self,name): self.__name = name def set_location(self,location): self.__location = location dog = Animal(18,'Jack','Asia') print(dog.get_details)
class Animal(object): def __init__(self,age,name,location): self.__age = age self.__name = name self.__location = location def get_age(self): return self.__age def get_name(self): return self.__name def get_location(self): return self.__location def get_details(self): print('name:{},age:{},location:{}'.format(self.get_name(),self.get_age(),self.get_location())) def set_age(self,age): self.__age = age def set_name(self,name): self.__name = name def set_location(self,location): self.__location = location dog = Animal(18,'Jack','Asia') dog.get_details()
方法 get_details(self) 中,self.get_name,self.get_amge, self.get_location调用是属性,而并没有定义这个属性,所以打印出应该是'name:,age:,location:'才对。如果调用方法,就是少了括号
输出后是> 不好意思 打错了