【问题标题】:Calling a method on an instance of a class, from another class (or file)从另一个类(或文件)调用类的实例上的方法
【发布时间】:2013-10-04 00:15:39
【问题描述】:

我有一个名为 Family Members 的类(在文件 family_members.rb 中),其中包含 namesextyperoleage 等属性

在这个类中,我定义了一个名为parent? 的方法,它检查Family members 类的实例是否属于type parent

代码工作正常,但我想知道是否可以在另一个类中定义 parent? 方法(可能是 superclass 或者只是能够包含该类),因为这会使代码更整洁.

有效的代码(但我想改进):

#family_members.rb  

class FamilyMembers

      attr_accessor :name, :sex, :status, :age
      def initialize (name, sex, type, role, age)
        @name = name
        @sex = sex
        @type = type
        @role = role
        @age = age
      end

      # Would Like the ability to put this method in another file or class
      def parent?
        parent_or_child = @type
        role = @role
        age = @age
        name = @name

        if parent_or_child == 'Parent'
        then puts "Yes, this family member is a parent; more specifically, a #{role} named #{name} who is #{age} years old."
        else puts  "This family member is not a parent, it`s only a child; more specifically, a #{role} named #{name} who is #{age} years old."
        end
      end

    end

    fm1 = FamilyMembers.new('John','Male', 'Child', "Son" , "27" )
    fm2 = FamilyMembers.new("Bill","Male", "Parent", "Father" , "63" )

    fm1.parent?

我尝试在另一个名为ParentOrChild.rb 的文件中定义该方法,并使用require_relative 将其包含在family_members.rb 的顶部。但是,当我这样做时,错误消息告诉我它无法加载此类文件..

#ParentOrChild.rb

def parent?
  parent_or_child = @type
  role = @role
  age = @age
  name = @name

  if parent_or_child == 'Parent'
  then puts "Yes, this family member is a parent; more specifically, a #{role} named #{name} who is #{age} years old."
  else puts  "This family member is not a parent, it`s only a child; more specifically, a #{role} named #{name} who is #{age} years old."
  end
end

我也尝试在另一个类中定义方法(在文件parent_or_child.rb:

#parent_or_child.rb

class ParentOrChild
  def parent?
    parent_or_child = @type
    role = @role
    age = @age
    name = @name

    if parent_or_child == 'Parent'
    then puts "Yes, this family member is a parent; more specifically, a #{role} named #{name} who is #{age} years old."
    else puts  "This family member is not a parent, it`s only a child; more specifically, a #{role} named #{name} who is #{age} years old."
    end
  end
end

但是当我执行上述操作时,错误告诉我有一个未定义的方法parent?(参考family_members.rb文件。

我使用的是 Ruby 2.0,所以我认为使用 require_relative 而不是 require 是正确的。

任何关于如何做到这一点的建议都会很棒 - 谢谢。

【问题讨论】:

  • 您说“错误告诉我有一个未定义的方法 parent”,但您的方法不是名为 parent?,因此您需要使用 fm1.parent? 调用它
  • 对不起,这是我的错,错误告诉我 undefined parent? .. 将编辑问题
  • 一般来说,以问号结尾的方法应该只返回真或假。无论如何,当我运行您的代码时,我没有收到错误消息。我得到了预期的输出。
  • 听起来您需要研究继承(或子类)。基本上:class ParentOrChild < FamilyMembers。这样子类 ParentOrChild 将继承父类 FamilyMembers 的方法。
  • 如果您愿意,我已经添加了答案:)

标签: ruby class methods


【解决方案1】:

这最初是一条评论,但它也是解决方案,所以我将其添加为答案。

听起来您需要研究继承(或子类)。基本上:

class ParentOrChild < FamilyMembers

这样子类ParentOrChild会继承父类FamilyMembers的方法

附带说明,以问号结尾的方法应该只返回真或假。

【讨论】:

  • 谢谢,我会考虑稍微改变一下程序,因为你说parent?应该只返回真或假
猜你喜欢
  • 2015-06-10
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多