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

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

How can my 2nd constructor parameter work?

问题

  1. import java.util.Scanner;
  2. class BloodData {
  3. static Scanner in = new Scanner(System.in);
  4. static String bloodType;
  5. static String rhFactor;
  6. public BloodData() {
  7. bloodType = "O";
  8. rhFactor = "+";
  9. }
  10. public BloodData(String bt, String rh) {
  11. bloodType = bt;
  12. rhFactor = rh;
  13. }
  14. public void display() {
  15. System.out.println(bloodType + rhFactor + " is added to the blood bank");
  16. }
  17. public static void main(String[] args) {
  18. System.out.println("Enter Blood Type(O, A, B, AB)");
  19. System.out.println("Enter rhFactor('+' or '-')");
  20. BloodData bd = new BloodData(BloodData.bloodType = in.nextLine(), BloodData.rhFactor = in.nextLine());
  21. BloodData bd1 = new BloodData();
  22. bd.display();
  23. }
  24. }
英文:
  1. import java.util.Scanner;
  2. class BloodData{
  3. static Scanner in = new Scanner(System.in);
  4. static String bloodType;
  5. static String rhFactor;
  6. public BloodData(){
  7. bloodType = "O";
  8. rhFactor = "+";
  9. }
  10. public BloodData(String bt, String rh){
  11. bloodType = bt;
  12. rhFactor = rh;
  13. }
  14. public void display() {
  15. System.out.println(bloodType+rhFactor+" is added to the blood bank");
  16. }
  17. public static void main(String[]args) {
  18. System.out.println("Enter Blood Type(O, A, B, AB)");
  19. System.out.println("Enter rhFactor('+' or '-')");
  20. BloodData bd= new
  21. BloodData(BloodData.bloodType=in.nextLine(),BloodData.rhFactor=in.nextLine());
  22. BloodData bd1= new BloodData();
  23. bd.display();
  24. }
  25. }

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

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

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

  1. class BloodData {
  2. private String bloodType;
  3. private String rhFactor;
  4. public BloodData(String type, String rhFactor) {
  5. this.bloodType = type;
  6. this.rhFactor = rhFactor;
  7. }
  8. // 其他代码...
  9. public static void main(String[] args) {
  10. Scanner in = new Scanner(System.in);
  11. String x = in.nextLine();
  12. String y = in.nextLine();
  13. BloodData bd = new BloodData(x, y);
  14. }
  15. }
英文:

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

  1. 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

  1. class BloodData{
  2. private String bloodType;
  3. private String rhFactor;
  4. public BloodData(String type, String rhFactor) {
  5. this.bloodType = type;
  6. this.rhFactor = rhFactor;
  7. }
  8. ...
  9. public static void main(String[] args) {
  10. Scanner in = new Scanner(System.in);
  11. String x = in.nextLine();
  12. String y = in.nextLine();
  13. BloodData bd = new BloodData(x, y);
  14. }

答案2

得分: 0

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

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

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

  1. 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:

确定