[course]08 —— 面向对象编程03
1. Type Testing (type, isinstance)
class A(object): pass
a = A()
print(type(a)) # A (technically, < class '__main__.A' >)
print(type(a) == A) # True
print(isinstance(a, A)) # True2. Special Methods
1. Equality Testing (eq)
1.The problem:** Shouldn't a1 == a2?
a1 == a2?class A(object):
def __init__(self, x):
self.x = x
a1 = A(5)
a2 = A(5)
print(a1 == a2) # False!2. The partial solution: eq
3.A better solution:
2. Converting to Strings (str and repr)
1. The problem:
2. The partial solution: str
3. The better solution: repr
3. Using in Sets and Dictionaries (hash and eq)
4. Fraction Example
3. Class-Level Features
1. Class Attributes
2. Static Methods
3. Playing Card Demo
4. Inheritance
1. Specifying a Superclass
2. Overriding methods
3. isinstance vs type in inherited classes
4. Monster Demo
5. Additional Reading
Last updated