簡體   English   中英

用main在python中編寫一個類

[英]writing a class in python with main

我已經基於我所在類的框架編寫了以下代碼,它基於類的概念。 我已經編寫了類並編寫了主類,但是我不斷遇到相同的錯誤,即該類中的函數未定義,這是我的代碼:

class Person:
    population = 0
    def __init__(self, name):
        self.name = name
        print ('Initializing' ,self.name)
        Person.population += 1
    def __del__(self):
        print (self.name,"says bye.")
        Person.population -= 1
        if Person.population == 0:
            print ('I was the last one.')
        else:
            print ('There is/are still',Person.population ,'person/persons left.')  
    def sayHi(self):
        print("Hi, my name is ",self.name)
    def howMany(self):
        #Prints the current population.
        print(Person.population)
        #If there is only 1 person then it should print "I am the only one"
        if Person.population == 1:
            print("I am the only one")
            #If there are 2 people for example , then print "We have 2 people here"
        if Person.population == 2:
            print("We have 2 people here")
def main():

  # Step 1: Ask for names of 2 people
  x=input("name of person one?")
  y=input("name of person two?") 
  # Step 2: Initialize Person 1
  init1=__init__(x)
  # Step 3: Use function sayHi() for Person 1
  init1.sayHi()
  # Step 4: Use howMany() for Person 1
  init1.howMany()
  # Step 5: Initialize Person 2
  init2=__init__(y)
  # Step 6: Use function sayHi() for Person 2
  init2.sayHi()
  # Step 7: Use howMany() for Person 2
  init2.howMany()
  # Step 8: Say Hi to Person 1
  # Step 9: Use howMany() for Person 1
  init1.howMany()
  # Step 10: Terminate Person 1
  init1.__del__()
  # Step 11: Terminate Person 2
  init2.__del__()

main()

它會不斷產生錯誤NameError: name __init__ is not defined誰能給我任何有關為什么發生這種情況的見解?

class Person:
 population = 0
 def __init__(self, name):
    self.name = name
    print ('Initializing' ,self.name)
    Person.population += 1
 def __del__(self):
    print (self.name,"says bye.")
    Person.population -= 1
    if Person.population == 0:
        print ('I was the last one.')
    else:
        print ('There is/are still',Person.population ,'person/persons left.')  
 def sayHi(self):
    print("Hi, my name is ",self.name)
 def howMany(self):
    #Prints the current population.
    print(Person.population)
    #If there is only 1 person then it should print "I am the only one"
    if Person.population == 1:
        print("I am the only one")
            #If there are 2 people for example , then print "We have 2 people here"
    if Person.population == 2:
        print("We have 2 people here")
def main():

  # Step 1: Ask for names of 2 people
  x=input("name of person one?")
  y=input("name of person two?") 
  # Step 2: Initialize Person 1
  init1= Person(x)
  # Step 3: Use function sayHi() for Person 1
  init1.sayHi()
  # Step 4: Use howMany() for Person 1
  init1.howMany()
  # Step 5: Initialize Person 2
  init2= Person(y)
  # Step 6: Use function sayHi() for Person 2
  init2.sayHi()
  # Step 7: Use howMany() for Person 2
  init2.howMany()
  # Step 8: Say Hi to Person 1
  # Step 9: Use howMany() for Person 1
  init1.howMany()
  init1 = None
  init2 = None

main()

__init__是在您的類中定義的。 因此,沒有此類函數__init__可以在類外部調用,而您可以在main進行調用。

您試圖將__init__作為全局函數調用。 這是Person一種方法。 盡管如此,您無需自己顯式調用__init__

你可以打電話。

init1 = Person(x)

同樣,您也不需要顯式調用__del__ 當對象被垃圾回收時(在腳本末尾),它將被自動調用。 因此,您只需刪除對__del__的調用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM