


Python의 서브클래스란 무엇입니까?
하위 클래스는 슈퍼클래스 또는 상위 클래스라고 하는 다른 클래스로부터 속성과 메서드를 상속하는 클래스입니다. 하위 클래스는 상위 클래스의 모든 속성과 메서드를 상속하며 자체 속성과 메서드를 추가할 수도 있습니다. " 및 "공원". 이제 Vehicle 클래스에서 상속받고 자동차에 특정한 속성과 메서드를 추가하는 "Car"라는 하위 클래스를 만들 수 있습니다. Car 클래스는 Vehicle 클래스의 모든 속성과 메서드를 상속하며 "make" 및 "model"과 같은 자체 속성과 메서드를 추가할 수도 있습니다.
다음은 Python에서 하위 클래스를 정의하는 방법에 대한 예입니다.
class 차량:
def __init__(self, color, number_of_wheels):
self.color = color
self.number_of_wheels = number_of_wheels
def 드라이브(self):
print("Driving...")
def park(self):
print("Parked...")
class Car(Vehicle):
def __init__(self, make, model, year):
super().__init__(make, model, year)
self.make = make
self. model = model
self.year = year
defdrive(self):
print("Driving the " + self.make + " " + self.model + " in " + self.year)
```
이 예에서는, Car 클래스는 Vehicle 클래스를 상속하고 자동차에 특정한 속성과 메서드를 추가합니다. Car 클래스는 또한 자동차의 제조업체, 모델 및 연도를 포함하는 새로운 구현으로 Vehicle 클래스의 운전 메소드를 재정의합니다. 부모 클래스의 메서드. 클래스 간의 계층적 관계를 생성하고 코드 재사용 및 모듈성을 촉진하기 위해 객체 지향 프로그래밍에서 일반적으로 사용됩니다.



