Define a Pet class that stores the pet’s name, age, and weight. Add appropriate constructors, accessor, and mutator functions. Also define a function named getLifespan that returns a string with the value “unknown lifespan.”
时间: 2024-03-18 08:40:36 浏览: 15
Sure! Here's an example implementation of the `Pet` class in Python:
```python
class Pet:
def __init__(self, name, age, weight):
self.name = name
self.age = age
self.weight = weight
# Accessor functions
def getName(self):
return self.name
def getAge(self):
return self.age
def getWeight(self):
return self.weight
# Mutator functions
def setName(self, name):
self.name = name
def setAge(self, age):
self.age = age
def setWeight(self, weight):
self.weight = weight
# Additional function
def getLifespan(self):
return "unknown lifespan"
```
In this implementation, the `Pet` class has a constructor that takes in the pet's name, age, and weight as parameters and initializes the corresponding instance variables. It also has accessor and mutator functions for each instance variable.
The `getLifespan` function is defined to return the string "unknown lifespan" as per the requirements. Note that this function does not take any parameters and is not an accessor or mutator function.