簡體   English   中英

類屬性內的 Python 類屬性

[英]Python class attribute inside of class attribute

我想創建一個名為 Customer 的類。 此類應包含屬性 self.name、self.address、self.customernumber。 屬性 self.address 應該包含更多信息,這些信息在不同客戶之間有所不同。 此信息包括客戶街道、郵政編碼、城市和發票地址。 是否有可能將我剛剛命名的這些點視為 self.address 的子屬性,我該怎么做?

class Customer:
    def __init__ (self, name, address, custnumber):
        self.name = name
        self.address = address:
              self.address.street
              self.address.postcode
              self.address.city
              self.address.invoiceaddress
        self.custnumber = customer

cust1 = Customer(Person Example, address: (street1, city1,invoiceaddress, ... ) , 124421)

這是一個組合案例。 您創建一個Address類,並將一個實例作為第二個參數傳遞給Customer

class Address:
    def __init__(self, street: str, postcode: str, city: str, invoiceaddress: str):
        self.street = street
        self.postcode = postcode
        self.city = city
        self.invoiceaddress = invoiceaddress


class Customer:
    def __init__ (self, name: str, address: Address, custnumber: str):
        self.name = name
        self.address = address
        self.custnumber = customer

cus1 = Customer("bob", Address("main st", "Springfield", "..."), 1)        

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM