2014年1月2日星期四

Reference variable points can be more than you

After changing the member variable ( pointing to a reference variable ) in the parent class members object to call the method , a reference variable can also point two addresses do




public class Test {
String name = "Tom";
public Test (String name) {
name = name;
}
public static void main (String [] args) {
Test t = new Test ("Jack");
System.out.println (t.name);
}
}
A. null
B. Tom
C. Jack
D. ""
The correct answer : B

------ Solution ------------------------------------ --------
first teach how to paste code :

String str = "abc";

The first question : a reference can only point to an object , an object can be multiple references to .
The second question : In the constructor : name = name did not change the name value Test too , if it is this.name = name on it
------ Solution ---- ----------------------------------------
a variable can not point at the same time different address ,
in the constructor arguments with members of the same name , member name is hidden, so your constructor is equivalent to doing nothing
unless you change the name of the constructor parameter or use this.name to access members
public class Test {
String name = "Tom";

public Test(String otherName) {
name = otherName;
}

public static void main(String[] args) {
Test t = new Test("Jack");
System.out.println(t.name);
}
}


or
public class Test {
String name = "Tom";

public Test(String name) {
this.name = name;
}

public static void main(String[] args) {
Test t = new Test("Jack");
System.out.println(t.name);
}
}

------ Solution ------------------------------------- -------
you this does not change the member variable , Test method without any meaning.
want to change :
this.name = name;
this refers Test yourself
------ Solution ------------------------------ --------------
class member variables defined in saying no problem,
If what you say is private non-static variables defined , then use it to define variables and methods in the surface there is no difference :
Class 1 variables can be used throughout the class , do not pass parameters , which leads to a problem of concurrency problems , this variable is complicated by insecurity .
2 if you want to define a class variable , preferably immutable constant , so there would be no concurrency issues .
3 If you do not want to cause concurrency problems , where the variables are defined in the method just fine ( of course, lose subtle performance , but a million times / minute call is not affected).
4 certainly not as long as there is concurrency problems with how you define yourself .

------ Solution ------------------------------------ --------
in addition , class variables and methods can be variable with the same name , if not this distinction , java default method is to use the same name as the variable .
------ For reference only ---------------------------------------
not change the member variable references . because of the same name , member variables are local variables hidden, unless the use this.name = name
------ For reference only -------------------- -------------------
references and objects are on a multi- relationship
------ For reference only ------- --------------------------------
procedure BUG
------ For reference only ---------------------------------------
code wrong result in misleading its own
------ For reference only -------------------------------------- -
  The reply was deleted administrator at 2013-12-30 16:36:02

------ For reference only ---------------------------------- -----
nobody tucao LZ "correct answer B" right ?
------ For reference only -------------------------------------- -


The answer is right. No need tucao . To tucao place on the constructor name = name.
This is meaningless constructor will not change the name attribute constructed object.

answer another question the landlord , a reference can only point to an object . Change the point of reference objects , this reference can not point to the previous object.
------ For reference only -------------------------------------- -

  
The answer is right. No need tucao . To tucao place on the constructor name = name.   
This is meaningless constructor will not change the name attribute constructed object.   
  
answer another question the landlord , a reference can only point to an object . Change the point of reference objects , this reference can not point to the previous object.  

name = name that is assigned to their own , which is the temporary variable name
------ For reference only ------------------ ---------------------

    
The answer is right. No need tucao . To tucao place on the constructor name = name.     
This is meaningless constructor will not change the name attribute constructed object.     
    
answer another question the landlord , a reference can only point to an object . Change the point of reference objects , this reference can not point to the previous object.          
  
name = name that is assigned to their own , which is the temporary variable name  
I rub it down wrong - shame
------ For reference only --------------------- ------------------
certainly correct is C. Jack
code is 3 floor first.

error code landlord himself dizzy .
------ For reference only ------------------------ ---------------
but by the landlord of the code, is indeed a B, you engage with this play it , haha ​​
----- - For reference only ---------------------------------------




praise , thank you
public void Test (String name1) {
name2 = name;
}
name1 and name2 is a thing?
name is a class member variables Tom?
------ For reference only -------------------------------------- -





Thank you , the forum soon , name = name of the second name is a class member variables do ? Is the reason for the scope of the class member variables do ?


Thank wing
------ For reference only ------------------------------- --------
Thank you for your reply , that this is a problem, , , , after we have discussed with their own thinking I've got it ,


There is another problem : the definition of a member variable ( defined here not very accurate estimates ) in the body of a class and define a variable in a method has the same effect it?

seek professional answers .
------ For reference only -------------------------------------- -
name = name; => this.name = name;

没有评论:

发表评论