Python基礎第四話:対象向け小練習

10567 ワード

目次
一番前に書く:
一、学生クラスの設計を完成する
二、簡単なphone類の設計を完成する
三、会社クラスを設計し、以下の要求を完成し、異なるオブジェクトを実例化して検証する
四、設計類とその継承類の小練習
一番前に書く:
初学の时、できないとは言わないで、理解していないとは言わないで、それはあなたの练习がまだ足りないことを说明して、独立して考えるのはまだまじめではありませんて、自分の1つの过程に対象に向かってそれからコードを次第に完璧にします.
一、学生クラスの設計を完成する
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
   01:       (Student)   :
              (student_totalnum),      (student_name),      1000(graduation_standrd),        (graduates_num)
          ,        60        ,         
               1000 ,      
          ,            ,            
                    
                 
                 
                      
"""
class Student(object):
    #        ,     0
    student_totalnum = 0
    #              ,    
    student_list = []
    #             1000  
    graduation_standrd = 1000
    #           ,     0
    graduates_num = 0
    #         、          ,     
    graduates_dict = dict()

    def __init__(self, student_name, student_age, student_sex):
        #               
        self.student_name = student_name
        #               
        self.student_age = student_age
        #               
        self.student_sex = student_sex

        #          0,          ,      
        self.__student_score = 0
        #        ,      +1
        Student.student_totalnum += 1
        #        ,             
        Student.student_list.append(student_name)

    def examinagion(self, student_score):
        if self.__student_score >= Student.graduation_standrd:
            str01 = str(self.student_name) + ',              ,    ,         ,        :' + str(self.__student_score)
            Student.graduates_dict.setdefault(self.student_name, self.__student_score)
            Student.graduates_num = len(Student.graduates_dict)
        elif 60 <= student_score <= 100:
            self.__student_score += student_score
            str01 = str(self.student_name) + ',      ,      ,        :' + str(self.__student_score)
        elif 0 <= student_score < 60:
            str01 = str(self.student_name) + ',       60  ,    ,       ,     ,        :' + str(self.__student_score)
        else:
            str01 = str(self.student_name) + ',      ,    ,       。'
        return str01

    def query_score(self):
        if self.__student_score >= Student.graduation_standrd:
            str02 = str(self.student_name) + ',       ,    :' + str(self.__student_score)
        else:
            disparity_score = Student.graduation_standrd - self.__student_score
            str02 = str(self.student_name) + ',        :' + str(self.__student_score) +',   ' + str(disparity_score) + '         。'
        return str02

    @classmethod
    def query_graduates_num(cls):
        cls.graduates_num = len(cls.graduates_dict)
        str03 = '            :' + str(cls.graduates_num)
        return str03

    @classmethod
    def query_student_totalnum(cls):
        str04 = '         :' + str(cls.student_totalnum) + ',          :' + str(cls.graduates_num)
        return str04

    @classmethod
    def query_student_list(cls):
        return cls.student_list

    @classmethod
    def query_graduates_dict(cls):
        return cls.graduates_dict

    @staticmethod
    def welcom_student():
        str05 = 'Welcome to Beijing!'
        return str05

二、簡単なphone類の設計を完成する
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
   02:       phone   
    1)         screen_size、price、brand
    2)              
    3)   play   ,     :play game
    4)   sendMessage   ,     :text message
    5)   powerOff   ,     :power off
    6)   get_info   ,         
    7)     phone(),     phone1,      5.5,    6288,    apple
    8)   3,4,5,6   ,    
    9)     phone(),     phone2,      5,    1999,    xiaomi
    10)   3,4,5,6   ,    
"""
class Phone(object):

    def __init__(self, screen_size, price, brand):
        self.__screen_size = screen_size
        self.__price = price
        self.__brand = brand

    def play(self):
        str01 = str(self.__brand) + ' play game'
        return str01

    def sendMssage(self, message):
        str02 = str(self.__brand) + ' >>> ' + str(message)
        return str02

    def powerOff(self, power_off):
        if str(power_off) == 'off':
            str03 = str(self.__brand) + '       .....'
        else:
            str03 = str(self.__brand) + '     ,      .....'
        return str03

    def getINFO(self):
        str04 = '     :' + str(self.__screen_size) + ';   :' + str(self.__price) + ':   :' + str(self.__brand)
        return str04

三、会社クラスを設計し、以下の要求を完成し、異なるオブジェクトを実例化して検証する
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
   03:       ,      ,            
       
        -         ,         
       
        -               
        -                
        
        -    ,  ,  ,   ,   ,    ,    
        :
        -     (           ,         ,  ,   )
        -     (            ,         ,   ,   )
        -       (        )
        -    (          ,        )
        -   (          ,        )
        -   (     *       ,      *         。)
        -         
        -        
