-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple_classes.py
More file actions
84 lines (63 loc) · 1.6 KB
/
simple_classes.py
File metadata and controls
84 lines (63 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
81
82
83
84
#!/usr/bin/env python
"""
simple_classes.py
demonstrating the basics of a class
"""
## create a point class
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
## create an instance of that class
p = Point(3,4)
## access the attributes
print "p.x is:", p.x
print "p.y is:", p.y
class Point2(object):
size = 4
color= "red"
def __init__(self, x, y):
self.x = x
self.y = y
p2 = Point2(4,5)
print p2.size
print p2.color
class Point3(object):
size = 4
color= "red"
def __init__(self, x, y):
self.x = x
self.y = y
def get_color(self):
return self.color
p3 = Point3(4,5)
print p3.size
print p3.get_color()
class Circle(object):
color = "red"
def __init__(self, diameter):
self.diameter = diameter
def grow(self, factor=2):
"""
grows the circles diameter
:param factor=2: factor by whioch to gros the circle
"""
self.diameter = self.diameter * factor
def get_area(self):
return math.pi * self.diameter / 2.0
class NewCircle(Circle):
color = "blue"
def grow(self, factor=2):
"""grows the area by factor..."""
self.diameter = self.diameter * math.sqrt(2)
nc = NewCircle
print nc.color
class CircleR(Circle):
def __init__(self. radius):
diameter = radius*2
Circle.__init__(self, diameter)
class CircleR2(Circle):
def __init__(self. radius):
self.radius = radius
def get_area(self):
return Circle.get_area(self, self.radius*2)