Java 无法创建子类的动态实例
我正在使用继承创建一个学生注册程序,在该程序中,我希望创建用户在提示有关学生总数时输入的学生动态实例,但我无法这样做。 任何帮助都将不胜感激Java 无法创建子类的动态实例,java,oop,inheritance,instance,subclass,Java,Oop,Inheritance,Instance,Subclass,我正在使用继承创建一个学生注册程序,在该程序中,我希望创建用户在提示有关学生总数时输入的学生动态实例,但我无法这样做。 任何帮助都将不胜感激 那么你的具体问题是什么?你写了你想做的,但没有写不起作用的……除此之外:学生为什么是从课程衍生出来的?当然,学生不是课程的专业版本……我不能创建学生类的动态实例。这不是设计类层次结构的方式。你不明白这是怎么回事。如果学生对象应该包含学生所修课程的列表,那么它应该包含课程对象的列表。这与继承无关。student类可能继承自“person”类。您可以通过多次
那么你的具体问题是什么?你写了你想做的,但没有写不起作用的……除此之外:
学生
为什么是从课程
衍生出来的?当然,学生不是课程的专业版本……我不能创建学生类的动态实例。这不是设计类层次结构的方式。你不明白这是怎么回事。如果学生对象应该包含学生所修课程的列表,那么它应该包含课程对象的列表。这与继承无关。student类可能继承自“person”类。您可以通过多次调用new
操作符并将结果存储在单独的对象中(例如学生的列表)来创建多个学生实例。
import java.util.*;
class Course{
static Scanner input=new Scanner(System.in);
String courseName;
String preReq;
static int totalStudents;
Course(){
System.out.println("Course Details:");
System.out.print("Enter name of course: ");
courseName=input.nextLine();
System.out.print("Enter pre-requisite: ");
preReq=input.nextLine();
System.out.println("Class Details:");
System.out.print("Enter no# of students: ");
totalStudents=input.nextInt();
}
}
class Student extends Course{
String studentName;
String studentCms;
static int count=0;
String confirm;
Student(){
count+=1;
System.out.println("Student Details:");
System.out.println("Enter name of student #"+count+": ");
studentName=input.nextLine();
System.out.println("Enter CMS id of student #"+count+": ");
studentCms=input.nextLine();
System.out.println("Enter Y/y to register in course: ");
confirm=input.next();
}
}
public class CourseRegistration{
public static void main(String args[]){
Student ob=new Student();
}
}