例如:
class Dog: def __init__(self, name, color): self.name = name self.color = color def bark(self): print("Woof!") fido = Dog("Fido", "brown") print(fido.name) fido.bark()
结果:
>>> Fido Woof! >>>
例如:
class Dog: legs = 4 def __init__(self, name, color): self.name = name self.color = color fido = Dog("Fido", "brown") print(fido.legs) print(Dog.legs)
结果:
>>> 4 4 >>>