python基础升级 第四课 练习与作业 离歌

练习1

class phone():
    def __init__(self,screen_size,price,brand):
        self.screen_size=screen_size
        self.price=price
        self.brand=brand
    def get_screen_size(self):
        return self.__screen_size
    def set_screen_siz(self,screen_size):
        self.__screen_size=screen_size
    def get_price(self):
        return self.__price
    def set_price(self,price):
        self.__price=price
    def get_brand(self):
        return self.__brand
    def set_brand(self,brand):
        self.__brand=brand
    def play(self):
        print('play game')
    def sendMessage(self):
        print('text message')
    def powerOff(self):
        print('power off')
    def get_info(self):
        print(self.__init__)
        print(self.__setattr__)
        
phone1=phone(5.5,6288,'apple')
phone1.play()
phone1.sendMessage()
phone1.get_info()
phone2=phone(4.7,1999,'MI')
phone2.play()

练习2

class Company():
    companyCount=0   #类成员
    def __init__(self,companyName,compRec,profits,sales,cost,employeeSum,*employeeName):
        self.__companyName=companyName
        self.__compRec=compRec
        self.__profits=profits
        self.__sales=sales
        self.__cost=cost
        self.__employeeName=[]
        self.__employeeSum=employeeSum
        Company.companyCount+=1
    @classmethod
    def reCompay(cls,):
        print('公司实例',Company.companyCount)
    #招聘人才
    def recruitTalent(self,employeeName,cost):
        self.__employeeName.append(employeeName)
        self.__employeeSum+=1
        self.__cost+=cost
    #解雇人员
    def fireTalent(self,employeeName,cost):
        self.__employeeName.remove(employeeName)
        self.__employeeSum-=1
        self.__cost+=cost
    #公司广告推广
    def advertPromotion(self,cost):
        self.__cost+=cost
    #交社保
    def socialSec(self,cost):
        self.__cost=self.__employeeSum*cost
    #交税
    def tax(self,cost):
        self.__cost=self.__employeeSum*cost
    #销售
    def saleMethod(self,piece,price,profitsRate):
        self.__sales=piece*price
        self.__profits=self.__sales*profitsRate
    #获取公司雇员列表
    def get_employee(self):
        return self.__employeeName
    #获取公司净利润
    def get_profits(self):
        return self.__profits-self.__cost

练习3

class Cinema():
    
        cinemaNum=0
        totalSale=0
        def __init__(self,cinemaName,position,sales):
            self.__cinemaName=cinemaName
            self.__position=position
            self.__sales=sales
            Cinema.cinemaNum+=1
            Cinema.totalSale=sales
        def saleTickets(self,sales):
            self.__sales+=sales
            Cinema.totalSale+=sales
        @classmethod
        def getSales(cls,):
            return Cinema.totalSale
        @property
        def sales(self):
            return self.__sales
        @sales.setter
        def sales(self,sales):
            self.__sales=sales
class MiniCinema(Cinema):
        
        def saleTickets(self,sales):
            if sales>100:
                self.sales=self.sales+sales*0.9
                Cinema.totalSale+=sales*0.9
            else:
                self.sales+=sales
                Cinema.totalSale+=sales

练习4

import os

os.listdir()

os.path.isdir()

import os
def myDir(dir1):
    for i in os.listdir(dir1):
         if os.path.isdir(dir1+'\\'+i)==True:
            print( dir1+'\\'+i )
            myDir(dir1+'\\'+i)
            

            
            

练习5

import PIL
from PIL import Image
image=Image.open('11111111.jpg')
image.resize((100,100))
a,b=image.size
image.thumbnail((a//2,b//2))
image.thumbnail((a*2,a*2))
image.getpixel((4,4))
image.putpixel((5,5),(255,255,0))

将黑色RGB 改成白色,黑色 0,0,0 白色 255 255 255

from PIL import Image
image=Image.open('11111111.jpg')
i,j=image.size
for i in range(i):
    for j in range(j):
        r,g,b=image.getpixel((i,j))
        if r==255 and g==255 and b==255:
            image.putpixel((i,j),(0,0,0))
image.save('11111111.jpg')

作业

class myRange():
    def __init__(self,sum):
        self.__sum=sum
    def myR(self,sum1,sum2):
        L2=[]
        for i in self.__sum[sum1:sum2+1]:
            L2.append(i)
        print(L2)
    
    
已邀请:

ClownFish

赞同来自:


呀,一直没看到离歌的作业~抱歉

其它题完成的都很好,但最后一题,好像不太对吧。
要实现自定义切片访问,需要重写魔法函数的。。你再查查。

要回复问题请先登录注册

返回顶部