标签: instance-methods

在没有实例化类的情况下调用ruby方法

如果我在rails活动模型方法上调用一个方法,如下所示:

class Foo < ActiveRecord::Base

end

Foo.first
Run Code Online (Sandbox Code Playgroud)

我会回来第一个活跃的记录.我不必实例化该类.

但是,如果我创建自己的类并调用方法,我会得到一个例外:

class Person < ActiveRecord::Base
  def greeting
    'hello'
  end
end

Person.greeting 

#EXCEPTION: undefined method `greeting' for Person:Class
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题呢?

ruby ruby-on-rails class-method instance-methods

4
推荐指数
2
解决办法
8876
查看次数

如何从另一个类调用@selector方法

是否可以从另一个类调用@selector方法?例如,我创建一个方法"bannerTapped:"并从"myViewController.m"类调用它.

myviewcontroller.m:

anotherClass *ac= [[anotherClass alloc]init];

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:ac action:@selector(bannerTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
cell.conversationImageView.tag = indexPath.row;
[cell.conversationImageView addGestureRecognizer:singleTap];
[cell.conversationImageView setUserInteractionEnabled:YES];
Run Code Online (Sandbox Code Playgroud)

anotherClass.m:

-(void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
    //do something here 
}
Run Code Online (Sandbox Code Playgroud)

更新:

viewController.m:

 #import "anotherClass.h"



 +(viewcontroller *)myMethod{
 // some code...

 anotherClass *ac= [[anotherClass alloc]init];

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:ac   action:@selector(bannerTapped:)];

}
Run Code Online (Sandbox Code Playgroud)

anotherClass.h:

