Python实现门的批量与单独上色功能:如何添加代码统一修改所有门的颜色?
Fixing Your Door Coloring System (Single & Batch Painting)
Hey there! Let's clean up your code and add the functionality you need to paint doors individually or all at once. First off, having four nearly identical Door classes is a big red flag—duplicate code is hard to maintain, and we can solve this way more elegantly.
The Core Problem with Your Current Code
Your four Door classes are identical except for their initial colour class attribute. This means if you want to add a new feature (like locking doors), you'd have to update four classes instead of one. Plus, there's no way to track all door instances to paint them all at once right now.
The Solution: Refactor to a Single Door Class with Instance Tracking
We'll do three key things:
- Use a single
Doorclass withcolouras an instance attribute (so each door can have its own color). - Add a class-level list to track all door instances automatically.
- Implement both individual painting (per door) and batch painting (all doors at once) methods.
Modified Code
class Door: # Class-level list to keep track of all door instances all_doors = [] def __init__(self, number, status, colour): self.number = number self.status = status self.colour = colour # Colour is now an instance attribute # Automatically add new doors to our tracking list Door.all_doors.append(self) @classmethod def knock(cls): print("Knock!") # Paint a single door (instance method) def paint(self, new_colour): self.colour = new_colour print(f"Door {self.number} painted {new_colour}!") # Paint ALL doors at once (class method) @classmethod def paint_all(cls, new_colour): for door in cls.all_doors: door.paint(new_colour) print(f"All doors painted {new_colour}!") def open(self): self.status = 'opened' def close(self): self.status = 'closed' # Create your door instances (each with unique initial color) door1 = Door(1, 'closed', 'brown') door2 = Door(2, 'closed', 'yellow') door3 = Door(1, 'closed', 'green') door4 = Door(2, 'closed', 'red') # Test initial colors print("Initial door colors:") for door in Door.all_doors: print(f"Door {door.number}: {door.colour}") # Test individual painting print("\n--- Painting Door 1 blue ---") door1.paint('blue') # Test batch painting all doors gray print("\n--- Painting all doors gray ---") Door.paint_all('gray') # Final colors print("\nFinal door colors:") for door in Door.all_doors: print(f"Door {door.number}: {door.colour}")
What This Does
- Single Class: No more duplicate code—all doors use the same
Doorclass, with unique attributes passed during initialization. - Individual Painting: Call
door.paint('color')on any door instance to change just that door's color. - Batch Painting: Call
Door.paint_all('color')to update every door's color in one go, thanks to theall_doorslist tracking every instance. - Automatic Tracking: Every time you create a new
Door, it's added toall_doorsautomatically—no manual list management needed.
Why This Is Better Than Your Original Code
- Maintainable: If you need to add a new method (like
lock()), you only update one class. - Flexible: You can easily add more doors later without writing a whole new class.
- Clean: Eliminates redundant code and centralizes door management.
内容的提问来源于stack exchange,提问作者tonyalmeida24
相关产品推荐
相关产品推荐

