簡體   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