Skip to content

Commit 0ce9ce4

Browse files
committed
add oop basic samples
1 parent 34865c0 commit 0ce9ce4

5 files changed

Lines changed: 136 additions & 0 deletions

File tree

py3/oop_basic/animals.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
class Animal(object):
5+
def run(self):
6+
print('Animal is running...')
7+
8+
class Dog(Animal):
9+
def run(self):
10+
print('Dog is running...')
11+
12+
class Cat(Animal):
13+
def run(self):
14+
print('Cat is running...')
15+
16+
def run_twice(animal):
17+
animal.run()
18+
animal.run()
19+
20+
a = Animal()
21+
d = Dog()
22+
c = Cat()
23+
24+
print('a is Animal?', isinstance(a, Animal))
25+
print('a is Dog?', isinstance(a, Dog))
26+
print('a is Cat?', isinstance(a, Cat))
27+
28+
print('d is Animal?', isinstance(d, Animal))
29+
print('d is Dog?', isinstance(d, Dog))
30+
print('d is Cat?', isinstance(d, Cat))
31+
32+
run_twice(c)

py3/oop_basic/attrs.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
class MyObject(object):
5+
6+
def __init__(self):
7+
self.x = 9
8+
9+
def power(self):
10+
return self.x * self.x
11+
12+
obj = MyObject()
13+
14+
print('hasattr(obj, \'x\') =', hasattr(obj, 'x')) # 有属性'x'吗?
15+
print('hasattr(obj, \'y\') =', hasattr(obj, 'y')) # 有属性'y'吗?
16+
setattr(obj, 'y', 19) # 设置一个属性'y'
17+
print('hasattr(obj, \'y\') =', hasattr(obj, 'y')) # 有属性'y'吗?
18+
print('getattr(obj, \'y\') =', getattr(obj, 'y')) # 获取属性'y'
19+
print('obj.y =', obj.y) # 获取属性'y'
20+
21+
print('getattr(obj, \'z\') =',getattr(obj, 'z', 404)) # 获取属性'z',如果不存在,返回默认值404
22+
23+
f = getattr(obj, 'power') # 获取属性'power'
24+
print(f)
25+
print(f())

py3/oop_basic/get_instance.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
class Animal(object):
5+
pass
6+
7+
class Dog(Animal):
8+
pass
9+
10+
class Husky(Dog):
11+
pass
12+
13+
a = Animal()
14+
d = Dog()
15+
h = Husky()
16+
17+
print('check a = Animal()...')
18+
print('isinstance(a, Animal) =', isinstance(a, Animal))
19+
print('isinstance(a, Dog) =', isinstance(a, Dog))
20+
print('isinstance(a, Husky) =', isinstance(a, Husky))
21+
22+
print('check d = Dog()...')
23+
print('isinstance(d, Animal) =', isinstance(d, Animal))
24+
print('isinstance(d, Dog) =', isinstance(d, Dog))
25+
print('isinstance(d, Husky) =', isinstance(d, Husky))
26+
27+
print('check h = Husky()...')
28+
print('isinstance(h, Animal) =', isinstance(h, Animal))
29+
print('isinstance(h, Dog) =', isinstance(h, Dog))
30+
print('isinstance(h, Husky) =', isinstance(h, Husky))

py3/oop_basic/get_type.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
# type()
5+
6+
print('type(123) =', type(123))
7+
print('type(\'123\') =', type('123'))
8+
print('type(None) =', type(None))
9+
print('type(abs) =', type(abs))
10+
11+
import types
12+
13+
print('type(\'abc\')==str?', type('abc')==str)
14+

py3/oop_basic/protected_student.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
class Student(object):
5+
6+
def __init__(self, name, score):
7+
self.__name = name
8+
self.__score = score
9+
10+
def get_name(self):
11+
return self.__name
12+
13+
def get_score(self):
14+
return self.__score
15+
16+
def set_score(self, score):
17+
if 0 <= score <= 100:
18+
self.__score = score
19+
else:
20+
raise ValueError('bad score')
21+
22+
def get_grade(self):
23+
if self.__score >= 90:
24+
return 'A'
25+
elif self.__score >= 60:
26+
return 'B'
27+
else:
28+
return 'C'
29+
30+
bart = Student('Bart Simpson', 59)
31+
print('bart.get_name() =', bart.get_name())
32+
bart.set_score(60)
33+
print('bart.get_score() =', bart.get_score())
34+
35+
print('DO NOT use bart._Student__name:', bart._Student__name)

0 commit comments

Comments
 (0)