2015-09-30 52 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) 
}