Ruby 使用oop(类)时出错
我刚开始学习ruby,今天刚开始学习oop,在上完一堂课后,我试图打印到控制台,但我一直遇到这个错误。有人知道怎么了吗 #的未定义方法“set_brand_name=”(NoMethodError) 以下是导致此错误的代码:Ruby 使用oop(类)时出错,ruby,Ruby,我刚开始学习ruby,今天刚开始学习oop,在上完一堂课后,我试图打印到控制台,但我一直遇到这个错误。有人知道怎么了吗 #的未定义方法“set_brand_name=”(NoMethodError) 以下是导致此错误的代码: class Laptop def set_brand_name(brand_name) @brand = brand_name end def get_brand_name return @brand en
class Laptop
def set_brand_name(brand_name)
@brand = brand_name
end
def get_brand_name
return @brand
end
def set_color(color)
@color = color
end
def get_color
return @color
end
def set_processor(processor)
@processor = processor
end
def get_processor
return @processor
end
def set_storage(hard_drive)
@storage = hard_drive
end
def get_storage
return @storage
end
def set_memory(ram)
@memory = ram
end
def get_memory
return @memory
end
end
my_laptop = Laptop.new
my_laptop.set_brand_name = "HP"
my_laptop.set_processor = 'i7-4700k'
my_laptop.set_memory = '16gb'
my_laptop.set_storage = '750gb'
my_laptop.set_color = 'Silver'
brand = my_laptop.get_brand_name
color = my_laptop.get_color
processor = my_laptop.get_processor
memory = my_laptop.get_memory
storage = my_laptop.get_storage
这将输出以下消息:
“我想要的笔记本电脑是{brand},它有一个{processor},
#ram的{memory},一个{storage},还有它{color}!!!”
我做错了什么?问题是您没有按照定义调用方法名。您定义的
set_brand_name
没有等号,因此请使用:
my_laptop.set_brand_name("HP")
我只想让这些能手和二传手这样:
class Laptop
def brand_name=(brand_name)
@brand_name = brand_name
end
def brand_name
@brand_name
end
end
或者更好:
class Laptop
attr_accessor :brand_name
end
然后您可以使用相同的方法:
my_laptop = Laptop.new
my_laptop.brand_name = "HP"
puts my_laptop.brand_name # => "HP"
问题是您没有按照定义的方式调用方法名。您定义的
set_brand_name
没有等号,因此请使用:
my_laptop.set_brand_name("HP")
我只想让这些能手和二传手这样:
class Laptop
def brand_name=(brand_name)
@brand_name = brand_name
end
def brand_name
@brand_name
end
end
或者更好:
class Laptop
attr_accessor :brand_name
end
然后您可以使用相同的方法:
my_laptop = Laptop.new
my_laptop.brand_name = "HP"
puts my_laptop.brand_name # => "HP"
问题是您没有按照定义的方式调用方法名。您定义的
set_brand_name
没有等号,因此请使用:
my_laptop.set_brand_name("HP")
我只想让这些能手和二传手这样:
class Laptop
def brand_name=(brand_name)
@brand_name = brand_name
end
def brand_name
@brand_name
end
end
或者更好:
class Laptop
attr_accessor :brand_name
end
然后您可以使用相同的方法:
my_laptop = Laptop.new
my_laptop.brand_name = "HP"
puts my_laptop.brand_name # => "HP"
问题是您没有按照定义的方式调用方法名。您定义的
set_brand_name
没有等号,因此请使用:
my_laptop.set_brand_name("HP")
我只想让这些能手和二传手这样:
class Laptop
def brand_name=(brand_name)
@brand_name = brand_name
end
def brand_name
@brand_name
end
end
或者更好:
class Laptop
attr_accessor :brand_name
end
然后您可以使用相同的方法:
my_laptop = Laptop.new
my_laptop.brand_name = "HP"
puts my_laptop.brand_name # => "HP"
在第45行中,您正在调用方法
set\u brand\u name=
,但是您的Laptop
类没有具有该名称的方法。您需要调用已有的方法(set\u brand\u name
),或者将set\u brand\u name
方法重命名为set\u brand\u name=
但请注意,这两种语言都不是惯用语。习惯上,该方法应命名为brand\u name=
(如果没有set\u
前缀,“设置”部分已经由=
符号暗示),您不应该手动定义它,而应该使用模块#attr\u writer
方法以编程方式进行定义
您的整个代码可以压缩为:
Laptop = Struct.new(:brand_name, :color, :processor, :storage, :memory)
my_laptop = Laptop.new('HP', 'Silver', 'i7-4700k', '750gb', '16gb')
brand = my_laptop.brand_name
color = my_laptop.color
processor = my_laptop.processor
memory = my_laptop.memory
storage = my_laptop.storage
puts "The Laptop I want is an #{brand}, it has a #{processor}, #{memory} of ram, a #{storage}, and it's #{color}!!!"
在第45行中,您正在调用方法
set\u brand\u name=
,但是您的Laptop
类没有具有该名称的方法。您需要调用已有的方法(set\u brand\u name
),或者将set\u brand\u name
方法重命名为set\u brand\u name=
但请注意,这两种语言都不是惯用语。习惯上,该方法应命名为brand\u name=
(如果没有set\u
前缀,“设置”部分已经由=
符号暗示),您不应该手动定义它,而应该使用模块#attr\u writer
方法以编程方式进行定义
您的整个代码可以压缩为:
Laptop = Struct.new(:brand_name, :color, :processor, :storage, :memory)
my_laptop = Laptop.new('HP', 'Silver', 'i7-4700k', '750gb', '16gb')
brand = my_laptop.brand_name
color = my_laptop.color
processor = my_laptop.processor
memory = my_laptop.memory
storage = my_laptop.storage
puts "The Laptop I want is an #{brand}, it has a #{processor}, #{memory} of ram, a #{storage}, and it's #{color}!!!"
在第45行中,您正在调用方法
set\u brand\u name=
,但是您的Laptop
类没有具有该名称的方法。您需要调用已有的方法(set\u brand\u name
),或者将set\u brand\u name
方法重命名为set\u brand\u name=
但请注意,这两种语言都不是惯用语。习惯上,该方法应命名为brand\u name=
(如果没有set\u
前缀,“设置”部分已经由=
符号暗示),您不应该手动定义它,而应该使用模块#attr\u writer
方法以编程方式进行定义
您的整个代码可以压缩为:
Laptop = Struct.new(:brand_name, :color, :processor, :storage, :memory)
my_laptop = Laptop.new('HP', 'Silver', 'i7-4700k', '750gb', '16gb')
brand = my_laptop.brand_name
color = my_laptop.color
processor = my_laptop.processor
memory = my_laptop.memory
storage = my_laptop.storage
puts "The Laptop I want is an #{brand}, it has a #{processor}, #{memory} of ram, a #{storage}, and it's #{color}!!!"
在第45行中,您正在调用方法
set\u brand\u name=
,但是您的Laptop
类没有具有该名称的方法。您需要调用已有的方法(set\u brand\u name
),或者将set\u brand\u name
方法重命名为set\u brand\u name=
但请注意,这两种语言都不是惯用语。习惯上,该方法应命名为brand\u name=
(如果没有set\u
前缀,“设置”部分已经由=
符号暗示),您不应该手动定义它,而应该使用模块#attr\u writer
方法以编程方式进行定义
您的整个代码可以压缩为:
Laptop = Struct.new(:brand_name, :color, :processor, :storage, :memory)
my_laptop = Laptop.new('HP', 'Silver', 'i7-4700k', '750gb', '16gb')
brand = my_laptop.brand_name
color = my_laptop.color
processor = my_laptop.processor
memory = my_laptop.memory
storage = my_laptop.storage
puts "The Laptop I want is an #{brand}, it has a #{processor}, #{memory} of ram, a #{storage}, and it's #{color}!!!"
您的setter方法定义不正确 以下是您对
set\u brand\u name
方法的定义:
def set_brand_name(brand_name)
@brand = brand_name
end
下面是您如何称呼它的:
my_laptop.set_brand_name = "HP"
您调用的方法不正确。如果你想保留你的定义,你应该这样称呼它:
my_laptop.set_brand_name("HP")
def set_brand_name=(brand_name)
@brand = brand_name
end
或者,如果您想使用等号,您应该这样定义您的方法:
my_laptop.set_brand_name("HP")
def set_brand_name=(brand_name)
@brand = brand_name
end
注意方法定义中的等号吗?当您希望setter看起来像一个常规任务时,需要使用它
然而,对于大多数简单的情况,您不需要手动定义getter和setter。您只需在类上使用attr\u访问器
,并将要定义的属性传递给它即可。下面是使用attr\u访问器时类的外观:
class Laptop
attr_accessor: :brand_name, :color, :processor, :storage, :memory
end
my_laptop = Laptop.new
my_laptop.brand_name = "HP"
my_laptop.processor = 'i7-4700k'
my_laptop.memory = '16gb'
my_laptop.storage = '750gb'
my_laptop.color = 'Silver'
brand = my_laptop.brand_name
color = my_laptop.color
processor = my_laptop.processor
memory = my_laptop.memory
storage = my_laptop.storage
puts """The Laptop I want is an #{brand}, it has a #{processor},
#{memory} of ram, a #{storage}, and it #{color}!!!"""
我鼓励您尝试一下。您的setter方法定义不正确
以下是您对set\u brand\u name
方法的定义:
def set_brand_name(brand_name)
@brand = brand_name
end
下面是您如何称呼它的:
my_laptop.set_brand_name = "HP"
您调用的方法不正确。如果你想保留你的定义,你应该这样称呼它:
my_laptop.set_brand_name("HP")
def set_brand_name=(brand_name)
@brand = brand_name
end
或者,如果您想使用等号,您应该这样定义您的方法:
my_laptop.set_brand_name("HP")
def set_brand_name=(brand_name)
@brand = brand_name
end
注意方法定义中的等号吗?当您希望setter看起来像一个常规任务时,需要使用它
然而,对于大多数简单的情况,您不需要手动定义getter和setter。您只需在类上使用attr\u访问器
,并将要定义的属性传递给它即可。他