-
Notifications
You must be signed in to change notification settings - Fork 1
/
class_basic
189 lines (140 loc) · 4.41 KB
/
class_basic
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#
class Restaurant_serve():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served=0
def describe_restaurant(self):
print(self.restaurant_name+' with ' + self.cuisine_type)
def open_restaurant(self):
print('Restaurant is openning')
def set_number_served(self,num):
self.number_served = num
print('Number reserved is '+ str(num))
def increment_number_served(self):
self.number_served += 1
#
# new=Restaurant_serve('KFC', 'Fast food')
# new.describe_restaurant()
# new.open_restaurant()
#
# new.set_number_served(500)
# new.increment_number_served()
#
# print("Now, number reserved is " + str(new.number_served))
# print("\n")
class IceCreamStand(Restaurant_serve):
def __init__(self,restaurant_name,cuisine_type,flavors):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.flavors= flavors
class Role(object):
def __init__(self,name):
self.name = name
def attack(self, enemy):
enemy.life_value -= self.power
class People(Role):
power= 10
life_value=100
def __int__(self,name):
super().__int__(name)
class Dog(Role):
power=15
life_value=80
def __int__(self,name):
super().__int__(name)
p1=People('Tom')
p1=People('Niki')
d1=Dog('animal')
print(p1.life_value)
print(d1.power)
d1.attack(p1)
print(p1.life_value)
'''
2个角色,人和狗,游戏开始后,生成2个人,3条狗,互相混战
人被狗咬了会掉血,狗被人打了也掉血,狗和人的攻击力,具备的功能都不一样。
注意,请按题14领域建模的方式来设计类。
'''
def show_flavors(self):
print('the flavors are:')
for flavor in self.flavors:
print('\t'+flavor)
good_ice = IceCreamStand('dadice','ice',['sweet','bitter'])
good_ice.show_flavors()
print("\n")
# class IceCreamStand():
# def __init__(self,restaurant_name,cuisine_type,flavors):
# super().__init__(restaurant_name, cuisine_type) ## super().__int__() heritage
# self.flavors=flavors
#
# def show_flavors(self):
# print('the flavors are:')
# for flavor in self.flavors:
# print('\t'+flavor)
#
# good_ice = IceCreamStand('dadice','ice',['sweet','bitter'])
# good_ice.show_flavors()
# print("\n")
from random import randint
class Die():
def __init__(self, side):
self.side = side
def roll_die(self):
x = randint(1, self.side)
print(" " + str(x))
for side in [6, 10, 20]:
die = Die(side)
print("Side: " + str(side))
for i in range(1, 11):
die.roll_die()
import ssl
from collections import Counter
ssl._create_default_https_context = ssl._create_unverified_context
class Read():
def __init__(self,url):
self.url = url
self.df=pd.read_csv(url,sep='\\t',engine='python')
def hd(self):
return self.df.head()
def db(self):
print(self.df.describe())
def info(self):
print(self.df.info())
def null(self):
print(self.df.isnull().sum())
def drop(self):
self.df=self.df.dropna()
class Person:
def name(self, name):
self.name = name
return self
def age(self, age):
self.age = age
return self
def show(self):
print("My name is", self.name, "and I am", self.age, "years old.")
p = Person()
p.name("Li Lei").age(15).show()
class Restaurant_serve():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served=0
def show_all(self):
print('the food we have in geneva are:')
for name, food in self.restaurant_name,self.cuisine_type:
print(name + ' offers ' + food)
class IceCreamStand(Restaurant_serve):
def __init__(self,restaurant_name,cuisine_type,flavors):
# self.restaurant_name = restaurant_name
# self.cuisine_type = cuisine_type
self.flavors= flavors
def show_flavors(self):
print('the flavors are:')
for flavor in self.flavors:
print('\t'+flavor)
good_ice = IceCreamStand('dadice','ice',['sweet','bitter'])
good_ice.show_flavors()
print("\n")
geneva_food=Restaurant_serve(['KFC','Junk food'],['JiaWei','Chinese food'])
geneva_food.show_all()