标签: instance

在Python中打印实例

class Complex:def init(self,realpart,imagpart):self.real = realpart self.imag = imagpart print self.real,self.imag

我得到这个输出:

>>> Complex(3,2)
3 2
<__main__.Complex instance at 0x01412210>
Run Code Online (Sandbox Code Playgroud)

但为什么他打印最后一行?

python class instance python-idle

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

如何访问本机对象调用的函数中的实例?

我承认我已经问了一个关于this关键字及其在某些情况下意味着什么的问题,但我仍然不完全理解它,我害怕.任何提示将不胜感激.

我基本上有一个Socket类,它是HTML5的WebSocket类的包装器,使事情变得更容易(我只复制了与我的问题相关的内容):

var Socket = function(url) {
    this.url = url;
    this.webSocket = null;
}

Socket.prototype.init = function() {
    this.webSocket = new WebSocket(this.url);
    this.webSocket.onmessage = this.parseMessage;
};

Socket.prototype.parseMessage = function(event) {
    console.log(this); // logs the WebSocket (native) instance
}
Run Code Online (Sandbox Code Playgroud)

在parseMessage函数中,this引用本机WebSocket对象的实例,那么如何Socket在此处访问实例到我的对象?

非常感谢.

javascript this instance

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

检查List是否包含与给定实例相等的实例

我不想遍历列表比较每个属性.具有此功能的东西:

