[英]How to call a function in a class?
我嘗試從Main()
調用類中的函數
基本上,我嘗試構建具有內部功能的Employee
類。 在我的主機中,我嘗試調用這些函數,但是它不起作用。
我想念什么嗎?
class Employee:
'Common base class for all employees'
empCount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def displayEmployee(self):
print("Name : ", self.name, ", Salary: ", self.salary)
def test(self):
print("This is a test")
if __name__ == '__main__':
x = Employee("Maxime", 75000)
y = Employee("Sacha", 100000)
x.displayEmployee # Nothing appear in my console ?
x.test # Nothing appear in my console ?
為什么DisplayEmployee()
函數不能在控制台中打印任何內容?
您應該不確定if __name__ == '__main__'
塊,並且調用應為x.displayEmployee()
,注意括號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.