-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass1.py
More file actions
80 lines (62 loc) · 1.6 KB
/
class1.py
File metadata and controls
80 lines (62 loc) · 1.6 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : class1.py
# @Author : Feng
# @Date : 2017/2/15
import types
import logging
class Student:
def __init__(self, name, score):
self.__name = name
self.__score = score
def __str__(self):
return 'Student object (name: %s)' % self.__name
def print_score(self):
print '%s: %s' % (self.__name, self.__score)
def get_grade(self):
if self.__score >= 90:
return 'A'
elif self.__score >= 60:
return 'B'
else:
return 'C'
li=Student('li',80)
feng=Student('feng',85)
test=Student('test', 999)
class Animal:
def run(self):
print 'animal'
def run_twice(self):
self.run()
self.run()
class Dog(Animal):
def run(self):
print 'dog'
class Cat(Animal):
def run(self):
print 'cat'
a=Animal()
d=Dog()
class MyObject(object):
def __init__(self):
self.x = 9
def power(self):
return self.x * self.x
class Fib(object):
def __init__(self):
self.a, self.b = 0, 1 # 初始化两个计数器a,b
def __iter__(self):
return self # 实例本身就是迭代对象,故返回自己
def next(self):
self.a, self.b = self.b, self.a + self.b # 计算下一个值
if self.a > 100000: # 退出循环的条件
raise StopIteration();
return self.a # 返回下一个值
def __getitem__(self, n):
a, b = 1,1
for x in range(n):
a, b = b, a+b
return a
f=Fib()
logging.basicConfig(level=logging.INFO)
print f[5]