- Classe
- `1. 类的方法`
- 1.1 `__init__()`
- 1.2 `__str__()`
- `2. 类变量和实例变量`
- `3. 只读变量`
- `3. 继承`
class MyFirstClass:
def __init__(self, name):
self.name = name
def greet(self):
print('Hello {}!'.format(self.name))
my_instance = MyFirstClass('John Doe')
print('my_instance: {}'.format(my_instance)) # my_instance: <__main__.MyFirstClass object at 0x00000215B3B3B610>
print('type: {}'.format(type(my_instance))) # type:
print('my_instance.name: {}'.format(my_instance.name)) # my_instance.name: John Doe
1. 类的方法
alice = MyFirstClass(name='Alice') alice.greet() # Hello Alice!1.1 __init__()
- 创建类的时候同时调用
class Example:
def __init__(self):
print('Now we are inside __init__')
example = Example() # Now we are inside __init__
- 在这里定义类中的参数可用self.参数在类中的其它方法调用参数
class Example:
def __init__(self, var1, var2):
self.first_var = var1
self.second_var = var2
def print_variables(self):
print('{} {}'.format(self.first_var, self.second_var))
e = Example('abc', 123)
e.print_variables() # abc 123
1.2 __str__()
_str__()是一种特殊方法,当类的实例转换为字符串时(例如,打印实例时),会调用它。换句话说,通过为类定义__str__方法,可以确定类实例的可打印版本。该方法返回一个字符串。
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return 'Person: {}'.format(self.name)
jack = Person('Jack', 82)
print('This is the string presentation of jack: {}'.format(jack))
# This is the string presentation of jack: Person: Jack
2. 类变量和实例变量
- 类变量通过类名.变量名调用(不限于类的内部调用)
- 在类的参数传时,实例变量通过self.变量名调用
class Example:
# 类变量
name = 'Example class'
description = 'Just an example of a simple class'
def __init__(self, var1):
# 实例变量
self.instance_variable = var1
def show_info(self):
info = 'instance_variable: {}, name: {}, description: {}'.format(
self.instance_variable, Example.name, Example.description)
print(info)
inst1 = Example('foo')
inst2 = Example('bar')
# name 和 description 是相等的
assert inst1.name == inst2.name == Example.name
assert inst1.description == inst2.description == Example.description
# 类变量的改变会修改所有实例的对应变量值
Example.name = 'Modified name'
inst1.show_info()
inst2.show_info()
# instance_variable: foo, name: Modified name, description: Just an example of a simple class
# instance_variable: bar, name: Modified name, description: Just an example of a simple class
3. 只读变量
- 如果想要类中的方法或变量不能从外部直接访问或修改,则在对应变量名前加上下划线
- 如果想在外部查看而不修改变量名,则可以定义变量名的方法+property装饰器
class Person:
def __init__(self, age):
self._age = age
@property
def age(self):
return self._age
example_person = Person(age=15)
print(example_person.age) # 12
example_person.age = 16 # AttributeError
3. 继承
- 子类继承父类的属性和方法
- 子类可以重写父类方法或定义新方法
class Animal:
def greet(self):
print('Hello, I am an animal')
@property
def favorite_food(self):
return 'beef'
class Dog(Animal):
def greet(self):
print('wof wof')
class Cat(Animal):
@property
def favorite_food(self):
return 'fish'
dog = Dog()
dog.greet() # wof wof
print("Dog's favorite food is {}".format(dog.favorite_food))
# Dog's favorite food is beef
cat = Cat()
cat.greet() # Hello, I am an animal
print("Cat's favorite food is {}".format(cat.favorite_food))
# Cat's favorite food is fish



