我的第二个构造函数参数如何工作?

huangapple go评论73阅读模式
英文:

How can my 2nd constructor parameter work?

问题

import java.util.Scanner;

class BloodData {
    static Scanner in = new Scanner(System.in);
    static String bloodType;
    static String rhFactor;

    public BloodData() {
        bloodType = "O";
        rhFactor = "+";
    }

    public BloodData(String bt, String rh) {
        bloodType = bt;
        rhFactor = rh;
    }

    public void display() {
        System.out.println(bloodType + rhFactor + " is added to the blood bank");
    }

    public static void main(String[] args) {
        System.out.println("Enter Blood Type(O, A, B, AB)");
        System.out.println("Enter rhFactor('+' or '-')");
        BloodData bd = new BloodData(BloodData.bloodType = in.nextLine(), BloodData.rhFactor = in.nextLine());

        BloodData bd1 = new BloodData();
        bd.display();
    }
}
英文:
       import java.util.Scanner;
       class BloodData{
            static Scanner in = new Scanner(System.in);
            static String bloodType;
            static String rhFactor;
 
         public BloodData(){
            bloodType = "O";
            rhFactor = "+";
       }
         public BloodData(String bt, String rh){
           bloodType = bt;
           rhFactor = rh;
       }
         public void display() {
           System.out.println(bloodType+rhFactor+" is added to the blood bank");
       }

         public static void main(String[]args) {

           System.out.println("Enter Blood Type(O, A, B, AB)");
           System.out.println("Enter rhFactor('+' or '-')");
           BloodData bd= new 
           BloodData(BloodData.bloodType=in.nextLine(),BloodData.rhFactor=in.nextLine());

           BloodData bd1= new BloodData();
           bd.display();

        }
      }

How can i use the constructor with 2 String parameters? because when I always run the code the first one only run. I'm only a beginner so hope someone could help because I already watched a lot of Youtube vids and I really didn't know why this happens

答案1

得分: 1

Java不使用像这样的命名参数来调用方法或构造函数。如果有视频展示这样的语法,我会感到惊讶...

new BloodData(BloodData.bloodType=in.nextLine(), BloodData.rhFactor=in.nextLine())

你需要在上一行中定义String变量,然后将它们传递给构造函数。你也不应该将实例构造函数与静态变量结合使用。

class BloodData {
    private String bloodType;
    private String rhFactor;

    public BloodData(String type, String rhFactor) {
        this.bloodType = type;
        this.rhFactor = rhFactor;
    }
    // 其他代码...

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String x = in.nextLine();
        String y = in.nextLine();
        BloodData bd = new BloodData(x, y);
    }
}
英文:

Java doesn't use named parameters like this for method or constructors. I would be surprised if a video showed such syntax...

new BloodData(BloodData.bloodType=in.nextLine(),BloodData.rhFactor=in.nextLine())

You need to define String variables on the line above, then pass them into the constructor. You also shouldn't be combining instance constructors with static variables

   class BloodData{
        private String bloodType;
        private String rhFactor;

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

        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            String x = in.nextLine();
            String y = in.nextLine();
            BloodData bd = new BloodData(x, y);
        }

答案2

得分: 0

如果你想调用一个有两个参数的构造函数:使用 this

BloodData blooddata = new BloodData("A", "+");
英文:

if you want to call a constructor having two parameters: Use this

BloodData blooddata = new BloodData("A","+");

huangapple
  • 本文由 发表于 2020年10月22日 01:53:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64469131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定