Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby—我可以选择一个位置,将一些代码放在用Super继承的方法中吗?_Ruby_Inheritance_Parent_Super - Fatal编程技术网

Ruby—我可以选择一个位置,将一些代码放在用Super继承的方法中吗?

Ruby—我可以选择一个位置,将一些代码放在用Super继承的方法中吗?,ruby,inheritance,parent,super,Ruby,Inheritance,Parent,Super,我在此举一个例子: class A def go(name = "girls") print "hello " print name puts " !" end end class B < A def go super("boys") end end A.new.go B.new.go 我需要这样做,但不只是改变参数,而是添加一些变量: class A def go(na

我在此举一个例子:

class A

    def go(name = "girls")

        print "hello "
        print name
        puts " !"
    end
end

class B < A

    def go

        super("boys")
    end
end

A.new.go
B.new.go
我需要这样做,但不只是改变参数,而是添加一些变量:

class A

    def go(name = "girls")

        dad_variable = "coders"

        print "hello "

        superAnchor("myAnchor") # put a "super anchor" here

        puts " !"
    end
end

class B < A

    def go

        superAnchor("myAnchor"){
            print dad_variable # do some code in relation with Daddy
            # do some other things but with the parent variables
            # interacting with the inherited environnement
        }
    end
end

A.new.go
B.new.go
总之,我想知道我们是否可以将锚放在继承父方法的子类中,并将一些代码与锚在父方法中的位置和父方法的环境变量相关联

有这样的系统吗?
谢谢大家:)

看起来你可以在子类中重写一个方法调用,而不是
superAnchor(“myAnchor”)
,不是吗?也许超类可以将
dad\u变量
对象传递给该方法,该方法可以使用或忽略该变量。此外,子类不应该能够读取超类的私有状态(或变量)…

我不确定这是否是您要寻找的,但是当您调用
super
A时,go
B
的实例上执行,并且像任何其他方法一样,返回(到
B
的实例)最后一次计算采用该方法。例如,如果您将
dad\u variable=“coders”
(或只是
“coders”
)放在
A#go
的最后一行,将返回
“coders”

class A
  def go(name = "girls")
    puts "hello from A"
    dad_variable = "coders"
  end
end

class B < A
  def go
    puts super
  end
end

A.new.go
  # hello from A
B.new.go
  #hello from A
  #coders
A类
def go(name=“girls”)
放上“A的你好”
dad_variable=“编码器”
结束
结束
B类
孩子们,我想,我发现,这就是屈服系统

(对不起法国消息来源)


但我不太确定……

我相信这项建议应该归功于马克斯。我将根据我的理解详细阐述这个想法

您可以在子类行为需要分歧的地方插入一个方法调用,如
do\u specialized\u stuff
(使用更好的名称)。然后,要自定义子类中的行为,只需覆盖
do\u specialized\u stuff
的定义

class A
    def go(name = "girls")

        dad_variable = "coders"

        print "hello "

        do_specialized_stuff(dad_variable)

        puts " !"
    end

    def do_specialized_stuff(dad_variable)
        # perhaps this is empty in class A or maybe it does something
    end
end

class B < A

    # no need to redefine the 'go' method

    def do_specialized_stuff(dad_variable)
        # specialized stuff is different in class B
    end
end
A类
def go(name=“girls”)
dad_variable=“编码器”
打印“你好”
do_专门的东西(dad_变量)
放入“!”
结束
def do_专用_材料(dad_变量)
#也许这在A类中是空的,或者它做了些什么
结束
结束
B类
是的,我知道super用于获取继承方法的内容,然后在submethod上可以使用所有parentMethod变量,但这里的问题是将代码放在父类中的特定位置。。。如果你是这么说的,请写一些代码来解释;)对于你的回答,是的,我了解覆盖系统,但要使用方法,你必须为该方法输入一些参数…是的,我知道,最后一个未分配的变量就像一个返回的变量,但这不是我要寻找的,我想从子方法在parentMethod中的指定位置输入一些代码。。。但是泰:)我不明白你想做什么。当然,您可以添加、删除或替换父类方法,但是您不能动态地添加、删除或修改方法中的语句,无论它们是在同一个类中还是在不同的类中。也许您可以编辑以提供一个更具体的示例来说明您试图完成的任务。父方法的环境(本地)变量可以通过
绑定捕获。但是
A.new
B.new
只是不同的对象。这里没有连接(除了
A
B
的超类,如果你称之为连接…)。我想知道你为什么想做这样的事情。好吧,那我就不能做我想做的事,但那什么也做不了,因为我想找到的是一种干净的方法,然后唯一的方法是重新创建方法:)但是ty All;)@CarySwoveland-我需要找到这种类型的东西,因为对于我从父类创建的每个子类,我重复了
musics Yield不会让你访问私有状态,除非你将它与Yield语句一起传递(例如Yield foo)。
class A
  def go(name = "girls")
    puts "hello from A"
    dad_variable = "coders"
  end
end

class B < A
  def go
    puts super
  end
end

A.new.go
  # hello from A
B.new.go
  #hello from A
  #coders
class A
    def go(name = "girls")

        dad_variable = "coders"

        print "hello "

        do_specialized_stuff(dad_variable)

        puts " !"
    end

    def do_specialized_stuff(dad_variable)
        # perhaps this is empty in class A or maybe it does something
    end
end

class B < A

    # no need to redefine the 'go' method

    def do_specialized_stuff(dad_variable)
        # specialized stuff is different in class B
    end
end