class ExampleClass:
class_variable = "I am a class variable"
# 构造方法(Constructor)
def __init__(self, instance_variable):
self.instance_variable = instance_variable
# 普通方法(Method)
def instance_method(self):
print(f"Instance method called with value: {self.instance_variable}")
# 类方法(Class Method)
@classmethod
def class_method(cls):
print(f"Class method called with class variable: {cls.class_variable}")
# 静态方法(Static Method)
@staticmethod
def static_method():
print("Static method called")
# 魔术方法(Magic Methods
def __str__(self):
return f"ExampleClass instance with value: {self.instance_variable}"
# 创建类的实例
obj = ExampleClass("example")
# 调用实例方法
obj.instance_method()
# 调用类方法
ExampleClass.class_method()
# 调用静态方法
ExampleClass.static_method()
# 输出字符串表示
print(obj)
Instance method called with value: example
Class method called with class variable: I am a class variable
Static method called
ExampleClass instance with value: example
1、构造方法(Constructor):
__init__
,并且它始终在对象创建后立即调用。2、普通方法(Method):
3、类方法(Class Method):
@classmethod
装饰器修饰的方法。cls
。4、静态方法(Static Method):
@staticmethod
装饰器修饰的方法。5、魔术方法(Magic Methods):
__init__
、__str__
等。__init__
用于对象的初始化,__str__
用于返回对象的字符串表示。具体例子:
class BankAccount:
bank_balance = 0
def __init__(self, account_holder, initial_balance=0):
self.account_holder = account_holder
self.balance = initial_balance
def deposit(self, amount):
self.balance += amount
print(f"{self.account_holder} deposited ${amount}. New balance: ${self.balance}")
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
print(f"{self.account_holder} withdrew ${amount}. New balance: ${self.balance}")
else:
print(f"Insufficient funds for {self.account_holder}. Balance: ${self.balance}")
@classmethod
def total_balance(cls):
print(f"Total bank balance: ${cls.bank_balance}")
@staticmethod
def is_business_hours():
# Assume business hours are from 9 AM to 5 PM
current_hour = 15 # Just an example, should use current time in a real application
return 9 <= current_hour <= 17
def __str__(self):
return f"Account Holder: {self.account_holder}, Balance: ${self.balance}"
# 创建账户实例
account1 = BankAccount("Alice", 1000)
account2 = BankAccount("Bob", 500)
# 存款和取款操作
account1.deposit(200)
account2.withdraw(100)
# 类方法 - 查看总余额
BankAccount.total_balance()
# 静态方法 - 检查是否是营业时间
print(f"Is it business hours? {'Yes' if BankAccount.is_business_hours() else 'No'}")
# 输出账户信息
print(account1)
print(account2)
构造方法 __init__
:
account_holder
(账户持有人)和可选的 initial_balance
(初始余额)作为参数。普通方法 deposit
和 withdraw
:
amount
表示存款或取款的金额。balance
属性,并输出相关信息。类方法 total_balance
:
cls
(类本身)作为第一个参数。静态方法 is_business_hours
:
魔术方法 __str__
:
self
表示对象本身。print
函数调用,返回对象的字符串表示。????????总的来说,这五种方法在类中的作用和使用方式不同。构造方法用于初始化对象,普通方法用于实例级别的操作,类方法用于类级别的操作,静态方法与类和实例无关,而魔术方法用于定义对象的字符串表示。
结果:
# 这里current_hour(假设为15),实际运行时可能会得到不同的输出,具体取决于实际的运行时间。
Alice deposited $200. New balance: $1200
Bob withdrew $100. New balance: $400
Total bank balance: $0
Is it business hours? Yes
Account Holder: Alice, Balance: $1200
Account Holder: Bob, Balance: $400
结果分析:
total_balance
类方法被调用,输出总银行余额为 $0。is_business_hours
静态方法被调用,输出是否为营业时间(在这个例子中,假设是)。