python设计带有私有成员的手机
时间: 2023-11-17 20:38:14 浏览: 136
class Phone:
def __init__(self, brand, model, price):
self.__brand = brand
self.__model = model
self.__price = price
def get_brand(self):
return self.__brand
def get_model(self):
return self.__model
def get_price(self):
return self.__price
def set_price(self, price):
self.__price = price
def make_call(self, number):
print("Dialing", number)
def send_message(self, number, message):
print("Sending message to", number, ":", message)
def __str__(self):
return f"{self.__brand} {self.__model} - ${self.__price}"
# Example usage
phone = Phone("Samsung", "Galaxy S21", 999)
print(phone.get_brand()) # Output: Samsung
phone.set_price(899)
print(phone.get_price()) # Output: 899
phone.make_call("123-456-7890") # Output: Dialing 123-456-7890
phone.send_message("123-456-7890", "Hello!") # Output: Sending message to 123-456-7890 : Hello!
print(phone) # Output: Samsung Galaxy S21 - $899
阅读全文