Create a Customer class that uses accessor (getter) and mutator (setter) methods to control access to its properties.
1. Class Properties
name- Should beย immutableย (cannot be changed after the object is created).
phone_number- Should beย mutableย (can be changed after the object is created).
2. Class Methods
__init__(self, name, phone_number)- The constructor that takesย
nameย andยphone_numberย as parameters. - Initializes the object’s properties with these values.
- The constructor that takesย
- Accessor Methods (Getters)
get_name(self)- Returns the customer’s name.
get_phone_number(self)- Returns the customer’s phone number.
- Mutator Method (Setter)
set_phone_number(self, new_phone_number)- Takes a new phone number as a parameter.
- Updates the customer’s phone number.
- (Only this property can be changed; theย
nameย cannot be modified).
3. Task Instructions
- Write theย
Customerย class with the constructor and the three methods described above. - Create an object (instance) of theย
Customerย class. - Test the class by:
- Using the getter methods (
get_name,ยget_phone_number) to retrieve the values. - Using the setter method (
set_phone_number) to change the phone number. - Verifying that the phone number has been successfully updated.
- Using the getter methods (
class Customer:
def __init__(self, name, phoneno):
self.name = name
self.phneno = phoneno
def get_name(self):
return self.name
def get_phoneno(self):
return self.phneno
def set_phoneno(self, ph):
self.phneno = ph
c1 = Customer('John', 7650984321)
print('Name:', c1.get_name())
print('Phone No:', c1.get_phoneno())
c1.set_phoneno(6789012345)
print('')
print('Name:', c1.get_name())
print('Phone No:', c1.get_phoneno())