简体   繁体   English

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

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

I want to print the default private bloodtype and rhfactor which is O+ and + I want to print it from another class which has the main method. 我想打印默认的私有bloodtyperhfactor ,这是O++我想从另一个具有main方法的类打印它。

I've already tried creating new objects and printed them but still it says that I'm accessing a private variable. 我已经尝试过创建新对象并打印它们,但它仍然说我正在访问一个私有变量。 When you input something on the scanner it prints it but if you input none I want to print the constructor with private variables! 当您在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;
    }
}

here is the class that has main method 这是具有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));
        }
    }
}

Getters aren't supposed to have parameters. 吸气剂不应该有参数。

When you declare a method parameter named the same as a field, the parameter hides the field. 声明名称与字段相同的方法参数时,该参数会隐藏该字段。 You basically return the parameter you take. 你基本上返回你参数。

6.4.1. 6.4.1。 Shadowing 阴影

A declaration d of a field or formal parameter named n shadows, throughout the scope of d, the declarations of any other variables named n that are in scope at the point where d occurs. 在整个d范围内,名为n shadows的字段或形式参数的声明d,名称为n的任何其他变量的声明,这些变量在d出现的范围内。

public String getBlood() {
    return bloodtype;
}

public String getFactor() {
    return rhFactor;
}

I will help you to simplify it a bit. 我会帮你简化一下。

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

As Andrew has already explain the design issue what you have in your code, I'll guide you towards the solution what you are seeking for. 由于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.

相关问题 如何正确地从另一个类调用构造函数,然后将其打印为数组以进行控制台? - How to call constructor from another class correctly and then print it as an array to console? 如何使用默认构造函数从另一个类启动 javafx 应用程序? - how to launch javafx application from another class with default constructor? 如何从另一个类构造函数调用变量(长度,宽度,高度) - How to call the variables (length,breadth,height) from another class constructor 如何从子类构造函数调用超类的私有构造函数? - How to call private constructor of super class from child class constructor? 在groovy中仅使用来自另一个类的私有构造函数的类的实例化 - Instantiation of a class with only private constructor from another class in groovy 从另一个类使用私有类构造函数访问内部子类 - Accessing an inner subclass with private class constructor from another class 如何使用私有构造函数从类创建对象? - How to create objects from a class with private constructor? 从同一类中的另一个构造函数访问构造函数变量 - Access constructor variables from another constructor in the same class 如何从另一个类中的public类型的参数化构造函数中调用默认类型的参数化构造函数? - How can I call parameterized constructor of default type from a parameterized constructor of type public which is in another class? 您可以从另一个类调用私有构造函数吗? - Can you call a private constructor from another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM