오류가 발생합니다.
lynda.rb:206:in `<main>': uninitialized constant Pig (NameError)
pig 클래스의 새 인스턴스를 인스턴스화하려고 할 때. 이 오류가 발생하는 이유를 아는 사람이 있습니까? 나는 온라인으로 검색했는데 일반적으로 부모 수업을 적절하게 요구하지 않기 때문이라고 들었습니다. 그러나 내 부모 클래스는 동일한 파일 내에 있습니다.
class Animal
attr_accessor :name
attr_writer :colour
attr_reader :legs, :arms
def initialize(noise,legs=4,arms=0)
@noise = noise
@legs = legs
@arms = arms
puts "A new animal has been instantiated"
end
def noise=(noise)
@noise = noise
end
def noise
@noise
end
class Pig < Animal
def noise
parent_method = super
puts "Hello and #{parent_method}"
end
end
class Cow < Animal
end
end
piggy = Pig.new("oink")
p piggy.noise
Pig
클래스 내부에 Animal
클래스를 정의했습니다 .
사실에도 불구하고 클래스를 올바르게 참조하고 싶은 문제를 해결하기 위해 의도 한 것이 아닐 가능성이 큽니다.
piggy = Animal::Pig.new("oink")
정의 Pig
및 Cow
외부 클래스를 Animal
가까이, 의미, 클래스 Animal
클래스를 열기 전에 Pig
. 이렇게하면 다음과 같이 인스턴스화 할 수 있습니다.
piggy = Pig.new("oink")
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다