class Cal
attr_reader :v1, :v2
attr_writer :v1
@@_history = []
def initialize(v1,v2)
@v1 = v1
@v2 = v2
end
def add()
result = @v1+@v2
@@_history.push("add : #{@v1}+#{@v2}=#{result}")
return result
end
def subtract()
result = @v1-@v2
@@_history.push("subtract : #{@v1}-#{@v2}=#{result}")
return result
end
def setV1(v)
if v.is_a?(Integer)
@v1 = v
end
end
def getV1()
return @v1
end
def Cal.history()
for item in @@_history
p item
end
end
def info()
return "Cal => v1 : #{@v1}, v2 : #{@v2}"
end
end
class CalMultiply < Cal
def multiply()
result = @v1*@v2
@@_history.push("multipy : #{@v1}*#{@v2}=#{result}")
return result
end
def info()
return "CalMultiply => #{super()}"
end
end
class CalDivide < CalMultiply
def divide()
result = @v1/@v2
@@_history.push("divide : #{@v1}/#{@v2}=#{result}")
return result
end
def info()
return "CalDivide => #{super()}"
end
end
c0 = Cal.new(30, 60)
p c0.info()
c1 = CalMultiply.new(10, 10)
p c1.info()
c2 = CalDivide.new(20, 10)
p c2.info()
자식 클래스가 부모 클래스와 같은 이름의 메소드를 만들면 자식 클래스의 메소드가 부모 클래스의 메소드를 override해서 자식 클래스의 메소드가 실행된다. 부모 클래스에서 같은 이름의 메소드를 실행시키기 위해서 파이썬은 super().메소드명()을 사용하고, 루비는 super()만 사용한다. 파이썬의 super()는 부모클래스 자체를 가리키는 반면, 루비의 super()는 super()가 쓰인 메소드와 같은 이름의 부모 클래스 메소드를 불러온다. 감사합니다!
기본적으로 아톰은 현재 OS에 이미 설치된 2.x 버전으로 코드를 실행합니다. 따라서 코드를 실행하는 파이썬 엔진의 버전을 변경할 필요가 있는데요, 아래 링크를 보시고 따라하시면 됩니다. 링크가 이해 안 되시는 분은 구글에서 atom python3 이라고 치시고 검색해보시면 비슷한 해결법이 많이 나오니 참고하시면 되겠습니다. https://hellobaek.tistory.com/7
python 3 버전을 쓰는데도 계속 super() takes at least 1 argument (0 given)이러한 오류가 나서 진도를 나가지를 못하고 있네요.
인터넷에서 찾아봐도 3.0버전에서 개선이 되서 super() 저 괄호 안에 특정 요소가 없어도 이고잉 님의 코드대로 적으면 오류가 안 날 것 같은데 계속 나는 이유 혹시 알 수 있을까요?