class myClass
{
    public int I { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        List<myClass> l = new List<myClass>();

        myClass x = new myClass();
        x.I = 1;
        l.Add(x);

        myClass y = new myClass();
        y.I = 2;
        l.Add(y);

        myClass z = new myClass();
        z.I = 2;

        if (l.ContainsAnInstanceEqualTo(z))
            Console.WriteLine("Contains");

        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

重要提示:我无法控制该类,并且有一些属性应该具有相同的值.

c# instance

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

如果我声明一个没有名字的类实例,它会留在内存中吗?

我有以下代码(我将跳过包含和你有什么):

main.cpp中

std::vector <actor> actors;
int main() {
    actor (2, 3);
}
Run Code Online (Sandbox Code Playgroud)

actor.h

class actor
{
public:
    int x, y;
    actor(int, int);
};
Run Code Online (Sandbox Code Playgroud)

actor.cpp

extern std::vector <actor> actors;
actor::actor(int x, int y)
{
    actor::x = x;
    actor::y = y;
    actors.push_back(*this);
}
Run Code Online (Sandbox Code Playgroud)

请问行:

    actor (2, 3)
Run Code Online (Sandbox Code Playgroud)

实际上创建一个演员的实例,除了被推到演员矢量的那个?

注意:如果代码没有编译,请忽略任何错误.实际的代码会编译,但实际上粘贴在这里太复杂了.

c++ class vector member instance

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

Java - 当我们在自己的类中创建类型类的数据类型时会发生什么?

我正在研究链表.我一次又一次地看到代码在该类的主体中声明了类的数据类型.以下是破解编码访谈(创建链表)一书中的一个例子

class Node {
    Node next = null;
    int data;
    public Node(int d) { data = d; }
    void appendToTail(int d) {
        Node end = new Node(d);
        Node n = this;
        while (n.next != null) { n = n.next; }
        n.next = end;
    }
}
Run Code Online (Sandbox Code Playgroud)

在第二行 - 正如您所看到的,'next'是Node类型的实例变量,是它自己的类.

我的问题是,如果我创建一个Node类的实例(比如X) -

Node X = new Node(10);
Run Code Online (Sandbox Code Playgroud)

书中说,编译器会自动为其实例变量(数据和下一个)保留空间,即使它们没有被启动.(int data = 10)在构造函数中清楚地处理,但接下来会发生什么?在我看来,X将在其中包含无限数量的"下一个"节点,因为X将在其中有一个名为next的节点.然后,因为每个节点都应该有一个数据和下一个部分,然后下一个将拥有自己的数据副本,然后在其中,依此类推......等等...

在此输入图像描述

java class linked-list object instance

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

导入haskell模块说"不在范围内"

我在名为Tree2.hs的文件中创建了一个Tree结构

module Tree2
(
 Tree
) where

data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
Run Code Online (Sandbox Code Playgroud)

然后我导入它并尝试将其用作类的实例

import qualified Tree2

class YesNo a where
  yesno :: a -> Bool

instance YesNo (Tree2.Tree a) where
  yesno EmptyTree = False
  yesno _ = True
Run Code Online (Sandbox Code Playgroud)

但是我在ghci中加载时遇到此错误:

Not in scope: data constructor ‘EmptyTree’
Failed, modules loaded: Tree2.
Run Code Online (Sandbox Code Playgroud)

谁知道为什么?

haskell class instance

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

Python类实例是重复的属性

对OOP来说相对较新,只是试图为游戏制作NPC类,除非指定,否则应根据随机值生成不同的NPC实例.真的只是概念验证.

然而,年龄,性别和种族属性由于某种原因而重复,即使它们是由应该返回随机值的函数生成的.

我已经单独测试了这些功能,他们正在做他们应该做的事情而且并不复杂.

class NPC:
    def __init__(self, gender = rand_gender(), race = rand_race(),
                 age = rand(16, 70),name = "", height = 0):
        self.gender = gender
        self.race = race
        self.age = age
        if name == "":
           self.name = name_gen(self.gender)
        else: self.name = name
        if height == 0:
            self.height = height_gen(self.race)
        else: self.height = height

def rand(min, max):
    return random.randrange(min, max)

def rand_gender():
    genders = ["Male", "Female"]
    random.shuffle(genders)
    return genders[0]

def rand_race():
    races = ["Human", "Dwarf", "Elf", "Orc"]
    random.shuffle(races)
    return races[0]

npc1 …
Run Code Online (Sandbox Code Playgroud)

python class duplication instance duplicates

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

python dict,list对所有类都有相同的实例

我从最近3个月开始研究python 2.7,今天注意到这一点后我感到非常惊讶.

对于以下代码段

class Example:
    children = {}

instance1 = Example()
instance1.children['instance1_child1'] = 'Instance 1 child 1'

instance2 = Example()
instance2.children['instance2_child1'] = 'Instance 2 child 1'
for key, value in instance2.children.items():
    print key + ' -> ' + value
Run Code Online (Sandbox Code Playgroud)

为什么输出

instance1_child1 -> Instance 1 child 1
instance2_child1 -> Instance 2 child 1
Run Code Online (Sandbox Code Playgroud)

对于我正在创建的所有对象,似乎只有一个dict()实例.我也检查了list [],他们的行为也是一样的.

我无法理解python的这种行为以及它背后的逻辑.这是某种设计缺陷还是我做错了什么并且应该退出编程?

python dictionary class list instance

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

Ruby动态创建方法和变量

如何用更合理的东西替换add_entry方法?

class MyStorageClass

    def add_entry key, value
        eval "(@#{key} ||= []) << value; def #{key}; @#{key}; end"
    end

end
Run Code Online (Sandbox Code Playgroud)

那么我可以按如下方式检索值:

def get_entry key
    begin
        self.send key.to_sym
    rescue NoMethodError
        nil
    end
end
Run Code Online (Sandbox Code Playgroud)

ruby variables methods dynamic instance

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

当一个类继承Python中的`list`时,如何获得该列表的实例?

我正在尝试编写堆结构,并且我意识到我在索引和设置项目方面实现的大部分内容已经以.的形式实现list.如何获取该列表的实例,以便将其用于堆构建?

这就是我所知道的

class heap(list):
    def __init__(self, arr=None, key=None):
        super().__init__(arr)
        self._arr = arr
Run Code Online (Sandbox Code Playgroud)

我想self._arr成为上面直线上创建的列表的实例.

python inheritance list instance

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