OOP tutorial 1: Class and Instances

1.

class Employee:
    pass


# Class is the blueprint for creating instances
# Each unique employee we create in the Employee class will be an instance of that class, or we can call it object.
emp_1 = Employee()
emp_2 = Employee()

print(emp_1)
print(emp_2)


# Instance variable contains data that is unique in each instance.
emp_1.first = "Corey"
emp_1.last = "Schafer"
emp_1.email = "Corey.Schafer@company.com"
emp_1.pay = 50000

emp_2.first = "Test"
emp_2.last = "User"
emp_2.email = "Test.User@company.com"
emp_2.pay = 60000

print(emp_1.email)
print(emp_2.email)

Output:

<__main__.Employee object at 0x101153fd0>
<__main__.Employee object at 0x101153f40>
Corey.Schafer@company.com
Test.User@company.com

2.

# We don't want to set all the variables manually everytime, it costs a lot of code. Then we can turn to
# __init__(self) method = a constructor
# When we create a method within a class, it receives the instance as the first argument automatically
# By convention, we should call the instance, self.
# After self instance, we can specify what argument we want to accept
class Employee:
    def __init__(self, first, last, pay):
        # self.something is similar to emp_1.first = "Corey", that's why self is an instance
        self.first = first
        self.last = last
        self.pay = pay
        self.email = first + "." + last + "@company.com"


# 每次创建一次新object,__init__(self)都会run automatically一次,所以在object括号里不用写self
emp_1 = Employee("Corey", "Schafer", 50000)
emp_2 = Employee("Test", "User", 60000)

print(emp_1.email)
print(emp_2.email)

Output:

Corey.Schafer@company.com
Test.User@company.com

3.

# Create our own method within our class
class Employee:
    def __init__(self, first, last, pay):
        # self.something is similar to emp_1.first = "Corey", that's why self is an instance
        self.first = first
        self.last = last
        self.pay = pay
        self.email = first + "." + last + "@company.com"

    # Each method within a class automatically takes the instance of the first argument, full name无需新定义argument
    def fullname(self):
        # Here we use 'self.' in order to apply fullname() method on every object
        return "{} {}".format(self.first, self.last)    


emp_1 = Employee("Corey", "Schafer", 50000)
emp_2 = Employee("Test", "User", 60000)
print(emp_1.fullname())

Output:

Corey Schafer

Tips:

# If we forget the parentheses ()
print(emp_1.fullname)
# Don't forget to add '()' when calling a method, or you'll get output like
<bound method Employee.fullname of <__main__.Employee object at 0x10470bfa0>>
# It will print the fullname method instead
# We can also use class name to run the method
# print(emp_1.fullname()) can be replaced by
print(Employee.fullname(emp_1))

4.

# If you forget to add 'self' in fullname() method
class Employee:
    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.pay = pay
        self.email = first + "." + last + "@company.com"

    def fullname():
        return "{} {}".format(self.first, self.last)


emp_1 = Employee("Corey", "Schafer", 50000)
emp_2 = Employee("Test", "User", 60000)
print(emp_1.fullname())

Output:

Traceback (most recent call last):
  File "/Users/zhaojingyuan/PycharmProjects/lab/OOP tutorial.py", line 88, in <module>
    print(emp_1.fullname())
TypeError: Employee.fullname() takes 0 positional arguments but 1 was given
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容