java - 为什么子类数据成员的值不显示?

标签 java inheritance

我创建了 2 个 java 文件:XCompanyShortlist.java 和 StudentDemo.java。 XCompanyShortlist.java 包含 main 方法和所有用户输入,如学生注册号、姓名、学期、GPA、CGPA、分支名称、安置状态和实习状态。

StudentDemo.java 有一个父类(super class) StudentDemo,它初始化 Reg。编号、姓名、学期、GPA、CGPA 使用参数化构造函数,它还包含一个方法 display() 显示所有信息。

BranchStudent 类扩展了 StudentDemo 类,并包含一个名为 BranchName 的额外字符串。该类还包含一个 display() 方法,该方法调用父类(super class)中的 display() 方法并打印 BranchName。另一个类 StudentPlacement 包含 InternshipStatus、PlacementStatus 和首选公司列表数组的变量。

这是 StudentDemo.java 文件代码:

class StudentDemo {
    long RegNo;
    String fname;
    short sem;
    float gpa;
    float cgpa;

    StudentDemo() {
        RegNo = 0;
        fname = "";
        sem = 0;
        gpa = (float) 0.0;
        cgpa = (float)0.0;
    }
    StudentDemo(long RegNo, String fname, short sem, float gpa, float cgpa) {
        this.RegNo = RegNo;
        this.fname = fname;
        this.sem = sem;
        this.gpa = gpa;
        this.cgpa = cgpa;
    }
    StudentDemo(StudentDemo obj) {
        RegNo = obj.RegNo;
        fname = obj.fname;
        sem = obj.sem;
        gpa = obj.gpa;
        cgpa = obj.cgpa;
    }
    void display() {
        System.out.println("------------------------------------------");
        System.out.println("Registration No. :"+RegNo);
        System.out.println("Full Name: "+fname);
        System.out.println("Semester: "+sem);
        System.out.println("GPA: "+gpa);
        System.out.println("CGPA: "+cgpa);
        System.out.println("------------------------------------------");
    }
}
class BranchStudent extends StudentDemo {
    public String BranchName;
    BranchStudent(long RegNo,String fname,short sem,float gpa,float cgpa,String BranchName) {
        super(RegNo,fname,sem,gpa,cgpa);
        this.BranchName = BranchName;
    }
    BranchStudent() {
        super();
        BranchName = "CSE";
    }
    BranchStudent(BranchStudent obj) {
        super(obj);
        BranchName = obj.BranchName;
    }

    void display() {
        super.display();
        System.out.println("Student Branch: "+BranchName);
    }
}
class StudentPlacement extends BranchStudent {
    String compList[];
    int StatusPlacement, StatusIntern;

    StudentPlacement() {
        super();
        StatusPlacement = 0;
        StatusIntern = 0;
        compList = new String[3];
    }

    StudentPlacement(StudentPlacement obj) {
        super(obj);
        StatusPlacement = obj.StatusPlacement;
        StatusIntern = obj.StatusIntern;
        compList = obj.compList;
    }

    StudentPlacement(long RegNo, String fname, short sem, float gpa, float cgpa, String BranchName,String compList[], int StatusPlacement,int StatusIntern) {
        super(RegNo, fname, sem, gpa, cgpa, BranchName);
        this.compList = compList;
        this.StatusPlacement = StatusPlacement;
        this.StatusIntern = StatusIntern;
    }
}

这是 XCompanyShortlist.java 文件代码:

import java.util.Scanner;
public class XCompanyShortlist {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please Enter The Number Of Students: ");
        int n = sc.nextInt();

        StudentPlacement obj[] = new StudentPlacement[n];
        for(int i = 0; i < n; i++) {
            obj[i] = new StudentPlacement();
        }
        System.out.println("Please Enter The Student Details: ");
        for(int i = 0; i < n; i++) {
            System.out.print("Please Enter The Reg. No. :");
            long RegNo = sc.nextLong();
            sc.nextLine();
            System.out.print("Please Enter The Full Name: ");
            String fname = sc.nextLine();
            System.out.print("Please Enter The Semester: ");
            short sem = sc.nextShort();
            System.out.print("Please Enter The GPA: ");
            float gpa = sc.nextFloat();
            System.out.print("Please Enter The CGPA: ");
            float cgpa = sc.nextFloat();
            System.out.print("Please Enter Branch Name:");
            String branchName = sc.nextLine();
            sc.nextLine();
            System.out.println("Please Enter 3 Preferred Choice: ");
            String compList[] = new String[3];
            for(int x = 0; x < 3; x++) {
                compList[x] = sc.nextLine();
            }
            System.out.print("Please Enter The Status Of Placement(0/1): ");
            int statusPlacement = sc.nextInt();
            System.out.print("Please Enter Status Of Internship(0/1): ");
            int statusIntern = sc.nextInt();
            obj[i] = new StudentPlacement(RegNo,fname,sem,gpa,cgpa,branchName,compList,statusPlacement,statusIntern);
            System.out.println();
        }
        for(int i = 0; i < n; i++) {
            obj[i].display();
        }
        sc.close();
    }

}

我面临的问题是显示了 StudentDemo 父类(super class)的所有学生详细信息,但子类 BranchStudent 没有打印 BranchName。我无法在我的代码中找到问题。

输出:

Please Enter The Number Of Students: 
1
Please Enter The Student Details: 
Please Enter The Reg. No. :159101046
Please Enter The Full Name: Bitan Basak
Please Enter The Semester: 3
Please Enter The GPA: 8.86
Please Enter The CGPA: 8.64
Please Enter Branch Name:CSE
Please Enter 3 Preferred Choice: 
HP
Dell
Microsoft
Please Enter The Status Of Placement(0/1): 0
Please Enter Status Of Internship(0/1): 0

------------------------------------------
Registration No. :159101046
Full Name: Bitan Basak
Semester: 3
GPA: 8.86
CGPA: 8.64
------------------------------------------
Student Branch: 

这是我的程序给出的输出。正如您所看到的,学生分会没有被打印。我无法理解为什么。

最佳答案

据我所知,这个问题与继承无关,而是您向构造函数提供了一个空行。

这意味着 Scanner.nextLine() 方法的使用有问题。如果我将您的代码更改为:

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please Enter The Number Of Students: ");
        int n = sc.nextInt();

        StudentPlacement obj[] = new StudentPlacement[n];
        for(int i = 0; i < n; i++) {
            obj[i] = new StudentPlacement();
        }
        System.out.println("Please Enter The Student Details: ");
        for(int i = 0; i < n; i++) {
            System.out.print("Please Enter The Reg. No. :");
            long RegNo = sc.nextLong();
            sc.nextLine();
            System.out.print("Please Enter The Full Name: ");
            String fname = sc.nextLine();
            System.out.print("Please Enter The Semester: ");
            short sem = sc.nextShort();
            System.out.print("Please Enter The GPA: ");
            float gpa = sc.nextFloat();
            System.out.print("Please Enter The CGPA: ");
            float cgpa = sc.nextFloat();
            sc.nextLine();
            System.out.print("Please Enter Branch Name:");
            String branchName = sc.nextLine();
            System.out.println("Please Enter 3 Preferred Choice: ");
            String compList[] = new String[3];
            for(int x = 0; x < 3; x++) {
                compList[x] = sc.nextLine();
            }
            System.out.print("Please Enter The Status Of Placement(0/1): ");
            int statusPlacement = sc.nextInt();
            System.out.print("Please Enter Status Of Internship(0/1): ");
            int statusIntern = sc.nextInt();
            obj[i] = new StudentPlacement(RegNo,fname,sem,gpa,cgpa,branchName,compList,statusPlacement,statusIntern);
            System.out.println();
        }
        for(int i = 0; i < n; i++) {
            obj[i].display();
        }
        sc.close();
    }

即将 sc.nextLine() 移到分支名称输入之前,扫描仪会从控制台获取正确的值。

希望有帮助。

问候

关于java - 为什么子类数据成员的值不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39769486/

相关文章:

Java LocalDate 如何将一年的最后一周作为第 53 周而不是新年的第一周?

java - 如何将 List<实现接口(interface)的对象> 传递给方法?

安卓性能 : useless global variables vs inheritance

java - ArrayList<String> 未设置最小容量

java - Byte Buddy 导致 IncompleteClassChangeError

java - 线程 "main"java.lang.ArithmeticException :/by zero 中的异常

C++从派生类转换为具有不同模板编号的基类

java - Java中的方法重写-将子类对象分配给父类变量

c++ - 当另一个成员存在另一个签名时,调用纯虚拟成员的正确方法是什么?

java - 如何在 Selenium Webdriver (JAVA) 中使用 "extends"