【问题标题】:ruby - One superclass , 2 subclasses and One common Method - Having an errorruby - 一个超类,2个子类和一种常见方法 - 有错误
【发布时间】:2019-07-02 22:26:55
【问题描述】:

以下有Universe(超类)、Common(模块)、FlatEarthBelievers(子类)和RoundEarthBelievers(子类)。输出有错误,想要让它工作,任何指针?

这是关于继承和混合的,子类从一个公共父类继承并希望利用与父类行为无关的公共方法。我希望能够从包含/扩展它的 Child 类中的公共模块调用方法。我对它的用法感到困惑,因为我想将这些常用方法同时视为类方法和实例方法。

 class Universe
      $knowledge = "Universe comprises of galaxies, stars, planets,  steroids, meteoroids, comets, etc"
       def  universe_comprises_of
       puts $knowledge
     end
    end

    ​module Common
      $earth_is = Hash.new
      def init(earth_is)
        $earth_is = earth_is
      end
       def statement
         puts  " Earth is " + $earth_is["attribute"].to_str
       end
    end

    class FlatEarthBelievers < Universe
      include Common
      earth_is = { :attribute => "Flat !" }
      init(earth_is)
    end

    class RoundEarthBelievers < Universe
       include Common
       earth_is = { :attribute => "Round like an Orange!" }
       Common.init(earth_is)
    end

    moron = FlatEarthBelievers.new
    moron.universe_comprises_of
    moron.statement

    sensible_person = RoundEarthBelievers.new
    sensible_person.universe_comprises_of
    sensible_person.statement

【问题讨论】:

  • 请用每个类的代码块格式化您的帖子,详细说明错误并解释您想要做什么,代码中有一些格式错误,但我认为这是因为您的 C/P
  • FWIW,变量名以$开头是'un-Ruby-like'
  • 为什么? THIS_IS_A_CONSTANT、@instance_variable、local_variable、$global_variable 更多在这里
  • “有一个错误”不是一个足够精确的错误描述,我们无法帮助您。 什么不起作用? 如何不起作用?你的代码有什么问题?您收到错误消息吗?错误信息是什么?你得到的结果不是你期望的结果吗?你期望什么结果,为什么,你得到的结果是什么,两者有什么不同?您正在观察的行为不是期望的行为吗?期望的行为是什么,为什么,观察到的行为是什么,它们有何不同?
  • @NMPennypacker:这不仅仅是“不像 Ruby”。使用全局变量通常不像编程。永远。

标签: ruby-on-rails ruby subclass mixins superclass


【解决方案1】:

这段代码有很多问题。将$ 变量问题放在一边,因为这已在您的 OP 的 cmets 中进行了讨论。

首先,你这样做:

moron = Flat_earth_believers.new

但是您没有任何名为Flat_earth_believers 的类。你的意思可能是FlatEarthBelievers

include Common 模块,它使其中的所有方法都成为实例方法,但是你尝试将它们称为类方法,这里:

class FlatEarthBelievers < Universe
  include Common
  earth_is = { :attribute => "Flat!" }
  init(earth_is) # this is calling a class method
end

这里:

class RoundEarthBelievers < Universe
  include Common
  earth_is = { :attribute => "Round like an Orange!" }
  Common.init(earth_is) # this is calling a class method
end    

还有其他东西,但你明白了。

无论如何,我真的不知道你想做什么,但我想我会让Universe 喜欢:

class Universe
  KNOWLEDGE = "Universe comprises of galaxies, stars, planets,  steroids, meteoroids, comets, etc"

  def  universe_comprises_of
    puts KNOWLEDGE
  end
end

还有Common 喜欢:

module Common

  def statement
    puts "Earth is #{self.class::EARTH_IS[:attribute]}"
  end  

end

然后FlatEarthBelievers点赞:

class FlatEarthBelievers < Universe
  include Common

  EARTH_IS = { :attribute => "Flat!" }
end

那么你可以这样做:

moron = FlatEarthBelievers.new
moron.universe_comprises_of
 => Universe comprises of galaxies, stars, planets,  steroids, meteoroids, comets, etc
moron.statement
 => Earth is Flat!

我还会注意到Jörg W Mittag 想说:

我是那些喜欢指出 Ruby 中不存在类方法之类的东西的 Ruby 纯粹主义者之一。不过,我完全可以通俗地使用术语类方法只要各方完全理解这是一种通俗用法。换句话说,如果您知道没有类方法之类的东西,并且术语“类方法”只是“作为实例的对象的单例类的实例方法”的缩写Class",那么就没有问题了。但除此之外,我只看到它妨碍理解。

让各方充分理解,上面使用的术语类方法是通俗的。

【讨论】:

  • 谢谢大家,特别是“jvillan”。问题是关于继承和混合的,子类从一个共同的父类继承并希望利用与父类行为无关的通用方法。我希望能够从包含/扩展它的 Child 类中的公共模块中调用方法,但我对它的使用感到困惑,因为我想将其视为类方法和实例方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 2010-11-18
  • 2014-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多