Programming Python - 1. Preview -1.3 Stepping up to OOP

OOP: Structure, Encapsulation, Customization

1. Using Classes

Programming Python - 1. Preview -1.3 Stepping up to OOP
class Person:
    def __init__(self, name, age, pay=0, job=None):
        self.name=name
        self.age=age
        self.pay=pay
        self.job=job

if __name__==__main__:
    bob=Person(bob smith, 42, 30000, software)
    sue=Person(sue jones,45, 45000,hareware)
    print(bob.name, sue.pay)

    print(bob.name.split()[-1])
    sue.pay *= 1.1
    print(sue.pay)
Programming Python - 1. Preview -1.3 Stepping up to OOP

2. Adding Behavior

Programming Python - 1. Preview -1.3 Stepping up to OOP

上一篇:Python中的正则表达式


下一篇:Python 学习入门(37)—— @classmethod函数