instance

Swift: Creating an Array with a Default Value of distinct object instances

人走茶凉 提交于 2019-11-26 05:54:38
问题 I noticed a bit weird ( and dangerous IMHO ) behavoir in Creating an Array with a Default Value . As stated in Swift 2.1: Collection Types Swift’s Array type also provides an initializer for creating an array of a certain size with all of its values set to the same default value. You pass this initializer the number of items to be added to the new array (called count) and a default value of the appropriate type (called repeatedValue): The point is: same default value ; in order to understand

What exactly is an instance in Java?

让人想犯罪 __ 提交于 2019-11-26 05:17:16
问题 What is the difference between an object, instance, and reference? They say that they have to create an instance to their application? What does that mean? 回答1: An object and an instance are the same thing . Personally I prefer to use the word "instance" when referring to a specific object of a specific type, for example "an instance of type Foo". But when talking about objects in general I would say "objects" rather than "instances". A reference either refers to a specific object or else it

super.onCreate(savedInstanceState);

不打扰是莪最后的温柔 提交于 2019-11-26 04:59:41
问题 I have created an Android Application Project and in MainActivity.java > onCreate() it is calling super.onCreate(savedInstanceState) . As a beginner, can anyone explain what is the purpose of the above line? 回答1: Every Activity you make is started through a sequence of method calls. onCreate() is the first of these calls. Each and every one of your Activities extends android.app.Activity either directly or by subclassing another subclass of Activity . In Java, when you inherit from a class,

How to copy/create derived class instance from a pointer to a polymorphic base class?

别等时光非礼了梦想. 提交于 2019-11-26 04:50:57
问题 I have been struggling with this kind of problem for a long time, so I decided to ask here. class Base { virtual ~Base(); }; class Derived1 : public Base { ... }; class Derived2 : public Base { ... }; ... // Copies the instance of derived class pointed by the *base pointer Base* CreateCopy(Base* base); The method should return a dynamically created copy, or at least store the object on stack in some data structure to avoid \"returning address of a temporary\" problem. The naive approach to

Can we create an instance of an interface in Java? [duplicate]

拟墨画扇 提交于 2019-11-26 04:06:50
问题 This question already has answers here : Can we create an object of an interface? (5 answers) Closed 4 years ago . Is it possible to create an instance of an interface in Java? Somewhere I have read that using inner anonymous class we can do it as shown below: interface Test { public void wish(); } class Main { public static void main(String[] args) { Test t=new Test() { public void wish() { System.out.println(\"output: hello how r u\"); } }; t.wish(); } } cmd> javac Main.java cmd> java Main

How can I tell if another instance of my program is already running?

荒凉一梦 提交于 2019-11-26 03:57:41
问题 How do i tell if one instance of my program is running? I thought I could do this with a data file but it would just be messy :( I want to do this as I only want 1 instance to ever be open at one point. 回答1: You can create a Semaphore and stop execution (put the code into your *.dpr file) and bring you running application to the screen. var Semafor: THandle; begin { Don't start twice ... if already running bring this instance to front } Semafor := CreateSemaphore(nil, 0, 1, 'MY_APPLICATION_IS

python class instance variables and class variables

混江龙づ霸主 提交于 2019-11-26 03:34:19
问题 I\'m having a problem understanding how class / instance variables work in Python. I don\'t understand why when I try this code the list variable seems to be a class variable class testClass(): list = [] def __init__(self): self.list.append(\'thing\') p = testClass() print p.list f = testClass() print f.list Output: [\'thing\'] [\'thing\', \'thing\'] and when I do this it seems to be an instance variable class testClass(): def __init__(self): self.list = [] self.list.append(\'thing\') p =

ES6: call class constructor without new keyword

隐身守侯 提交于 2019-11-26 02:10:01
问题 Given a simple class class Foo { constructor(x) { if (!(this instanceof Foo)) return new Foo(x); this.x = x; } hello() { return `hello ${this.x}`; } } Is it possible to call the class constructor without the new keyword? Usage should allow (new Foo(\"world\")).hello(); // \"hello world\" Or Foo(\"world\").hello(); // \"hello world\" But the latter fails with Cannot call a class as a function 回答1: Classes have a "class body" that is a constructor . If you use an internal constructor() function

Getting an instance name inside class __init__() [duplicate]

左心房为你撑大大i 提交于 2019-11-26 01:59:41
问题 This question already has an answer here: Getting the name of a variable as a string 17 answers While building a new class object in python, I want to be able to create a default value based on the instance name of the class without passing in an extra argument. How can I accomplish this? Here\'s the basic pseudo-code I\'m trying for: class SomeObject(): defined_name = u\"\" def __init__(self, def_name=None): if def_name == None: def_name = u\"%s\" % (<INSTANCE NAME>) self.defined_name = def

Java Static vs Instance

一世执手 提交于 2019-11-26 01:59:21
问题 So my coder friend hates using the static coding. Yet my Java program is full of it to link between classes, and I have a lot of them! Is it worth rewriting the whole code to remove the static method? Is there any advantage of using one over the other? 回答1: 1. An instance variable is one per Object , every object has its own copy of instance variable. Eg: public class Test{ int x = 5; } Test t1 = new Test(); Test t2 = new Test(); Both t1 and t2 will have its own copy of x . 2. A static