Java 如何使用另一个类的私有变量打印默认构造函数

Java 如何使用另一个类的私有变量打印默认构造函数,java,Java,我想打印默认的私有血型和rhfactor,它是O+,我想从另一个拥有main方法的类打印它 我已经尝试创建新对象并打印它们,但它仍然表示我正在访问一个私有变量。当你在扫描器上输入一些东西时,它会打印出来,但是如果你没有输入任何东西,我想打印带有私有变量的构造函数 公共类血液数据{ 私家血型; 私有字符串因子; 血液数据{ 血型=O; rhFactor=+; } blooddataString btx,字符串rhx{ 这个血型=btx; this.rhFactor=rhx; } 公共字符串getb

我想打印默认的私有血型和rhfactor,它是O+,我想从另一个拥有main方法的类打印它

我已经尝试创建新对象并打印它们,但它仍然表示我正在访问一个私有变量。当你在扫描器上输入一些东西时,它会打印出来,但是如果你没有输入任何东西,我想打印带有私有变量的构造函数

公共类血液数据{ 私家血型; 私有字符串因子; 血液数据{ 血型=O; rhFactor=+; } blooddataString btx,字符串rhx{ 这个血型=btx; this.rhFactor=rhx; } 公共字符串getblood字符串血型{ 回归血型; } 公共字符串getfactor字符串rhFactor{ 回报系数; } 公有血型{ this.bloodtype=血型; } 公共无效设置因子字符串因子{ 这个系数=系数; } } 下面是具有main方法的类

导入java.util.Scanner; 公共类运行数据{ 静态扫描仪sc=新的ScannerSystem.in; 静态字符串btx; 静态字符串rhx; 公共静态无效字符串[]args{ System.out.printEnter血型:; btx=sc.nextLine; System.out.printEnter rhFactor:; rhx=sc.nextLine; 如果btx.isEmpty | | rhx.isEmpty{ blooddata asd=new blooddata;//这就是我迷路的地方 }否则{ 血液数据bd=新的血液数据; bd.setbloodbtx; bd.setfactorrhx; System.out.printlnbd.getbloodbtx; System.out.printlnbd.getfactorrhx; } } }
getter不应该有参数

当您声明名为与字段相同的方法参数时,该参数将隐藏该字段。您基本上返回您获取的参数

一个名为n的字段或形式参数的声明d在d的整个范围内,隐藏了在d出现点的范围内任何其他名为n的变量的声明

我会帮你简化一下

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;
    }
}

getter不应该有参数

当您声明名为与字段相同的方法参数时,该参数将隐藏该字段。您基本上返回您获取的参数

一个名为n的字段或形式参数的声明d在d的整个范围内,隐藏了在d出现点的范围内任何其他名为n的变量的声明

我会帮你简化一下

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());

由于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());

为什么getter方法接受一个参数,然后直接返回?这没有任何意义,删除参数;还有一件事,请在if-else部分外弹出bd.getbloodbtx,否则它将始终打印您为else部分设置的数据。为什么您的getter方法接受一个参数,然后您只需返回该参数?这没有任何意义,删除参数;还有一件事,请将bd.getbloodbtx弹出if-else,否则它将始终打印您为else部分设置的数据。@cedie_asd在运行代码时告诉我,它是否可以正常工作expected@cedie_asd请告诉我您何时运行了代码,以及它是否如预期的那样工作。在meOP看来,btx.isEmpty | | | rhx.isEmpty更简单在我看来,isEmpty更简单