python基础升级第四节


练习1:创建一个名为phone的类

class Phone():
phone_total = 0
phone_list=[]






def __init__(self,screeen_size, price, brand):
self.screeen_size = screeen_size
self.price = price
self.brand = brand




Phone.phone_total +=1
Phone.phone_list.append(brand)






def play(self):
print('paly game')
return 'paly game'


def sendMessage(self):
print('text messagee')
return 'text message'
def powerOff(self):
print('power off')
return 'power off'


def get_info(self,):
print('品牌',self.brand)
print('尺寸',self.screeen_size)
print('价格',self.price)
return 'get_info'







phone1=Phone(4.7,5000,'xphone6')
phone2=Phone(5.5,8000,'xphone7')

phone1.play()
phone1.sendMessage()
phone1.powerOff()
phone1.get_info()

print('--'*8)
phone2.play()
phone2.sendMessage()
phone2.powerOff()
phone2.get_info()

练习2:设计一个公司类,完成以下要求,并实例化不同对象进行验证
class Company():
company_total = 0
company_list=[]



def __init__(self, name, info, profit, sales, cost):
self.__name = name
self.__info = info
self.__profit = profit #利润
self.__sales = sales #销售额
self.__cost = cost #成本
self.__employee = []
self.__employee_count = 0



Company.company_total +=1
Company.company_list.append(name)


def hire(self, employee_name, cost):
self.__employee.append(employee_name)
self.__employee_count += 1
self.__cost += cost


def fire(self, employee_name,cost):
self.__employee.remove(employee_name)
self.__employee_count -= 1
self.__cost -= cost


def ad(self, cost):
self.__cost += cost


def paySocialInsurance(self, cost):
self.__cost += cost * self.__employee_count


def payTax(self, tax):
self.__cost += tax * self.__employee_count


def sell(self, product_count, product_price, profit_rate):
totalSale = product_count * product_price
self.__sales += totalSale
self.__profit += totalSale * profit_rate




def getEmployeeList(self,):
return self.__employee


def getProfit(self,):
return self.__profit - self.__cost


company1 = Company('DaMi','Mobile Internet technology company',0,0,0)

company1.hire('A1',500000)
company1.hire('B1',500000)
company1.hire('C1',900000)
company1.hire('D1',900000)
company1.hire('E1',800000)

company1.fire('E1',800000)

company1.ad(1000000)
company1.paySocialInsurance(2000000)
company1.payTax(3000000)
company1.sell(100000, 10000, 0.1)
company1.getEmployeeList()
company1.getProfit()

练习三:设计一个叫cinema的电影院的类
创建一个miniCinema迷你电影院的类,继承自cinema类
class Cinema():
cinema_total = 0
cinema_totalSales = 0
cinema_list=[]



def __init__(self, name, location,sales):
self.__name = name
self.__location = location
self._sales = sales #销售额


Cinema.cinema_total +=1
Cinema.cinema_totalSales += sales
Cinema.cinema_list.append(name)


def saleTickets(self, sales):
self._sales += sales #销售额
Cinema.cinema_totalSales += sales


@classmethod
def getSales(cls,):
print('所有电影院的销售额为:', Cinema.cinema_totalSales)
return Cinema.cinema_totalSales



@classmethod
def get_cinema_list(cls,):
print('电影院数目:',Cinema.cinema_total)

print('电影院名字:',Cinema.cinema_list)
print('电影院销售总和:',Cinema.cinema_totalSales)
return Cinema.cinema_list



cinema1 = Cinema('111','A1',0)

cinema2 = Cinema('222','A2',0)
cinema3 = Cinema('333','A3',0)
cinema1.saleTickets(1000000)
cinema2.saleTickets(3000000)
cinema3.saleTickets(5000000)

Cinema.getSales()
print('--'*8)
Cinema.get_cinema_list()

class miniCinema(Cinema):
def saleTickets(self, sales):



if(sales>100):
sales *= 0.9
self._sales += sales
Cinema.cinema_totalSales += sales




miniCinema1=miniCinema('444','A4',0)
miniCinema1.saleTickets(1000000)

Cinema.getSales()
print('--'*8)
Cinema.get_cinema_list()

练习四:导入包完成遍历目录

练习1:创建一个名为phone的类

class Phone():
phone_total = 0
phone_list=[]






def __init__(self,screeen_size, price, brand):
self.screeen_size = screeen_size
self.price = price
self.brand = brand




Phone.phone_total +=1
Phone.phone_list.append(brand)






def play(self):
print('paly game')
return 'paly game'


def sendMessage(self):
print('text messagee')
return 'text message'
def powerOff(self):
print('power off')
return 'power off'


def get_info(self,):
print('品牌',self.brand)
print('尺寸',self.screeen_size)
print('价格',self.price)
return 'get_info'







