繁体   English   中英

请纠正我。 我写的代码在笔记本里

[英]Correct me please . The code I wrote was in the notebook

class Student:
    workshop = 'python'
    def _init_(self, name, age):
        self.name = name
        self.age = age
    def describe(self):
        print(self.name,"is",self.age,"years old and participating in",
              Student.workshop,"class")
        return

student1 = Student("Sridhar", 35 )
student1.name   

init方法需要两个下划线__ 因此,

改变这个:

def _init_(self, name, age):

对此:

def __init__(self, name, age):

所以:

class Student:
    workshop = 'python'
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def describe(self):
        print(self.name,"is",self.age,"years old and participating in",
              Student.workshop,"class")
        return

student1 = Student("Sridhar", 35)
print(student1.name)
student1.describe()

OUTPUT:

Sridhar
Sridhar is 35 years old and participating in python class

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM