Python 继承 以房产中介为例(一)房产类

Apartment 和 House 有共同属性:房间大小,卧室数目,卫生间数目。
故而以Property为超类,使Apartment 和 House 继承这些属性。

但是有个问题,子类在添加特有属性后,初始化时有各种其他属性的值输入。这是引入字典dict(dictionary)
将属性设置为空字符串,可以让属性作为关键字参数(key),dict可以使任何使用任何方法都不知道如何处理的额外参数,

通过super方法传递给子类。

class Property:#super class
    def __init__(self,square_feet=' ',bed=' ',baths=' ',**kwargs):
        super().__init__(**kwargs)    #详见**kwargs语法
        self.square_feet = square_feet
        self.bed = bed
        self.baths = baths
        
    def display(self):
        '''display property detalls.'''
        print("PROPERTY DETALLS")
        print("================")
        print("square_feet: {}".format(self.square_feet))
        print("has {}".fromat(self.bed)+" bedrooms")
        print("has {}".fromat(self.baths)+" bedrooms")
        print()
        
    def prompt_init():
        '''return detalls from dict'''
        return dict(square_feet = input("Enter the square feet: "),
                beds = input("Enter number of bedrooms: "),
                baths = input("Enter number of bathrooms: "))
    prompt_init = staticmethod(prompt_init)

Apartment 和 House 类继承自超类的属性、方法又含有自身特有属性、方法。
在获取参数时,两者使用的方法一致,都是input_string 和 valid_options两个参数,
因此可以将这个方法专门写成一个gvt.py 模块
def get_valid_input(input_string,valid_options):
    input_string += "({})".format(",".join(valid_options))
    response = input(input_string)
    while response.lower() not in valid_options:
        response = input(input_string)
    return response

剩下的Apartment 和 House 类基本一致
Apartment.py
from Property import Property
import gvt
class Apartment(Property):
        valid_laundries = ("coin","ensuite","none")
        valid_balconies = ("yes","no","solarium")
        
        def __init__(self,balcony=' ',laundry = ' ',**kwargs):
            super().__init__(**kwargs)
            self.balcony = balcony
            self.laundry = laundry
            
        def display(self):
            super().display()
            print("Apartment detalls")
            print("laundry: %s" % self.laundry)
            print("has balcony: %s" % self.balcony)
            
        def prompt_init():
            parent_init = Property.prompt_init()
            laundry = gvt.get_valid_input("What laundry facilities does "
                "the property have?",
                Apartment.valid_laundries)
            balcony = gvt.get_valid_input("Does the property have a balcony? ",
            Apartment.valid_balconies)
            parent_init.update({
                "laundry": laundry,
                "balcony": balcony
            })
            return parent_init
    
        prompt_init = staticmethod(prompt_init)
   
House.py
from Property import Property
import get_valid_input
class House(Property):
    valid_garage = ("attached","detached","none")
    valid_fenced = ("yes","no")
    
    def __init__(self,num_stories=' ',garage=' ',fenced=' ',**kwargs):
        super().__init__(**kwargs)
        self.num_stories = num_stories
        self.garage = garage
        self.fenced = fenced
    
    def display(self):
        super().display()
        print("House Detalls")
        print("# of stories: {}".format(self.num_stories))
        print("garage: {}".format(self.garage))
        print("fenced yard: {}".format(self.fenced)) 
    
    def prompt_init():
        parent_init = Property.parent_init()
        fenced = gvt.get_valid_input("Is the yard fenced? ",House.valid_fenced)
        garage = gvt.get_valid_input("Is there a garage? ",House.valid_garage)
        num_stories = input("How many stories? ")
        parent_init.update({
            "fenced":fenced,
            "garage":garage,
            "num_stories":num_stories})
        return parent_init
    prompt_init = staticmethod(prompt_init)
下次更新 Purchase 和 Rental 操作类(租赁/购买)点击打开链接

阅读更多
文章标签: Python
个人分类: Python
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