In this Python oops program, we will create a Python class example. Python class called Phone with attributes brand, model, and storage.
Python class example
Steps to solve the program
- The Phone class is defined with a constructor method __init__ that takes brand, model, and storage as parameters and initializes the instance variables self.brand, self.model, and self.storage with the provided values.
- The class also has three methods: make_call, send_text_message, and check_storage_capacity.
- The make_call method takes a number as a parameter and prints a message indicating that a call is being made to that number.
- The send_text_message method takes a number and a message as parameters and prints a message indicating that a text message is being sent to the specified number with the provided message.
- The check_storage_capacity method prints the storage capacity of the phone in gigabytes.
- The code creates an object phone of the Phone class by calling its constructor and passing the brand “Apple”, model “iPhone 14”, and storage capacity of 256 as arguments.
- The make_call method is called on the phone object to simulate making a call to the number “1234567890”.
- The send_text_message method is called on the phone object to simulate sending a text message to the number “1234567890” with the message “Hello!”.
- The check_storage_capacity method is called on the phone object to display the storage capacity of the phone.
- Thus, we have created a Python class example.
class Phone:
def __init__(self, brand, model, storage):
self.brand = brand
self.model = model
self.storage = storage
def make_call(self, number):
print(f"Making a call to {number}")
def send_text_message(self, number, message):
print(f"Sending a text message to {number}: {message}")
def check_storage_capacity(self):
print(f"Storage capacity: {self.storage}GB")
# Create an object of the Phone class
phone = Phone("Apple", "iPhone 14", 256)
phone.make_call("1234567890")
phone.send_text_message("1234567890", "Hello!")
phone.check_storage_capacity()
Output:
Making a call to 1234567890
Sending a text message to 1234567890: Hello!
Storage capacity: 256GB
Related Articles
Create a Python class called Laptop with attributes brand, model, and storage.
Create a Python class called Book with attributes title, author, and pages.
Create a Python class called EBook that inherits from the Book class.
Create a Python class called ShoppingCart with attributes items and total_cost.
Create a Python class called Animal with attributes name and color.
Create a Python class called Dog that inherits from the Animal class.