Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python?为什么子类从父类继承?当您使用init of方法分配父级时?但它不起作用 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 文件“/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_umd.py”,第198行,在runfile中 pydev_imports.execfile(文件名、全局变量、本地变量)#执行脚本 文件“/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py”,execfile中第18行 exec(编译(内容+“\n”,文件,'exec'),全局,loc) 文件“/Users/songpengfei/PycharmProjects/untitled1/review.py”,第69行,在 stu.print_info() 文件“/Users/songpengfei/PycharmProjects/untitled1/review.py”,第60行,打印信息 打印(“我的学名是{}姓名是{}年龄是{}”。格式(self.\uuuu学校名称,self.\uuu姓名,self.\uu年龄)) AttributeError:“Student”对象没有属性“\u Student\u name”_Python_Oop_Object_Parameters_Init - Fatal编程技术网

用python?为什么子类从父类继承?当您使用init of方法分配父级时?但它不起作用 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 文件“/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_umd.py”,第198行,在runfile中 pydev_imports.execfile(文件名、全局变量、本地变量)#执行脚本 文件“/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py”,execfile中第18行 exec(编译(内容+“\n”,文件,'exec'),全局,loc) 文件“/Users/songpengfei/PycharmProjects/untitled1/review.py”,第69行,在 stu.print_info() 文件“/Users/songpengfei/PycharmProjects/untitled1/review.py”,第60行,打印信息 打印(“我的学名是{}姓名是{}年龄是{}”。格式(self.\uuuu学校名称,self.\uuu姓名,self.\uu年龄)) AttributeError:“Student”对象没有属性“\u Student\u name”

用python?为什么子类从父类继承?当您使用init of方法分配父级时?但它不起作用 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 文件“/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_umd.py”,第198行,在runfile中 pydev_imports.execfile(文件名、全局变量、本地变量)#执行脚本 文件“/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py”,execfile中第18行 exec(编译(内容+“\n”,文件,'exec'),全局,loc) 文件“/Users/songpengfei/PycharmProjects/untitled1/review.py”,第69行,在 stu.print_info() 文件“/Users/songpengfei/PycharmProjects/untitled1/review.py”,第60行,打印信息 打印(“我的学名是{}姓名是{}年龄是{}”。格式(self.\uuuu学校名称,self.\uuu姓名,self.\uu年龄)) AttributeError:“Student”对象没有属性“\u Student\u name”,python,oop,object,parameters,init,Python,Oop,Object,Parameters,Init,简而言之,您应该使用属性self.name而不是字段self.\u name。同样,您应该只使用@属性中以开头的名称。否则,请使用该属性。因此,例如,使用self.age而不是self.\uu age在@属性之外。当您在字段名的开头使用\uuuuu时,它对该类是“私有的”,因为python会进行所谓的“名称篡改”。我建议您用谷歌搜索”python名称篡改“了解它是如何工作的。将print\u info行的第一行替换为print(“我的学名是{}姓名是{}年龄是{}”。格式(self.\uuuuu

简而言之,您应该使用属性
self.name
而不是字段
self.\u name
。同样,您应该只使用
@属性
中以
开头的名称。否则,请使用该属性。因此,例如,使用
self.age
而不是
self.\uu age
@属性

之外。当您在字段名的开头使用
\uuuuu
时,它对该类是“私有的”,因为python会进行所谓的“名称篡改”。我建议您用谷歌搜索”python名称篡改“了解它是如何工作的。将
print\u info
行的第一行替换为
print(“我的学名是{}姓名是{}年龄是{}”。格式(self.\uuuuu学校名称,self.\uPerson\uu姓名,self.\uPerson\uuuuu年龄))
。我删除了一个似乎出于政治动机的meme。如果这与你的问题有任何关系,请翻译并解释它的相关性。@HenryTjhia非常感谢你,你的答案是正确的,非常正确complex@Code-学徒Henry Tjhia是对的你的回答也是对的
class Person:


    @property
    def name(self):
        return self.__name

    @name.setter
    def name(self,name):
        self.__name = name


    @property
    def age(self):
        return self.__age

    @age.setter
    def age(self,age):
        self.__age = age



    def __init__(self,name,age):

        self.__name = name
        self.__age = age



        pass

    def print_info(self):

        print("my name is {} and my age is {}".format(self.__name,self.__age))

        pass

class Student(Person):

    @property
    def school_name(self):
        return self.__school_name

    @school_name.setter
    def school_name(self,school_name):
        return self.__school_name


    def __init__(self,school_name,name,age):
        Person.__init__(self,name,age)
        self.__school_name = school_name

        pass


    def print_info(self):

        print("my schoolname is {} name is {} age is {}".format(self.__school_name,self.__name,self.__age))
        print("hello")


    pass

if __name__ == '__main__':

    stu = Student("primary school","songpengfei",22)
    stu.print_info()




    pass
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/songpengfei/PycharmProjects/untitled1/review.py", line 69, in <module>
    stu.print_info()
  File "/Users/songpengfei/PycharmProjects/untitled1/review.py", line 60, in print_info
    print("my schoolname is {} name is {} age is {}".format(self.__school_name,self.__name,self.__age))
AttributeError: 'Student' object has no attribute '_Student__name'