class User(object): def __init__(self, username, password): self.username = username self.password = password def login(self, uname, pwd): if self.username == uname and self.password == pwd: return True else: return False u1 = User("嘗試了就好", "123") ret = u1.login(input("username:").strip(), input("password:").strip()) print(ret)
xxx = 類名()編程
class Animal(object): def run(self): print("----run----") class Cat(Animal): pass cat = Cat() cat.run() # ----run----
class Animal(object): def run(self): print("Animal is running...") class Dog(Animal): def run(self): print("Dog is running...") class Cat(Animal): def run(self): print("Cat is running...") class Observer(): def observe(self, animal): animal.run() dg = Dog() ct = Cat() ob = Observer() ob.observe(dg) ob.observe(ct) 結果: Dog is running... Cat is running...