标签: instance

Ruby中的SingletonMethods和InstanceMethods是什么?

我看到了很多东西

include ActiveRecord::XXXX::InstanceMethods
extend ActiveRecord::XXXX::SingletonMethods
Run Code Online (Sandbox Code Playgroud)

我不知道财产或工作,只是想要一个容易理解的答案.如果它有充分的理由被使用.

ruby singleton class instance

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

如何将Activator.CreateInstance返回的对象转换为它转换的类型?

在下面的代码中,是否可以将x转换为您传入的类型Activator.CreateInstance而不知道它是什么时候提前?我尝试过,typeof...但这不起作用.

var testClasses = AppDomain.CurrentDomain.GetAssemblies()
                  .Single(a=>a.FullName.StartsWith("VerifyStuff")).GetTypes()
                  .Where(t=>t.UnderlyingSystemType.Name.StartsWith("VerifyXXX"));

var x = Activator.CreateInstance(testClasses.ElementAt(0));
Run Code Online (Sandbox Code Playgroud)

谢谢!

types object instance

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

在JS中减慢.push()间隔

player.fire是一个布尔值,如果空格键在,则返回true.一切都按预期工作. keydown

我遇到的问题是控制阵列asteroids.firing.push间隔.现在,它添加了一些实例,即使我轻轻点击空格键.我在用requestAnimationFrame.

我该如何控制push间隔?

    if(player.fire){
        var angle = thisShip.rot, 
        hyp = 10; //speed

        var vX = Math.cos(angle) * hyp, 
        vY = Math.sin(angle) * hyp;

        asteroids.firing.push(new asteroids.model.fire(thisShip.x, thisShip.y, vX, vY));
    }
Run Code Online (Sandbox Code Playgroud)