phone1=Phone(4.7,5000,'xphone6')
phone2=Phone(5.5,8000,'xphone7')

phone1.play()
phone1.sendMessage()
phone1.powerOff()
phone1.get_info()

print('--'*8)
phone2.play()
phone2.sendMessage()
phone2.powerOff()
phone2.get_info()

练习2:设计一个公司类,完成以下要求,并实例化不同对象进行验证
class Company():
company_total = 0
company_list=[]



def __init__(self, name, info, profit, sales, cost):
self.__name = name
self.__info = info
self.__profit = profit #利润
self.__sales = sales #销售额
self.__cost = cost #成本
self.__employee = []
self.__employee_count = 0



Company.company_total +=1
Company.company_list.append(name)


def hire(self, employee_name, cost):
self.__employee.append(employee_name)
self.__employee_count += 1
self.__cost += cost


def fire(self, employee_name,cost):
self.__employee.remove(employee_name)
self.__employee_count -= 1
self.__cost -= cost


def ad(self, cost):
self.__cost += cost


def paySocialInsurance(self, cost):
self.__cost += cost * self.__employee_count


def payTax(self, tax):
self.__cost += tax * self.__employee_count


def sell(self, product_count, product_price, profit_rate):
totalSale = product_count * product_price
self.__sales += totalSale
self.__profit += totalSale * profit_rate




def getEmployeeList(self,):
return self.__employee


def getProfit(self,):
return self.__profit - self.__cost


company1 = Company('DaMi','Mobile Internet technology company',0,0,0)

company1.hire('A1',500000)
company1.hire('B1',500000)
company1.hire('C1',900000)
company1.hire('D1',900000)
company1.hire('E1',800000)

company1.fire('E1',800000)

company1.ad(1000000)
company1.paySocialInsurance(2000000)
company1.payTax(3000000)
company1.sell(100000, 10000, 0.1)
company1.getEmployeeList()
company1.getProfit()

练习三:设计一个叫cinema的电影院的类
创建一个miniCinema迷你电影院的类,继承自cinema类
class Cinema():
cinema_total = 0
cinema_totalSales = 0
cinema_list=[]



def __init__(self, name, location,sales):
self.__name = name
self.__location = location
self._sales = sales #销售额


Cinema.cinema_total +=1
Cinema.cinema_totalSales += sales
Cinema.cinema_list.append(name)


def saleTickets(self, sales):
self._sales += sales #销售额
Cinema.cinema_totalSales += sales


@classmethod
def getSales(cls,):
print('所有电影院的销售额为:', Cinema.cinema_totalSales)
return Cinema.cinema_totalSales



@classmethod
def get_cinema_list(cls,):
print('电影院数目:',Cinema.cinema_total)

print('电影院名字:',Cinema.cinema_list)
print('电影院销售总和:',Cinema.cinema_totalSales)
return Cinema.cinema_list



cinema1 = Cinema('111','A1',0)

cinema2 = Cinema('222','A2',0)
cinema3 = Cinema('333','A3',0)
cinema1.saleTickets(1000000)
cinema2.saleTickets(3000000)
cinema3.saleTickets(5000000)

Cinema.getSales()
print('--'*8)
Cinema.get_cinema_list()

class miniCinema(Cinema):
def saleTickets(self, sales):



if(sales>100):
sales *= 0.9
self._sales += sales
Cinema.cinema_totalSales += sales




miniCinema1=miniCinema('444','A4',0)
miniCinema1.saleTickets(1000000)

Cinema.getSales()
print('--'*8)
Cinema.get_cinema_list()

练习四:导入包完成遍历目录

看别人代码

import os
def openCalal(path):
listDir = os.listdir(path)
for List in listDir:
pathname = os.path.join(path,List)
if os.path.isfile(pathname):
print('文件名为:',List)
print('文件路径为:',pathname)
else: openCalal(pathname)

path = 'e:/'
openCalal(path)

练习五:练习使用PIL包

看别人代码

from PIL import Image

im=Image.open('e:/1.jpg')

im.show() #彩色的图片
form = im.format
print('Size = ',im.size) # (500,362)
out = im.resize((128,128))
print('Size1 = ',out.size) #(128,128)
new_im = im.convert('L')
new_im.show() # 灰度图片
new_im.save('e:/2.jpg')
dir(Image)
help(Image.open)

作业 1:新建类并修改其实例的切片访问

看别人代码

class Newcut():
def __init__(self, value=None):
if value == None:
self.value = []
else:
self.value = value

def __getitem__(self, key):
if type(key) == slice:
start = key.start
stop = key.stop + 1
if key.start == None:
start = 0
if key.stop == None:
stop = -1
return self.value[start:stop]
else:
return str(self.value[key])

def __iter__(self):
return iter(self.value)

c1 = Newcut([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(c1[2:7])
已邀请:

要回复问题请先登录注册

返回顶部