簡體   English   中英

如何使用來自另一個類的私有變量打印默認構造函數

[英]how to print the default constructor with private variables from another class

我想打印默認的私有bloodtyperhfactor ,這是O++我想從另一個具有main方法的類打印它。

我已經嘗試過創建新對象並打印它們,但它仍然說我正在訪問一個私有變量。 當您在scanner上輸入內容時,它會打印它,但如果您輸入任何內容,我想用私有變量打印構造函數!

public class blooddata {

    private String bloodtype;
    private String rhFactor;

    blooddata(){
        bloodtype = "O";
        rhFactor = "+";
    }

    blooddata(String btx, String rhx){
        this.bloodtype = btx;
        this.rhFactor = rhx;
    }

    public String getblood (String bloodtype){
        return bloodtype;
    }

    public String getfactor (String rhFactor){
        return rhFactor;
    }

    public void setblood(String bloodtype){
        this.bloodtype = bloodtype;
    }

    public void setfactor(String factor){
        this.rhFactor = factor;
    }
}

這是具有main方法的類

import java.util.Scanner;

public class Runblooddata {

    static Scanner sc = new Scanner(System.in);
    static String btx;
    static String rhx;

    public static void main(String[] args) {

        System.out.print("Enter blood type: ");
        btx = sc.nextLine();

        System.out.print("Enter rhFactor: ");
        rhx = sc.nextLine();

        if (btx.isEmpty() || rhx.isEmpty()){
           blooddata asd = new blooddata(); //this is where i am lost
        }else{   
            blooddata bd = new blooddata();
            bd.setblood(btx);
            bd.setfactor(rhx);

            System.out.println(bd.getblood(btx));
            System.out.println(bd.getfactor(rhx));
        }
    }
}

吸氣劑不應該有參數。

聲明名稱與字段相同的方法參數時,該參數會隱藏該字段。 你基本上返回你參數。

6.4.1。 陰影

在整個d范圍內,名為n shadows的字段或形式參數的聲明d,名稱為n的任何其他變量的聲明,這些變量在d出現的范圍內。

public String getBlood() {
    return bloodtype;
}

public String getFactor() {
    return rhFactor;
}

我會幫你簡化一下。

final class Example {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter blood type: ");
        String btx = sc.nextLine();

        System.out.print("Enter rhFactor: ");
        String rhx = sc.nextLine();

        BloodData data = btx.isEmpty() || rhx.isEmpty() ? 
                new BloodData() : 
                new BloodData(btx, rhx);

        System.out.println(data.getBloodType());
        System.out.println(data.getRhFactor());
    }
}

final class BloodData {

    private final String bloodType;
    private final String rhFactor;

    BloodData() {
        this("O", "+");
    }

    public BloodData(String bloodType, String rhFactor) {
        this.bloodType = bloodType;
        this.rhFactor = rhFactor;
    }

    public String getBloodType() {
        return bloodType;
    }

    public String getRhFactor() {
        return rhFactor;
    }
}

由於Andrew已經在您的代碼中解釋了設計問題,我將引導您找到您正在尋找的解決方案。

    blooddata bd = new blooddata();

    if (!btx.isEmpty() && !rhx.isEmpty()){

        bd.setblood(btx);
        bd.setfactor(rhx);
    }
    System.out.println(bd.getblood());
    System.out.println(bd.getfactor());

暫無
暫無

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

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