我尝试过类似的东西,但它不会减慢push间隔时间,而只会产生脉冲效果,一次又一次地重新生成点火序列.

    ...
    if(player.fire){
            fireInterval(thisShip);
    }

    function fireInterval(thisShip){
        var angle = thisShip.rot, 
            hyp = 10; //speed

        var vX = Math.cos(angle) * hyp, 
        vY = Math.sin(angle) * hyp;

        asteroids.firing.push(new asteroids.model.fire(thisShip.x, thisShip.y, vX, vY));

        setTimeout(function(){
            fireInterval(thisShip); …
Run Code Online (Sandbox Code Playgroud)

javascript arrays instance

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

我希望能够动态地向python类添加函数,而不是事先知道它们的名字

假设我有一个名单.我希望能够动态地将这些实例函数添加到类实例中.我知道类型.MethodType但我从这里到这里有点新手.基本上我想做的是:

class foo( object ):
     def __init__(self):
         pass

f = foo()
names = ["a","b","c"]
for name in names:
    add name() to f  # not sure what to do here
    # what I wanted added to instance "f" is this for each name:
    def name(self, *args, **kwargs):
        print( "My name is %s" % inspect.stack()[0][3]   )       
        print( "__called, args=%r, **kwargs=%r" % (args, kwargs) )

f.a() # ==> calls f.a()
f.b(1,2,3) # calls f.b(1,2,3 )and so on
Run Code Online (Sandbox Code Playgroud)

python methods class dynamic instance

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

c ++函数中的新实例

我有这个问题:

void myFunc()
{
  MyClass * myInstance = NULL;

  newInstance(myInstance);
}

void newInstance(MyClass * instance)
{
  instance = new MyClass(...);
}
Run Code Online (Sandbox Code Playgroud)

代码似乎工作正常,但是当我退出newInstance函数时,我的myInstance为null,而不是newInstance中的值...问题出在哪里?

谢谢

c++ pointers class instance

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

Java:如何检查实例所属的对象?

如果这个问题已经得到回答,我很抱歉,虽然我确实有一个广泛的外观,但无法找到答案.总结一下情况我试图创建一个模拟器程序,处理不同的捕食者和猎物生物,目前有让每个生物检查它旁边的生物类型的问题,我更愿意检查是否instance属于同一个对象.

所以说例如我做了这个:

private class Creature {
...
Creature [] fish = new Creature();
Creature [] shark = new Creature();
Creature [] penguin = new Creature();
}
Run Code Online (Sandbox Code Playgroud)

然后在循环中创建每个类型的几个实例(生物),如下所示:

for (int f=1;f<rnd;f++) {
fish[f] = new Creature();
//set attributes of creature
Run Code Online (Sandbox Code Playgroud)

然后程序可以告诉他们相对于彼此的位置我创建了一个网格系统,如下所示:

Creature [][] gridloc = new Creature[x][y]; //relates to number of spaces tiles that determines movement.
Creature [] crloc = new Creature[tc]; //stores a reference to all creatures created.
...

crloc[tc] = fish[f]; gridloc[x][y]=crloc[tc] //or fish[f]
}
Run Code Online (Sandbox Code Playgroud)

无论如何总结甚至我在那里总结了相当多的代码,这一切都有效,但是当让每个生物在gridloc中检查旁边有什么例如捕食者时我不确定是否找到另一个生物来检查确定这是同一对象类型的实例还是不同的实例.所以类似于:

if (!gridloc[x][y].getObject().equals(gridloc[x+1][y].getObject()) //if …
Run Code Online (Sandbox Code Playgroud)

java arrays equals object instance

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

类本身内部的实例

此代码有效:

class Person{
    public Person p;
    public string name;
    private int age;

}
class Solution {


    static void Main(String[] args) {

        Person z = new Person () { name = "Stacl"};
        Console.WriteLine (z.name);
        Person a = new Person ();
        Console.WriteLine (a.name);

    }
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用:

class Person{
    public Person p = new Person (){name = "Inside",age = 45}; // add 'new'
    public string name;
    private int age;

}
class Solution {


    static void Main(String[] args) {

        Person z = new Person …
Run Code Online (Sandbox Code Playgroud)

c# oop instance

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

定义自己的实例类

我很难为我的数据类型创建自己的实例.我定义了一个类型:

data Breakfast = Egg | Sausage Int | Bread Breakfast deriving (Eq, Show)

并希望它是Ord类的一个实例.我想通过一些规则来比较它:一个鸡蛋和2个香肠等一样好.

我试过这样的:

instance Ord a => Ord (Breakfast) where
 compare (Egg) (Sausage 2) = EQ 
 ...
Run Code Online (Sandbox Code Playgroud)

但我得到错误:变量a比实例头更频繁地发生.我尝试了另一个例子,这很好用:

data Down a = Down a deriving (Eq, Show, Read)

instance Ord a => Ord (Down a) where
 compare (Down x) (Down y) = y `compare` x
Run Code Online (Sandbox Code Playgroud)

希望你们能帮助我什么是错的.我对Haskell很新.谢谢

haskell class instance

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

通过关系提取连接两个实例的实例链

我想通过询问SPARQL查询来提取我的本体的两个实例之间的实例链.例如,在下图中,如果我想知道A如何连接到E,则查询的结果应该类似于A,B,D,F,E的列表.

在此输入图像描述

应该如何设计本体并建立查询?它甚至可能吗?

rdf ontology instance sparql protege

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

如何从列表Python创建类实例

我正在为一个学校项目制作一个程序.这是模拟联合国.

countries = ['Australia', 'Brazil', 'Canada']

class delegate(object):
   def __init__(self, country):
     self.country = country
Run Code Online (Sandbox Code Playgroud)

我有一个国家列表和一个名为delegate的类.因此,每个委托对象必须有一个国家/地区.通常我会这样做:

mexico = delegate('Mexico')
Run Code Online (Sandbox Code Playgroud)

就是这样.但我想循环遍历国家/地区列表并为每个列表创建类实例.我的意思是:

australia = delegate('Australia')
brazil = delegate('Brazil')
canada = delegate('Canada')
Run Code Online (Sandbox Code Playgroud)

等等. 我该怎么做? 非常感谢你!!

python string class instance python-2.6

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