"""
class Company(object):

    #      
    company_total = 0
    #       
    company_list = []

    def __init__(self, company_name, company_profile, company_cost):

        #     
        self.__company_name = company_name
        #     
        self.__company_profile = company_profile
        #     
        self.__company_profits = 0
        #      
        self.__company_sales = 0
        #      ,        
        self.__company_cost = company_cost
        #     (     =>   )
        self.__employee_dict = dict()

        #       ,      +1
        Company.company_total += 1
        #       ,      
        Company.company_list.append(company_name)

    #     
    def recruitTalented(self, employee_name, employee_salary):
        if employee_name in self.__employee_dict:
            str01 = '  !           ,' + str(employee_name) + '             ,    ,            。'
        else:
            self.__employee_dict.setdefault(employee_name, employee_salary)
            self.__company_cost += employee_salary
            employee_num = len(self.__employee_dict)
            str01 = '      ,            ,        :' + str(employee_num)
        return str01

    #     
    def dismissTalented(self, dismiss_name):
        if dismiss_name in self.__employee_dict:
            dismiss_salary = self.__employee_dict.pop(dismiss_name)
            self.__company_cost -= dismiss_salary
            employee_num = len(self.__employee_dict)
            str02 ='      ,' + str(dismiss_name) + '              ,        :' + str(employee_num)
        else:
            str02 = '  !    ,              。'
        return str02

    #       
    def advertsing(self, ad_costs):
        self.__company_cost += ad_costs
        str03 = '      ,        :' + str(self.__company_cost)
        return str03

    #    
    def paymentSocialSecurity(self):
        socia_security_base = 0
        for _, employee_salary in self.__employee_dict.items():
            socia_security_base += (employee_salary * 0.2)
        self.__company_cost += socia_security_base
        str04 = '           :' + str(socia_security_base) + ',        :' + str(self.__company_cost)
        return str04

    #   
    def payTaxes(self):
        pay_tax_base = 0
        for _, employee_salary in self.__employee_dict.items():
            pay_tax_base += (employee_salary * 0.12)
        self.__company_cost += pay_tax_base
        str05 = '         :' + str(pay_tax_base) + ',        :' + str(self.__company_cost)
        return str05

    #   (     *       ,      *         )
    def companySales(self, sales_count, selling_price, profit_margin):
        if 0 <= profit_margin <= 1:
            self.__company_sales += (sales_count * selling_price)
            self.__company_profits += (sales_count * selling_price * profit_margin)
            str06 = '      :' + str(self.__company_sales) + ',     :' + str(self.__company_profits)
        else:
            str06 = '     ,         。'
        return str06

    #         
    def employeeList(self):
        return list(self.__employee_dict.keys())

    #        
    def companyReported(self):
        company_reported = (self.__company_profits - self.__company_cost)
        str07 = '        :' + str(company_reported)
        return str07

    #              
    @classmethod
    def companyInfo(cls):
        str08 = '       :' + str(cls.company_total) + '  ,   :' + str(cls.company_list)
        return str08

四、設計類とその継承類の小練習
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
   04:     cinema      ,  :
       :
             ,             
        getSales(              )    
        :
        saleTickets  ,                  
       :
                    ,     
        :
             ,  ,   
        miniCinema       ,   cinema 
              ,  100       9     。
         miniCinema           
"""

class Cinema(object):

    cinema_totalnum = 0
    cinema_salessum = 0

    def __init__(self, cinema_name, cinema_address):

        self.cinema_name = cinema_name
        self.cinema_address = cinema_address
        self.cinema_sales = 0

        Cinema.cinema_totalnum += 1

    def saleTickets(self, sales):
        self.cinema_sales += sales
        Cinema.cinema_salessum += sales
        str01 = "%s        :%6.1f,           :%6.1f。" %(self.cinema_name, self.cinema_sales, Cinema.cinema_salessum)
        return str01

    @classmethod
    def getSales(cls):
        str02 = "     (%d)      :%6.1f。" %(cls.cinema_totalnum, cls.cinema_salessum)
        return str02

class MiniCinema(Cinema):

    def saleTickets(self, sales):
        if sales > 100:
            self.cinema_sales += (sales * 0.9)
            Cinema.cinema_salessum += (sales * 0.9)
        else:
            self.cinema_sales += sales
            Cinema.cinema_salessum += sales
        str03 = "%s        :%6.1f,           :%6.1f。" %(self.cinema_name, self.cinema_sales, Cinema.cinema_salessum)
        return str03