instance-variables

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

感情迁移 提交于 2019-11-28 10:26:49
问题 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

Problem in instance variable initialization

。_饼干妹妹 提交于 2019-11-28 09:33:07
Heres some sample code, class Base { private int val; Base() { val = lookup(); } public int lookup() { //Perform some lookup // int num = someLookup(); return 5; } public int value() { return val; } } class Derived extends Base { private int num = 10; public int lookup() { return num; } } class Test { public static void main(String args[]) { Derived d = new Derived(); System.out.println("d.value() returns " + d.value()); } } output: d.value() returns 0 // I expected 10 as lookup() is overridden, but not 0! can someone clarify this? The initialization of Derived 's instance variables has not

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

余生颓废 提交于 2019-11-28 07:41:35
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 to set the object's class variables that I collect from the user. private void button1_Click(object

Java ArrayList add() method in the instance variable section

情到浓时终转凉″ 提交于 2019-11-28 06:03:28
问题 In a Java class where you normally declare/define instance variables, I would like to have an ArrayList as one of the instance variables and initialize it with some elements to start out with. One way of doing this is declare the ArrayList and initialize it in a constructor. However, I am wondering why it is illegal to initialize the value outside the constructor. For example, public class Test { // some instance variables... private ArrayList<String> list = new ArrayList<String>(); list.add(

Object-Oriented Perl constructor syntax and named parameters

自闭症网瘾萝莉.ら 提交于 2019-11-28 04:23:08
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 variables called "High" and "Low". #But I'm not sure how $params{'High'} has any meaning, since it was an

How can Ruby's attr_accessor produce class variables or class instance variables instead of instance variables?

偶尔善良 提交于 2019-11-28 04:09:08
If I have a class with an attr_accessor , it defaults to creating an instance variable along with the corresponding getters and setters. But instead of creating an instance variable, is there a way to get it to create a class variable or a class instance variable instead? Like this: class TYourClass class << self attr_accessor :class_instance_variable end end You can look at this as opening the metaclass of the class (of which the class itself is an instance) and adding an attribute to it. attr_accessor is a method of class Class , it adds two methods to the class, one which reads the instance

Java - access private instance variables

人走茶凉 提交于 2019-11-28 02:23:27
问题 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() {

Object assignment in Ruby [closed]

一笑奈何 提交于 2019-11-28 01:52:25
问题 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 回答1: If you're familiar with C++, then you might want to consider every variable in Ruby, instance or otherwise, as a reference

Objective-C. Property for C array

二次信任 提交于 2019-11-28 01:37:55
问题 I need something like this: @property (nonatomic, retain) int field[10][10]; but this code doesn't work. How to replace it? I need both setter and getter methods 回答1: You can do it if you wrap the array in a struct. Structs are supported in @property notation (see CGRect bounds on CALayer, for example). First define your struct: typedef struct { int contents[10][10]; } TenByTenMatrix; Then, in your class interface, you can do: @property (assign) TenByTenMatrix field; Note that in this case,

Are synthesized instance variables generated as private instead of protected?

谁都会走 提交于 2019-11-28 01:26:15
问题 Since recent runtimes in iOS, we are able to define properties that will generate accessors for instance variables. From what I understand, it is not mandatory to declare the instance variable used since it will be automatically done for us. For example, if I write: @interface MyFirstClass @property (readonly, nonatomic) int size; @end and in the .m @implementation MyFirstClass @synthesize size; @end Then an instance variable named "size" will be added for me and a method called "-(int)size"