-(void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer;
Run Code Online (Sandbox Code Playgroud)

anotherClass.m:

-(void)Viewdidload{
    [viewController myMethod];
     }


   -(void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
      //do something here 
   }
Run Code Online (Sandbox Code Playgroud)

objective-c instance-methods uitapgesturerecognizer ios7

4
推荐指数
1
解决办法
9394
查看次数

查找数组的第二个元素

我不明白这怎么行不通。该程序应该采用 Array 类中的第二个实例方法并返回数组中的第二个对象

class Array
  def second(*arr)
  arr.length <= 1 ? nil : arr[1]
  end
end

#Test cases
Test.assert_equals(Array([1, 2, 3]), 2,) #Getting nil
Test.assert_equals(Array([]), nil) #passes
Test.assert_equals(Array([1]), nil) #passes
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?如果我删除类数组并在第二个测试它工作正常吗?

ruby arrays class splat instance-methods

3
推荐指数
1
解决办法
1万
查看次数

使用超类方法作为实例方法

我目前正在阅读 Luciano Ramalho 的优秀著作Fluent Python。在关于接口和继承的一章中,我们构建了一个列表的子类(原始代码请参见github),但我对定义其中一个实例方法的方式感到困惑。对于一个简化的例子,我的困惑是由以下情况引起的:

class ListWithLoadMethod(list):
    load = list.extend
Run Code Online (Sandbox Code Playgroud)

它生成一个新的 list 子类,该子类具有extend方法的别名as load。我们可以通过编写来测试这个类

loaded_list = ListWithLoadMethod(range(4))
print(loaded_list)
loaded_list.extend(range(3))
print(loaded_list)
loaded_list.load(range(3))
print(loaded_list) 
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,它产生:

[0, 1, 2, 3]
[0, 1, 2, 3, 0, 1, 2]
[0, 1, 2, 3, 0, 1, 2, 0, 1, 2]
Run Code Online (Sandbox Code Playgroud)

我的困惑源于类方法和静态方法的区别。当新实例ListWithLoadedMethod被创建,它的子类list,但是当我们初始化我们点了实例loadlist.extend; Python怎么知道list.extend我们不是说load应该指向类的类方法,list而是实际上(显然?)继承超类列表的实例方法?

python inheritance class-method instance-methods

3
推荐指数
1
解决办法
55
查看次数

从Main调用函数()

我是C#的新手,我在从Main()方法调用函数时遇到了一些问题.

class Program
{
    static void Main(string[] args)
    {
        test();
    }

    public void test()
    {
        MethodInfo mi = this.GetType().GetMethod("test2");
        mi.Invoke(this, null);
    }

    public void test2()
    { 
        Console.WriteLine("Test2");
    }
}
Run Code Online (Sandbox Code Playgroud)

我在编译错误test();:

非静态字段需要对象引用.

我还不太了解这些修饰语,所以我做错了什么?

我真正想做的是将test()代码放在里面,Main()但是当我这样做时它会给我一个错误.

c# static-methods instance-methods

2
推荐指数
2
解决办法
2万
查看次数

Ruby 从实例方法调用类方法

从实例方法调用类方法的语法是什么?假设我有以下内容

class Class1
    def initialize
       #instance method
       self.class.edit
       puts "hello"
    end

    def self.edit
       #class method
       "ha"
    end
end

c= Class1
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,我没有得到任何输出。

ruby methods class-method instance-methods

2
推荐指数
1
解决办法
2670
查看次数

静态或不静态

我真的很喜欢使用静态方法(特别是对于helpers类).但是由于静态方法不是顽固的,最终它们是一种不好的做法,不是吗?所以我必须在静态方法使用方便性和可测试性之间做出选择.有任何妥协吗?

c# static-methods instance-methods

1
推荐指数
1
解决办法
785
查看次数

在Ruby/Rails中动态定义方法 - 如何设置params?

我试图定义一组函数,我可以传入给定的参数.

例如,我该怎么做?

>> get_1_type("xxx")

V4_RELATIONSHIP_TYPES=[1=>2,3=>4]

V4_RELATIONSHIP_TYPES.keys.each do |key|
 self.class.send(:define_method, "get_#{key}_type".downcase) do
  return GuidInfo.get_or_new(PARAMS, V4_RELATIONSHIP_TYPES[key])
 end
end

# i can call -> get_1_type("xxx") , and get the function called
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails dynamic-method instance-methods

1
推荐指数
1
解决办法
1712
查看次数

Java,继承和实例方法.为什么我的方法不会继承?

我是一名本科大学生,试图理解Java中的继承.docs.oracle站点表示类的所有成员都是继承的,但构造函数除外.这是有道理的.问题是我做了一个实验而且没有用.这里是:

public class One{
    public void outPrint(){
        System.out.println("Hello World!");
    }//end outPrint
}//end One.java

public class Two extends One{
    //empty
}//end Two.java

public class Three extends Two{
    public static void main(String[]args){
        outPrint();
    }//end main
}//end Three.java
Run Code Online (Sandbox Code Playgroud)

当我运行Three时,我得到:非静态方法outPrint()不能从静态上下文中引用.这当然是因为编译器将outPrint()视为实例成员.如果我将关键字"static"添加到outPrint()方法标题中,整个过程就可以了.

这就是我的困惑所在.它似乎不仅仅是不可继承的构造函数,而且还有它的所有实例成员.有人能解释一下这对我好一点吗?有没有涉及使用"静态"的解决方法?我尝试了一些"超级"的实验无济于事.提前致谢!

java inheritance static-methods extends instance-methods

1
推荐指数
1
解决办法
410
查看次数

实例方法是否在c#中没有创建对象引用

我正在使用c#中的asp.net Web应用程序.我有一个名为GetUser的公共类.在那个类中,我有一个名为GetCurrentUser的方法.方法如下:

    public MobileUser GetCurrentUser(MDMDataContext dc, string userCode)
    {
        using (dc)
        {
            dc.ObjectTrackingEnabled = false;
            var currentUser =
                from MobileUser in dc.MobileUsers
                where MobileUser.UserCode == usercode                     
                select MobileUser;

            MobileUser mu = new MobileUser();
            mu = currentUser.Single();

            return mu;
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用GetCurrentUser实例方法时,如下所示:

using (MDMDataContext dc = new MDMDataContext())
        {
            GetUser.GetCurrentUser(dc, "ABCD");
        }
Run Code Online (Sandbox Code Playgroud)

我收到以下错误,"非静态字段,方法或属性的对象引用' .... GetUser.GetCurrentUser ....'

但是如果我将static关键字添加到函数中,则错误就会消失.有人可以为我揭开这个概念的神秘面纱吗?

c# asp.net static-methods instance-methods

1
推荐指数
1
解决办法
137
查看次数