1. 상속(inheritance) 상속 : 기존의 클래스를 재사용해서 새로운 클래스 작성 : 자손은 조상의 모든 멤버(생성자, 초기화블럭 제외)를 상속받음 : 자손의 멤버개수 >= 조상의 멤버개수 : Java는 단일상속만 허용 : '~은 ~이다.'를 가지고 문장을 만들었을 때 말이 되면 상속관계 ex) 원은 도형이다 class 자손클래스 extends 조상클래스 { // ... } class Parent {} class Child extends Parent {} class Child2 extends Parent {} class GrandChild extends Child {} 포함(composite) : 한 클래스의 멤버변수로 다른 클래스 선언 : 비중이 높은 클래스 하나만 상속관계로, 나머지는 포함관계..
- Ball 클래스(터틀 그래픽) from turtle import * class Ball(Turtle): def __init__(self, color, speed, size): self.turtle = Turtle() self.turtle.shape("circle") self.x = 0 self.y = 0 self.color = color self.turtle.color(color, color) self.xspeed = speed self.yspeed = speed self.size = size def move(self): self.x += self.xspeed self.y += self.yspeed self.turtle.goto(self.x, self.y) ball = Ball("red", 1, 2)..
__str__() 메소드 - 객체의 데이터를 문자열로 변환할 때 사용 - print(인스턴스) 출력할 때 자동적으로 호출됨 class Car: def __init__(self, speed, color, model): self.speed = speed self.color = color self.model = model def drive(self): self.speed = 60 def __str__(self): msg = "속도 = "+str(self.speed)+" 색상 = "+self.color+" 모델 = "+self.model return msg myCar = Car(0, "red", "SUV") print(myCar) # 속도 = 0 색상 = red 모델 = SUV - 참고로 __str__() 메소..