class People: def __init__(self,name): self.name = name def work(self): print(self.name,'在工作') class Teacher(People): def __init__(self,name,salary,tel): super().__init__(name,salary,tel) self.salary = salary self.tel = tel def work(self): super().work() print(self.name,'在授课') def play(self,sport): print(self.name,'在',sport) if __name__== '__main__': t = Teacher('张三丰','男','15566668888') t.work() t.play('打篮球')
时间: 2024-04-26 15:20:46 浏览: 120
这是一个简单的 Python 类继承的例子,其中定义了一个 People 类和一个继承自 People 类的 Teacher 类。People 类有一个初始化方法 __init__(),用于初始化人的名字 name,而 Teacher 类在继承 People 类的基础上,增加了薪水 salary 和电话号码 tel 两个属性,并重写了 work() 方法和新增了 play() 方法。在主函数中,创建了一个 Teacher 的实例 t,并调用了它的 work() 和 play() 方法。其中,work() 方法被重写,覆盖了 People 类中的同名方法,同时也调用了父类的 work() 方法。play() 方法则是 Teacher 类新增的方法,用于打印老师正在进行的运动。
相关问题
优化代码class DeliveryRequest: def __init__(self, name, location, time): self.name = name self.location = location self.time = time class DeliveryStation: def __init__(self, location): self.location = location self.requests = [] def add_request(self, request): self.requests.append(request) def match_deliveryman(self, deliverymen): for request in self.requests: for deliveryman in deliverymen: if request.location == deliveryman.location and request.time == deliveryman.available_time: print(f"您有一个快递将送给{request.name},请您按时送货") print(f"您的快递将由{deliveryman.name}在{request.time}送达,请您按时等候") deliverymen.remove(deliveryman) break class DeliveryMan: def __init__(self, name, location, available_time): self.name = name self.location = location self.available_time = available_time class Dispatcher: def __init__(self, delivery_stations, deliverymen): self.delivery_stations = delivery_stations self.deliverymen = deliverymen def dispatch(self): for station in self.delivery_stations: station.match_deliveryman(self.deliverymen)
可以考虑优化代码的可读性和效率,例如使用列表推导式和更好的命名:
```python
class DeliveryRequest:
def __init__(self, name, location, time):
self.name = name
self.location = location
self.time = time
class DeliveryStation:
def __init__(self, location):
self.location = location
self.requests = []
def add_request(self, request):
self.requests.append(request)
def match_deliveryman(self, deliverymen):
for request in self.requests:
matching_deliverymen = [dm for dm in deliverymen if dm.location == request.location and dm.available_time == request.time]
for deliveryman in matching_deliverymen:
print(f"您有一个快递将送给{request.name},请您按时送货")
print(f"您的快递将由{deliveryman.name}在{request.time}送达,请您按时等候")
deliverymen.remove(deliveryman)
break
class DeliveryMan:
def __init__(self, name, location, available_time):
self.name = name
self.location = location
self.available_time = available_time
class Dispatcher:
def __init__(self, delivery_stations, deliverymen):
self.delivery_stations = delivery_stations
self.deliverymen = deliverymen
def dispatch(self):
for station in self.delivery_stations:
station.match_deliveryman(self.deliverymen)
```
这样,我们使用列表推导式来创建了一个匹配的快递员列表,然后使用一个 for 循环来遍历它。我们也使用更好的变量名来提高代码的可读性。
class Atank: def __init__(self,name): self.name=name def jineng1(self): print('{},坦克可以飞'.format(self.name)) class Btank: def __init__(self,name): self.name=name def jineng1(self): print('{},坦克可以发激光'.format(self.name)) def jineng2(self): print('{},坦克可以放大招'.format(self.name)) def Ctank(Atank,Btank): def __init__(self,name): super().__init__(name) atank=Atank('A') atank.jineng1() btank=Btank('B') btank.jineng1() btank.jineng2() ctank=Ctank('C') ctank.jineng1() ctank.jineng2()
对不起,我之前给出的代码有一些问题。重新给你提供一个修正后的代码如下:
```python
class Atank:
def __init__(self, name):
self.name = name
def jineng1(self):
print('{}, 坦克可以飞'.format(self.name))
class Btank:
def __init__(self, name):
self.name = name
def jineng1(self):
print('{}, 坦克可以发激光'.format(self.name))
def jineng2(self):
print('{}, 坦克可以放大招'.format(self.name))
class Ctank(Atank, Btank):
def __init__(self, name):
super().__init__(name)
atank = Atank('A')
atank.jineng1()
btank = Btank('B')
btank.jineng1()
btank.jineng2()
ctank = Ctank('C')
ctank.jineng1()
ctank.jineng2()
```
修正后的代码中,定义了三个类:Atank、Btank和Ctank。Atank和Btank都有自己的构造函数__init__,以及各自的jineng1和jineng2方法。
Ctank继承了Atank和Btank,通过super()调用父类的构造函数进行初始化。
然后创建了Atank的实例atank,并调用它的jineng1方法。
接着创建了Btank的实例btank,并依次调用它的jineng1和jineng2方法。
最后创建了Ctank的实例ctank,并调用它的jineng1和jineng2方法。
运行代码后,输出结果应该是:
```
A, 坦克可以飞
B, 坦克可以发激光
B, 坦克可以放大招
C, 坦克可以飞
C, 坦克可以放大招
```
如果你还有其他问题,请随时提问。
阅读全文