簡體   English   中英

Java:實例訪問和輸入System.in

[英]Java: instance access and input System.in

我必須根據實例提出問題,並使用System.in輸入。

首先是實例:

我創建了一個實例變量,名為woodenSword ,它具有:

Sword woodenSword=new Sword("Wooden Sword", 2);

public Sword(String nameSword, int damageSword){
        this.nameSword=nameSword;
        this.damageSword=damageSword;
        numberOfSwords++;
}

現在,我要訪問DamageSword,但是我該怎么做? 我嘗試了woodenSword.damageSword,但顯然不起作用...我認為那是因為我將變量設為private ,但是我不想更改它,因為我讀到某個地方最好將變量設為private (還有一個附帶的問題:為什么最好讓變量保持私有?)

另一個問題:我如何獲得System.in輸入? 是否必須使用System.in.toString()

我應該為此使用一個功能嗎? 要從類中獲取私有變量,然后將該函數放入類中? 我想到了這個功能:

public static int getSwordStats(String nameSword){
    damageSword=nameSword.damageSword;

}

但是,我在nameSword.damageSwordnameSword.damageSword錯誤,我認為它不理解這是一個變量...我該如何解決?

希望你能幫我!

您的劍班似乎負責跟蹤3件事情:

  1. 每把劍的名字
  2. 每把劍的傷害
  3. 劍總數

前兩個必須是成員變量,而最后一個必須是靜態變量。

public class Sword {
    // private (so no one can access it but Sword)
    // static (so it belongs to the class Sword and not any specific Sword)
    private static int numberOfSwords = 0; // initialize to 0
    // public accessor method
    public static int getNumberOfSwords() {
        return numberOfSwords;
    }
    // notice there's no "setNumberOfSwords" - no one can come along and change
    // our data - it's 'encapsulated' in the class

    private String name; // private
    private int damage; // private

    public Sword(String name, int damage) {
        this.name = name;
        this.damage = damage;
        numberOfSwords++; // the only place we change number of swords
    }

    // this is how people outside Sword access the name
    // note that we could add a "setName(String name)" if we want
    public String getName() {
        return name;
    }

    // same with name - protect and provide an accessor
    public int getDamage() {
        return damage;
    }
}

在以后的課程中,您現在可以執行以下操作:

Sword wood = new Sword("Wooden Sword", 2);
System.out.println("wood's name is " + wood.getName());
System.out.println("wood's damage is " + wood.getDamage());
System.out.println("swords crafted so far: " + Sword.getNumberOfSwords());

Sword mithril = new Sword ("Mithril Sword", 10);
System.out.println("mithril 's name is " + mithril .getName());
System.out.println("mithril 's damage is " + mithril .getDamage());
System.out.println("swords crafted so far: " + Sword.getNumberOfSwords());

哪個會打印

Wooden Sword
2
1
Mithril Sword
10
2

關於第二個問題,我相信有很多很棒的資源可以幫助您找到Google。 作為一個簡單的例子,這是我的工作:

// assumes you "import java.util.Scanner"
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()) {
    String line = sc.nextLine();
    System.out.println("You typed: " + line);
}

如果您需要從任何地方訪問劍的傷害,則應該有一個公共方法返回此信息:

public int getDamage() {
    return this.damageSword;
}

(請注意,我將方法命名為getDamage() ,而不是getDamageSword() 。該方法位於Sword類中。到處亂放劍是無用的,只會增加噪音,並使代碼的可讀性降低)。

關於第二個問題。 是的,System.in是標准輸入流。 toString()不會返回用戶輸入的內容。 閱讀該類的javadoc以了解其工作原理。 另請閱讀Java IO教程 ,其中包含有關命令行的部分。

關於最后一部分,您的代碼嘗試獲取String的損壞。 琴弦沒有損壞。 劍有傷害。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM