2015-09-30 53 views
0
import java.util.Scanner; 

class Description 
{ 
String name = "Pablo"; 
public void getName(String newName) throws InterruptedException 
{ 
    System.out.println("Your name is "); 
    name = newName; 
    System.out.println(newName); 
    Thread.sleep(2000); 
    System.out.println("However, the creator of this crappy program is "); 
    System.out.println(this.name); 
} 

} 

public class Learning { 
    public static void main(String[] args) throws InterruptedException 
    { 
     System.out.println("Description of yourself"); 
     Scanner scan = new Scanner(System.in); 
     Thread.sleep(1000); 
     System.out.println("Please type your name"); 
     Description person = new Description(); 
     String myName = scan.nextLine(); 
     person.getName(myName); 
    } 
} 

我对Java相当陌生。我正在读关于二传手和这个。我希望它能够在代码的这一部分输出“Pablo”。this.name不输出所需的字符串

public void getName(String newName) throws InterruptedException 
{ 
    System.out.println("Your name is "); 
    name = newName; 
    System.out.println(newName); 
    Thread.sleep(2000); 
    System.out.println("However, the creator of this crappy program is "); 
    System.out.println(this.name); 
} 

但是,可以说,我输入“史蒂夫”,在它应该参数传递给方法GetName,这是确实的代码

String myName = scan.nextLine(); 

的这一部分。然而,当我到

 System.out.println(this.name); 

它打印出“史蒂夫”,而应该打印出“巴勃罗的一部分。因此,控制台看起来像

Description of yourself 
Please type your name 
Steve 
Your name is 
Steve 
However, the creator of this crappy program is 
Steve 

难道我做错了什么?

+6

你觉得'name = newName;'是吗? – Titus

+0

我想你对'name'变量的范围感到困惑。 – ochi

回答

1

它打印出“史蒂夫”,而它应该打印出“巴勃罗。所以控制台看起来像

否。在您的getName方法中,您将名称更改为给定名称。

name = newName; 

因此你name没有更多的运送"Pablo",并从那里开始有一个值"Steve"

+0

但我想用户this.name引用第一个String name =“Pablo”? – Pablo

+0

@Pablo是的,但后来你改变了名字在这里'name = newName; “不是吗? –

+0

是的,我改变了这个值,但是我在方法中做了那个?我想我可能会对使用this.name – Pablo

1

这是因为你有这条线

name = newName; 

您使用this.name在打印之前更换了newName名称值。当你打电话this.name它打印替换值。

只是以前了newName不是更换名字,你就大功告成了打印名称后,:

public void getName(String newName) throws InterruptedException 
{ 
    System.out.println("Your name is "); 
    System.out.println(newName); 
    Thread.sleep(2000); 
    System.out.println("However, the creator of this crappy program is "); 
    System.out.println(this.name); 
    name = newName; 
} 

当调用类的方法(函数),this.attribute_name等于只attribute_name

1

我希望我的回答能让你对变量的范围有所了解。

解释在内嵌评论中找到。

让我知道如果你不明白一些评论。

import java.util.Scanner; 

public class Learning { 
    Description description; 

    public Learning(){ 
     description = new Description(); 
    } 

    /* app entry point */ 
    public static void main(String[] args) throws InterruptedException 
    { 
     System.out.println("Description of yourself"); 
     Scanner scan = new Scanner(System.in); 
     Thread.sleep(1000); 
     System.out.println("Please type your name"); 
     Learning person = new Learning(); 

     String myName = scan.nextLine(); 
     person.description.getName(myName); 
    } 

    public class Description { // (0) 
     String name = "Pablo"; // name scope is from (0) to (1) so it can accessible from these places (*) 

     public String getName() { 
      // (*) here 
      return name; 
     } 

     public void setName(String name) { 
      // (*) here 
      this.name = name; 
     } 

     // newName has a reduced scope, so it can only be reached inside this method 
     // its scope goes from (2) to (3) 
     public void getName(String newName) throws InterruptedException{ // (2) 
      // (*) here 
      System.out.println("Your name is "); 
      // this line modifies the name outside of this method (which is where it was defined) 
      name = newName; 
      System.out.println("A: " + newName); 
      System.out.println("B: " + name); 

      // this line modifies the localName (since it was defined in this method) but not the one outside 
      // (because it is a new variable bound to this method scope) 
      String name = "RandomName"; 
      System.out.println("C: " + newName); 
      System.out.println("D: " + name); 

      Thread.sleep(2000); 
      System.out.println("However, the creator of this crappy program is "); 
      // this refers to the class (Description) not the scope so 
      // this.name is the same as name in A: but not the same as the local-scoped name 
      System.out.println("E: " + this.name); 
      System.out.println("F: " + name); 
     } // (3) 
    } // (1) 
}