簡體   English   中英

構造函數混淆Java

[英]Constructor confusion Java

嗨,這似乎是一個非常愚蠢的問題,但是我最近接觸了Java,並且自學了有關構造函數的知識。

public class creatures {
    private static String name;
    private static int age;
    private static String type;

    public creatures( String name, int age, String type) {
        this.name = name;
        this.age = age;
        this.type = type;
        System.out.println("The creature's name is " + name + " \nThe creatures age is"  + age + " \nThe creatures type is " + type);
    } 

    public  static void main(String [] args) {
        creatures newcreature = new creatures("Zack", 100, "alien");
        creatures newcreature1 = new creatures("Jonny", 500, "vampire");
        creatures newcreature2 = new creatures("Dick", 4, "witch");
        System.out.println(newcreature.name);
    }
}

因此,在我的主要方法中的system.out.println中,在構造函數被打印之后,我想通過引用我的newcreature構造函數的名稱來打印名稱“ Zack”,但是它只是從最后一個構造函數中打印名稱“ Dick”我做的。 如何區分同一個類中的這些構造函數? 再次抱歉,如果這是一個愚蠢的問題。

問題出在變量上的static關鍵字上。

閱讀: 輸入鏈接描述

靜態變量只會獲得一次內存,如果任何對象更改了靜態變量的值,它將保留其值。

因為您的名稱字段是靜態的,所以它共享一個公共內存。因此,如果您嘗試通過用不同的對象來對其進行修飾來訪問它,它將給出相同的輸出。

自上次更改值以來, new creatures("Dick", 4, "witch"); Dick將改變它。

因此,刪除static關鍵字即可獲得所需的o / p

public class creatures {
    private String name;
    private int age;
    private String type;

    public creatures( String name, int age, String type) {
        this.name = name;
        this.age = age;
        this.type = type;
        System.out.println("The creature's name is " + name + " \nThe creatures age is"  + age + " \nThe creatures type is " + type);
    } 

    public  static void main(String [] args) {
        creatures newcreature = new creatures("Zack", 100, "alien");
        creatures newcreature1 = new creatures("Jonny", 500, "vampire");
        creatures newcreature2 = new creatures("Dick", 4, "witch");
        System.out.println(newcreature.name);
    }
}

產量

Zack

類的所有數據成員都是靜態的,這就是每個實例共享同一成員的原因。 每當您創建生物的新實例時,構造函數都將用新值覆蓋舊值。

在您的代碼中:

private static String name; 
private static int age;
private static String type;

在生物,生物1,生物2之間共享。

刪除靜態關鍵字。

public class creatures {
 private String name;
 private int age;
 private String type;

 public creatures(String name, int age, String type) {
    this.name = name;
    this.age = age;
    this.type = type;
    System.out.println("The creature's name is " + name
            + " \nThe creatures age is" + age + " \nThe creatures type is "
            + type);
 }

 public static void main(String[] args) {
    creatures newcreature = new creatures("Zack", 100, "alien");
    creatures newcreature1 = new creatures("Jonny", 500, "vampire");
    creatures newcreature2 = new creatures("Dick", 4, "witch");
    System.out.println(newcreature.name);
 }

}

您的字段nameagetype是靜態的。 這意味着它們被您所有的生物共享。 因此,您不能說“此生物的名稱為...”,因為該生物在您的代碼中沒有名稱。 按照其編寫的方式,您只能說“該生物類具有此名稱...”,在Java中是這樣寫的creatures.name=...

因此,您需要從字段中刪除該static修飾符。

暫無
暫無

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

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