文章

Python练习-面像对象基础

本部分练习Python面像对象编程基础知识。练习代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
python面像对象学习相关代码

import types

from demo.module.module import Student
import demo.module.module as im_module

# 对象类型
print(type(123))
print(type("123"))
print(type(Student("Object", 99)))
print(type(im_module.stu1))

print(type(abs) == types.BuiltinFunctionType)
print(type((x for x in range(10))) == types.GeneratorType)

print(isinstance("abc", str))
print(isinstance([1, 2, 3], (list, tuple)))

print(dir(Student))
print(dir("123"))
print(hasattr(im_module.stu1, "__init__"))
print(hasattr(im_module.stu1, "__name"))
getattr(im_module.stu1, "print_score")()


class Fruit():
    category = "one"

    def __init__(self, name):
        self.name = name


apple = Fruit("Apple")
pear = Fruit("pear")
print(getattr(apple, "name"))
pear.category = "two"
print(getattr(apple, "category"))
print(getattr(pear, "name"))
print(getattr(pear, "category"))


# 练习
# 为了统计学生人数,可以给Student类增加一个类属性,每创建一个实例,该属性自动增加:

class Student(object):
    count = 0

    def __init__(self, name):
        self.name = name
        Student.count += 1

# 测试:
if Student.count != 0:
    print('测试失败!')
else:
    bart = Student('Bart')
    if Student.count != 1:
        print('测试失败!')
    else:
        lisa = Student('Bart')
        if Student.count != 2:
            print('测试失败!')
        else:
            print('Students:', Student.count)
            print('测试通过!')
本文由作者按照 CC BY 4.0 进行授权