instance-variables

Object of the class as instance variable inside the class [duplicate]

℡╲_俬逩灬. 提交于 2019-11-30 02:09:15
Possible Duplicate: How can a class have a member of its own type, isnt this infinite recursion? The Code: public class Test2{ private Test2 subject = new Test2(); //Create Test2 object in Test2 private int num; } The Questions: Why does Java permit the above code to be executed, but C++ doesn't? Does the code above create infinite number of objects? Since Test2 itself contains a Test2 object which again contains a Test2 object which itself has a Test2 object and so on. The key difference between the two languages regarding your problem is that Java is a language with reference semantics (with

Ruby class instance variables and inheritance

非 Y 不嫁゛ 提交于 2019-11-30 01:52:42
I have a Ruby class called LibraryItem . I want to associate with every instance of this class an array of attributes. This array is long and looks something like ['title', 'authors', 'location', ...] Note that these attributes are not really supposed to be methods, just a list of attributes that a LibraryItem has. Next, I want to make a subclass of LibraryItem called LibraryBook that has an array of attributes that includes all the attributes of LibraryItem but will also include many more. Eventually I will want several subclasses of LibraryItem each with their own version of the array

How do I set an attr_accessor for a dynamic instance variable?

喜你入骨 提交于 2019-11-29 20:56:49
I dynamically created an instance variable within my class: class Mine attr_accessor :some_var def intialize @some_var = true end def my_number num self.instance_variable_set "@my_#{num}", num end end How do I make @my_#{num} now as an attr value? e.g. I want to be able to do this: dude = Mine.new dude.my_number 1 dude.my_1 => 1 this answer doesn't pollutes the class space, example.. if i do mine.my_number 4 then the other instances of Mine will not get the my_4 method.. this happens because we use the singleton class of the object instead of the class. class Mine def my_number num singleton

What is the correct (or best) way to subclass the Python set class, adding a new instance variable?

大城市里の小女人 提交于 2019-11-29 17:29:17
问题 I'm implementing an object that is almost identical to a set, but requires an extra instance variable, so I am subclassing the built-in set object. What is the best way to make sure that the value of this variable is copied when one of my objects is copied? Using the old sets module, the following code worked perfectly: import sets class Fooset(sets.Set): def __init__(self, s = []): sets.Set.__init__(self, s) if isinstance(s, Fooset): self.foo = s.foo else: self.foo = 'default' f = Fooset([1

How to have access to instance members in a static method?

主宰稳场 提交于 2019-11-29 16:57:26
I'm trying to create classes to encapsulate validation and logic for objects like Email , URL , phone number and so on . in the first try I found that I'm repeating the same code in all classes specially static IsValid and the Constructor . so I decided to create a base class to put all the same codes in it . so there is a base class that other classes inherit it . it's abstract as I don't want it to be used directly . public abstract class BaseClass { protected string value; private bool isValid; public bool IsValid{get { return this.isValid;}} protected virtual string RegexPattern{get;}

Which run first? default values for instance variables or Super Constructors?

淺唱寂寞╮ 提交于 2019-11-29 11:28:07
According to the SCJP6 (Page 507) i found that instance variables are assigned default values before the superclass constructors complete, i tried an example in Debugg mode but i saw that the super contractor runs before instance variables get their default values, could any one explain that to me ? Example i used in case someone want to try it: package courseExercise; class test { test() { System.out.println("Super Constructor run"); } } public class Init extends test { private Integer i = 6; private int j = 8; Init(int x) { super(); System.out.println("1-arg const"); } Init() { System.out

Hide instance variable from header file in Objective C

给你一囗甜甜゛ 提交于 2019-11-29 10:06:57
问题 I came across a library written in Objective C (I only have the header file and the .a binary). In the header file, it is like this: @interface MyClass : MySuperClass { //nothing here } @property (nonatomic, retain) MyObject anObject; - (void)someMethod; How can I achieve the same thing? If I try to declare a property without its corresponding ivar inside the interface's {}, the compiler will give me an error. Ultimately, I want to hide the internal structure of my class inside the .a, and

Why instance variable in Servlet is not thread-safe [duplicate]

痴心易碎 提交于 2019-11-29 09:44:34
问题 This question already has answers here : How do servlets work? Instantiation, sessions, shared variables and multithreading (8 answers) Closed 3 years ago . When I read Head First Servlet and JSP , they say that instance variable is non-thread safe. I don't understand this statement so much. For example: I have a servlet which name is ActionServlet.java . Each time, each user's request is sent to server, container will create a new thread and create new ActionServlet instance. ActionServlet

Java - access private instance variables

删除回忆录丶 提交于 2019-11-29 08:57:46
I need to access the private variables from the following class listing (Species.java) in order to use them in the KlingonOx.java class. The purpose of the KlingonOx.java class is to determine after how many years the population of the Elephant species will be larger than the population of the Klingon Ox species. Here is the Species.java class: import java.util.Scanner; public class Species { private String name; private int population; private double growthRate; public void readInput() { Scanner keyboard = new Scanner(System.in); System.out.println("What is the species' name?"); name =

Object assignment in Ruby [closed]

只愿长相守 提交于 2019-11-29 08:25:40
Coming from a c++ background I'm curious about object assignment in Ruby. What considerations (if any) should be made for the following object assignments: class MyClass attr_accessor :a, :b def initialize(a, b) @a = a @b = b end def some_method puts "#{self.a} #{self.b}" end end m = MyClass.new("first", "last") n = MyClass.new("pizza", "hello") q = n q.some_method If you're familiar with C++, then you might want to consider every variable in Ruby, instance or otherwise, as a reference to another object. Since everything in Ruby is an object, even nil , which is of type NilClass, this holds