Skip to content

Commit 1ca447e

Browse files
committed
class examples added 📝
1 parent 9463614 commit 1ca447e

File tree

4 files changed

+235
-0
lines changed

4 files changed

+235
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Dog:
2+
species = 'canine'
3+
4+
def __init__(self, color, name, breed):
5+
self.color = color
6+
self.name = name
7+
self.breed = breed
8+
self.steps = 0
9+
10+
def bark(self):
11+
return f"woof my name is {self.name}"
12+
13+
def walk(self):
14+
self.steps += 5000
15+
16+
def energy(self):
17+
if self.steps > 10000:
18+
return "sleepy"
19+
else:
20+
return "pumped"
21+
22+
class HighEnergyDog(Dog):
23+
def __init__(self, color, name):
24+
super().__init__(color, name)
25+
self.breed = 'ridgeback'
26+
27+
def energy(self):
28+
if self.steps > 10000000:
29+
return "sleepy"
30+
else:
31+
return "pumped"
32+
33+
roofus = HighEnergyDog('brown', 'roofus')
34+
35+
print(roofus.steps)
36+
roofus.walk()
37+
print(roofus.steps)
38+
print(roofus.energy())
39+
roofus.walk()
40+
roofus.walk()
41+
roofus.walk()
42+
roofus.walk()
43+
roofus.walk()
44+
roofus.walk()
45+
print(roofus.steps)
46+
print(roofus.energy())
47+
48+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# methods:
2+
# -drinks should be able to be sipped
3+
# -drinks should be able to be refilled
4+
5+
# DRY
6+
# Don't Repeat Yourself
7+
8+
9+
class Drink:
10+
def __init__(self, owner, cup_volume):
11+
self.volume = 0
12+
self.cup_volume = cup_volume
13+
self.owner = owner
14+
15+
def whose_drink(self):
16+
return f'this drink belongs to {self.owner}'
17+
18+
def sip_drink(self, size):
19+
# lower drink's volume by some small amount
20+
if size == 'gulp':
21+
self.volume -= 2.5
22+
elif size == 'chug':
23+
self.volume -= self.volume
24+
else:
25+
self.volume -= .1
26+
return self.volume
27+
28+
def refill(self):
29+
self.volume += self.cup_volume
30+
31+
32+
class Seltzer(Drink):
33+
def __init__(self, owner, cup_volume, flavor):
34+
super().__init__(owner, cup_volume)
35+
self.flavor = flavor
36+
37+
seltzie = Seltzer('sarah', 12, 'apple')
38+
print(seltzie)
39+
40+
# drink = Drink(12, 'Zach')
41+
# print(drink.volume) #0
42+
# drink.refill()
43+
# print(drink.volume) #12
44+
45+
46+
# drank = Drink(24, 'Ozz')
47+
# print(drink.whose_drink())
48+
# print(drink.sip_drink('chug'))
49+
50+
# while True:
51+
# order = input('What would you like to drink? ')
52+
# name = input('whats your name? ')
53+
# size = float(input('what size cup do you want? Enter ounces in number: '))
54+
55+
# drink = Drink(name, size)
56+
# drink.refill()
57+
# print(f'Here is your {order}')
58+
59+
60+
# create a class SpecificTypeOfDrink(class choice)
61+
# should inherit from class Drink
62+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
number = 0 #does not influence 'number' inside class
2+
3+
class Distance:
4+
5+
def __init__(self, number, unit):
6+
self.number = number
7+
self.unit = unit
8+
9+
def to_meters(self):
10+
if self.unit == 'm': # meters
11+
return self.number
12+
elif self.unit == 'miles': # miles
13+
return self.number * 1609.34
14+
elif self.unit == 'ft': # feet
15+
return self.number * 0.3048
16+
elif self.unit == 'km': # kilometers
17+
return self.number * 1000
18+
elif self.unit == 'yd': # yards
19+
return self.number * 0.9144
20+
elif self.unit == 'in': # inches
21+
return self.number * 0.0254
22+
23+
to_portland = Distance(3000, 'miles')
24+
25+
to_portland.number = 10
26+
27+
to_nyc = Distance(200, 'miles')
28+
print('first', to_nyc.number)
29+
to_nyc.number = 100
30+
print('second', to_nyc.number)
31+
32+
print(to_portland.unit)
33+
print(to_portland.number)
34+
print(to_portland.to_meters())
35+
print(to_portland)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
class User:
2+
species = 'human'
3+
def __init__(self, username, email):
4+
self.username = username
5+
self.email = email
6+
def __str__(self):
7+
return self.username
8+
9+
class SuperUser(User):
10+
def __init__(self, username, email):
11+
super().__init__(username, email)
12+
13+
14+
15+
bruce = SuperUser('criticbruce', '[email protected]')
16+
print(bruce.species)
17+
Kacey = User('demo', '[email protected]')
18+
print(Kacey.species)
19+
print(type(bruce))
20+
print(bruce)
21+
22+
23+
24+
# class Circle:
25+
# pi = 3.14
26+
# def __init__(self, radius):
27+
# self.radius = radius
28+
29+
# @staticmethod
30+
# def circumference(self, radius):
31+
# c = 2 * self.pi * radius
32+
# return Circle(c)
33+
34+
# egg = Circle(6)
35+
# print(egg.circumference(egg.radius))
36+
37+
38+
# def distance(p1, p2):
39+
# dx = p1['x'] - p2['x']
40+
# dy = p1['y'] - p2['y']
41+
# return math.sqrt(dx*dx + dy*dy)
42+
43+
# p1 = {'x': 5, 'y': 2}
44+
# p2 = {'x': 8, 'y': 4}
45+
# print(distance(p1, p2))
46+
47+
class Point:
48+
def __init__(self, x, y): # this is the initializer
49+
self.x = x # these are member variables
50+
self.y = y
51+
52+
def distance(self, p): # method, or 'member function'
53+
dx = self.x - p.x
54+
dy = self.y - p.y
55+
return math.sqrt(dx*dx + dy*dy)
56+
57+
# p1 = Point(5, 2) # call the initializer, instantiate the class
58+
59+
# p1 = (5,2)
60+
# p2 = Point(8, 4)
61+
62+
# # print(p1.x) # 5
63+
# # print(p1.y) # 2
64+
# print(type(p1)) # Point
65+
# print(p1.distance(p2))
66+
67+
68+
69+
70+
71+
import math
72+
73+
class Point:
74+
def __init__(self, x=0, y=0):
75+
self.x = x
76+
self.y = y
77+
78+
def distance(self, p): # method, or 'member function'
79+
dx = self.x - p.x
80+
dy = self.y - p.y
81+
return math.sqrt(dx*dx + dy*dy)
82+
83+
def scale(self, v):
84+
self.x *= v
85+
self.y *= v
86+
87+
# p3 = Point()
88+
# p2 = Point(8,4)
89+
# dist = p1.distance(p2) # or p2.distance(p1), either works
90+
# print(dist)

0 commit comments

Comments
 (0)