Skip to main content
Version: python

创建

# 默认: Employee(object)
class Employee:
empCount = 0

# 构造方法 self 代表类的实例
def __init__(self, name):
self.name = name

def displayCount(self):
print(self.empCount)

赋值

e = Employee("小红")
# 调用属性, 方法
e.empCount = 10
e.displayCount()

继承

class Child(Employee): # 定义子类
name = "zi"

def __init__(self):
print("call Child __init__()")

# 方法重写
def displayCount(self):
print(self.name)

c = Child() # 子类实例
c.displayCount() # 子类调用重写方法