隨機整數生成類,可以指定一批生成的個數,可以指定數值的範圍,可以調整每批生成數字的個數
使用這個類,隨機生成20個數字,兩兩配對形成二維座標系的座標,把這些座標組織起來,並列印輸出
一:
import random
class randnum:
def __init__(self, rmin=0, rmax=100, num=10):
self.min = rmin
self.max = rmax
self.num = num
def creat(self):
return [random.randint(self.min, self.max) for x in range(self.num)]
test1 = randnum()
print(test1.creat())
out:
[30, 18, 46, 63, 41, 31, 49, 93, 26, 9]
二:
import random
class randnum:
@classmethod
def creat(cls, minnum=0, maxnum=100, num=10):
return [random.randint(minnum, maxnum) for x in range(num)]
print(randnum.creat())
out:
[44, 82, 12, 48, 54, 36, 49, 98, 88, 96]
三:
import random
class randomgenerator:
def __init__(self, count=10, start=1, stop=100):
self.count = count
self.start = start
self.stop = stop
self.gen = self._generate()
def _generate(self):
while true:
yield random.randint(self.start, self.stop)
def generate(self):
return [next(self.gen) for _ in range(self.count)]
rg = randomgenerator(5)
print(rg.generate())
out:
[27, 79, 87, 29, 30]
完整:
import random
class randomgenerator:
def __init__(self, count=10, start=1, stop=100):
self.count = count
self.start = start
self.stop = stop
self.gen = self._generate()
def _generate(self):
while true:
yield [random.randint(self.start, self.stop) for _ in range(self.count)]
def generate(self, count):
self.count = count
return next(self.gen)
class point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return '{}:{}'.format(self.x, self.y)
rg = randomgenerator()
lst = [point(x, y) for x, y in zip(rg.generate(10), rg.generate(10))]
# lst = [point(*v) for v in zip(rg.generate(10), rg.generate(10))]
print(lst)
out:
[45:36, 1:36, 58:10, 58:20, 75:79, 12:66, 27:68, 98:26, 27:76, 100:67]
記錄車的品牌mark,顏色,**,速度等特徵,並實現增加車輛資訊,顯示全部車輛資訊的功能
class car:
def __init__(self, mark, color, price, speed):
self.mark = mark
self.color = color
self.price = price
self.speed = speed
class carinfo:
def __init__(self):
self.lst =
def addcar(self, car: car):
def getall(self):
return self.lst[0].mark, self.lst[0].color, self.lst[0].price, self.lst[0].speed
ci = carinfo()
car = car('audi', '400', 'red', 100)
ci.addcar(car)
print(ci.getall())
out:
('audi', '400', 'red', 100)
實現溫度的處理
實現華氏溫度和攝氏溫度的轉換
℃ = 5 * (℉ - 32) / 9
℉ = 9 * ℃ / 5 + 32
模擬購物車
class color:
red = 0
blue = 1
green = 2
golden = 3
black = 4
other = 1000
class item:
def __init__(self, **kwargs):
self.__spec = kwargs
def __repr__(self):
return str(sorted(self.__spec.items()))
class cart:
def __init__(self):
self.items =
def additem(self, item:item):
def getallitems(self):
return self.items
mycart = cart()
myphone = item(mark='huawei', color=color.golden, memory='5g')
mycart.additem(myphone)
print(mycart.getallitems())
myphone = item(mark='oppo', color=color.black, memory='5g')
mycart.additem(myphone)
print(mycart.getallitems())
out:
[[('color', 3), ('mark', 'huawei'), ('memory', '5g')]]
[[('color', 3), ('mark', 'huawei'), ('memory', '5g')], [('color', 4), ('mark', 'oppo'), ('memory', '5g')]]
物件導向(基礎題目 射擊)
package com.svse.seven2 1 編寫乙個槍類,有乙個成員變數用於儲存彈夾中的子彈,有乙個成員方法用於發射,每呼叫一次發射功能,彈夾中的子彈數減少1。2 修改上體,增強發射功能。呼叫發射功能時首先判斷彈夾中還有子彈麼,如果有,在螢幕上顯示 呯!並將彈夾中的子彈量減少1 如果沒子彈量...
物件導向(一)
當new去呼叫乙個函式 函式中的this就是建立出來的物件,並且函式的返回值就是this 隱式返回 function createperson name var p1 new createperson 張三 p1.showname var p2 new createperson 李四 p2.show...
物件導向(一)
oop程式設計是利用 類 和 物件 來建立各種模型來實現對真實世界的描述,使用物件導向程式設計的原因一方面是因為它可以使程式的維護和擴充套件變得更簡單,並且可以大大提高程式開發效率 另外,基於物件導向的程式可以使它人更加容易理解你的 邏輯,從而使團隊開發變得更從容 class foo 定義乙個類,c...