instance-variables

Visual C# - Access instance of object created in one class in another

▼魔方 西西 提交于 2019-11-27 02:00:37
问题 I apologize in advance with what will probably be a fairly easy/quick answer based on scope, but I've looked everywhere and am surprised to not come up with an answer. I have created a class called Soldier with roughly 100 class variables. I need a user to enter information and gradually build a Solider object over the course of several different class Forms (because there is too much data to collect on just one). I create an (empty) instance of a Soldier (tempSoldier) in Form1.cs and start

What is the difference between an instance and a class (static) variable in Java [closed]

情到浓时终转凉″ 提交于 2019-11-27 01:42:00
问题 The title of this question is actually a previous examination question and I am looking for clarification / an answer to it. Please note that I am learning Java and am becoming familiar with its syntax. I understand that this question may have been asked before and if so can someone please show me where I may access the question if possible? Also please accept my apologies if this is the case. To show that I have been researching this area, my own understanding is that instance variables

Object-Oriented Perl constructor syntax and named parameters

一笑奈何 提交于 2019-11-27 00:27:48
问题 I'm a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I don't get what this line does at all, but I always see it. Do I need it? my $type = shift; #I'm turning the array of inputs into a hash, called parameters. my %params = @_; #I'm making a new hash called $self to store my instance variables? my $self = {}; #I'm adding two values to the instance

Instance variables with underscore in Objective-C 2.0 and renaming with @synthetize leads to optimization warnings by the 'Analyze' tool of Xcode 4 [duplicate]

只愿长相守 提交于 2019-11-26 23:17:48
Possible Duplicate: How does an underscore in front of a variable in a cocoa objective-c class work? I'm using the same convention for instance variable and properties naming as shown by sebnow in his following answer: instance variable/ method argument naming in Objective C I copy paste his example code here: @interface Foo : NSObject { id _bar; } @property (nonatomic, retain) id bar; - (id) initWithBar:(id)aBar; @end @implementation Foo @synthesize bar = _bar; - (id) initWithBar:(id)aBar { self = [super init]; if(self != nil) { _bar = aBar; } return self; } @end In the implementation of some

Private ivar in @interface or @implementation

↘锁芯ラ 提交于 2019-11-26 20:34:36
问题 Is there any reason to declare a private ivar in @interface instead of @implementation ? I see code like this all over the internet (including documentation provided by Apple): Foo.h @interface Foo : NSObject { @private id _foo; } @end Foo.m @implementation Foo // do something with _foo @end The header file defines the public interface of a class, whereas a private ivar is... well... private. So why not declare it like this? Foo.h @interface Foo : NSObject @end Foo.m @implementation Foo {

class variables is shared across all instances in python? [duplicate]

左心房为你撑大大i 提交于 2019-11-26 20:29:52
This question already has an answer here: How to avoid having class data shared among instances? 8 answers I started coding in python a week ago, it is my mistake i started coding using oops,classes and objects that soon. I assumed my C++ proficiency will help.... I got bit by the following code class A: var=0 list=[] def __init__(self): pass Here to my surprise, var and list are kinda global variable, it is shared across all instances it seems.... What I thought was it was different across all the instances..... It took me half a day to figure out that.... It does not make even slightest

Java -What is an instance variable? [closed]

六月ゝ 毕业季﹏ 提交于 2019-11-26 20:08:11
My assignment is to make a program with an instance variable, a String, that should be inputed by user. But I don't even know what an instance variable is. What is an instance variable? How do I create one? What does it do? Instance variable is the variable declared inside a class, but outside a method: something like: class IronMan{ /** These are all instance variables **/ public String realName; public String[] superPowers; public int age; /** Getters / setters here **/ } Now this IronMan Class can be instantiated in other class to use these variables, something like: class Avengers{ public

Using Instance Variables in Class Methods - Ruby

回眸只為那壹抹淺笑 提交于 2019-11-26 19:25:50
问题 I have a class something like below, and I used instance variables (array) to avoid using lots of method parameters. It works as I expected but is that a good practice? Actually I wouldn't expect that worked, but I guess class methods are not working as static methods in other languages. class DummyClass def self.dummy_method1 @arr = [] # Play with that array end def self.dummy_method2 # use @arr for something else end end 回答1: The reason instance variables work on classes in Ruby is that

How to make instance variables private in Ruby?

拥有回忆 提交于 2019-11-26 19:18:53
问题 Is there any way to make instance variables "private"(C++ or Java definition) in ruby? In other words I want following code to result in an error. class Base def initialize() @x = 10 end end class Derived < Base def x @x = 20 end end d = Derived.new 回答1: Like most things in Ruby, instance variables aren't truly "private" and can be accessed by anyone with d.instance_variable_get :@x . Unlike in Java/C++, though, instance variables in Ruby are always private. They are never part of the public

Difference between self.var and simply var

随声附和 提交于 2019-11-26 19:08:52
What is the difference between using self.var vs. just var in an Objective-C class? Are there benefits or dangers to one or the other? foo = self.var; self.var = foo; is conceptually identical to foo = [self var]; [self setVar: foo]; So using dot notation, you are really sending messages to self. foo = var; var = foo; is conceptually the same as foo = self->var; self->var = foo; So not using dot notation to access an instance variable is the same as treating self as a pointer to a C struct and accessing the struct fields directly. In almost all cases, it is preferable to use the property