python类和对象初识

# python类和对象初识
a = 2
b = 3
print(id(a))
print(type(a))
print(a)

print(id(b))
print(type(b))
print(b)

a = 2
b = 2
print(id(a))
print(type(a))
print(a)

print(id(b))
print(type(b))
print(b)

'''
打印结果: 
1518913232
<class 'int'>
2
1518913264
<class 'int'>
3
1518913232
<class 'int'>
2
1518913232
<class 'int'>
2              2是一个int class,a,b是标签,对象。
'''

##创建类
class Account:
    #构造函数 个人账户
    def __init__(self,balance,name):
        self.balance = balance
        self.name = name

    #方法 收入
    def credit(self,deposit):
        self.balance = self.balance + deposit

    # 方法 支出
    def debit(self, withdrawal):
        self.balance = self.balance - withdrawal

##开户
customer = Account(0,"Steve")
print(customer.name,"有存款",customer.balance)   # TypeError: must be str, not int 使用逗号分隔的方法来处理。
##存款100
customer.credit(100)
print(customer.name,"有存款",customer.balance)
##提现20
customer.debit(20)
print(customer.name,"有存款",customer.balance)
## 变更账户名称
customer.name = "Bruce"
print(customer.name,"有存款",customer.balance)
原文地址:https://www.cnblogs.com/oktokeep/p/15691767.html