简体   繁体   中英

How to call a function in a class?

I try to call a function in a class from my Main()

Basically, I try to built an Employee class with internal function. In my main, I try to call these function but it doesn't work.

Am I missing something ?

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 ?

Why doesn't the DisplayEmployee() function print anything in my console?

您应该不确定if __name__ == '__main__'块,并且调用应为x.displayEmployee() ,注意括号。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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