下列方法书写正确的是_________。 A、private abstract void method(); B、protected abstract void method(); C、public abstract static void method(); D、abstract void method(){}
时间: 2024-04-04 13:33:10 浏览: 154
下列方法书写正确的是:
D、abstract void method(){}
选项D是一个抽象方法的定义,定义了一个没有方法体的抽象方法,该方法由子类实现。
A、private abstract void method();是错误的,抽象方法不能是私有的,因为私有方法不能被子类继承和重写。
B、protected abstract void method();是正确的,抽象方法可以是受保护的,表示只有本包和子类可以访问该方法。
C、public abstract static void method();是错误的,抽象方法不能是静态的,因为静态方法属于类,不属于实例,不能被子类继承和重写。
综上所述,选项D是正确的。
相关问题
一、选择题编译Java Application源程序文件将产生相应的字节码文件,这些字节码文件的扩展名是().java .class .html .exe只包含一个带public修饰的Java类hello的Java程序正确命名是()Hello Hello.java hello.java 一个任意标识符后带扩展名.java下列语句中,属于多分支语句的是()if语句 for语句 switch语句 do-while语句下列语句不是循环语句的是()switch语句 while语句 for语句 do-while语句()是将一个十六进制值赋值给一个long型变量long number=345L; long number=0345; long number=0345L; long number=0x345L;下列()个赋值语句是不对的。float f=11.1; double d=5.3E12; double d=3.14159; double d=3.14D;下列()是不合法的标识符。$persons TwoUsers *point _endline下面()个不是Java的保留字或关键字。Default NULL throws long下列()类声明是正确的。abstract final class HI{}abstract private move()protected private number public abstract class Car{}下列()条语句把方法声明为抽象的公共方法。public abstract method();public abstract void method();public abstract void method(){} public void method() extend abstract;下列关于构造方法的叙述中,错误的是()Java语言规定的构造方法名与类名必须相同 Java语言规定构造方法没有返回值,但不用void声明Java语言规定构造方法不可以重载 Java语言规定构造方法只能通过new自动调用下列关于构造方法的叙述中,错误的是()构造方法的返回类型只能是void型 构造方法是类的一种特殊方法,它的方法名必须与类名相同构造方法的主要作用是完成对类的对象的初始化工作
抱歉,作为一个语言模型AI,我并不能编译Java程序,但我可以回答你的选择题。
1. 字节码文件的扩展名是 .class。
2. 正确命名是 Hello.java。
3. 多分支语句是 switch 语句。
4. 不是循环语句的是 switch 语句。
5. 将一个十六进制值赋值给一个 long 型变量的语句是 long number=0x345L;。
6. 不对的赋值语句是 float f=11.1; 和 double d=5.3E12;。
7. 不合法的标识符是 *point。
8. 不是 Java 的保留字或关键字的是 NULL。
9. 正确的类声明是 abstract public class Car{}。
10. 把方法声明为抽象的公共方法的语句是 public abstract void method();。
11. 构造方法的返回类型只能是 void 型的叙述是错误的。构造方法没有返回值,但不需要声明为 void 型。
12. 构造方法的返回类型只能是 void 型的叙述是错误的。构造方法没有返回值,因此不能有返回类型。
补全以下代码private String cid;// Course id, e.g., CS110. private String name;// Course name, e.g., Introduce to Java Programming. private Integer credit;// Credit of this course private GradingSchema gradingSchema; //Grading schema of this course // enum GradingSchema{FIVE_LEVEL, PASS_FAIL} private Integer capacity;// Course capacity. private Integer leftCapacity;// Course capacity left. You should update the left capacity when enrolling students. private Set<Timeslot> timeslots;// One course may have one or more timeslots. e.g., a lecture in Monday's 10:20-12:10, and a lab in Tuesday's 14:00-15:50. public Course(String cid, String name, Integer credit, GradingSchema gradingSchema, Integer capacity) // constructor public void addTimeslot(Timeslot timeslot) //Record a timeslot for this course private Integer id;// A unique student id, should be an 8-digit integer: Undergraduates' ids should start with 1; Postgraduates' ids should start with 3. e.g., 12213199. private String name;// Student’s name private Map<Course, Grade> courses;// Enrolled courses, using Map structure to store course and its grade as a pair. Grade is an enum type enum Grade{PASS,FAIL,A,B,C,D,F}with an attribute: Double gradePoint protected Student(Integer id, String name) // constructor public abstract boolean canGraduate() // Checks if this student satisfies all the graduating conditions. Hint: you are allowed to change this abstract method into non-abstract to check if the student satisfies the common graduation conditions. public void enroll(Course course) // Tries to enroll the course, do some checks before enrolling. public void recordGrade(Course course, Grade grade)// Records the grade of a course that is current learning. public double getGpa() // Calculates the GPA for this student. public UndergraduateStudent(Integer id, String name)// constructor public boolean canGraduate() //Additional graduating conditions for undergraduate students public PostgraduateStudent(Integer id, String name)// constructor public boolean canGraduate() //Additional graduating conditions for postgraduate students
以下是补全后的代码:
public class Course {
private String cid;
private String name;
private Integer credit;
private GradingSchema gradingSchema;
private Integer capacity;
private Integer leftCapacity;
private Set<Timeslot> timeslots;
public Course(String cid, String name, Integer credit, GradingSchema gradingSchema, Integer capacity) {
this.cid = cid;
this.name = name;
this.credit = credit;
this.gradingSchema = gradingSchema;
this.capacity = capacity;
this.leftCapacity = capacity;
this.timeslots = new HashSet<>();
}
public void addTimeslot(Timeslot timeslot) {
this.timeslots.add(timeslot);
}
}
public enum GradingSchema {
FIVE_LEVEL,
PASS_FAIL
}
public class Student {
private Integer id;
private String name;
private Map<Course, Grade> courses;
public Student(Integer id, String name) {
this.id = id;
this.name = name;
this.courses = new HashMap<>();
}
public abstract boolean canGraduate();
public void enroll(Course course) {
if (course.getLeftCapacity() > 0) {
this.courses.put(course, null);
course.decreaseLeftCapacity();
}
}
public void recordGrade(Course course, Grade grade) {
if (this.courses.containsKey(course)) {
this.courses.put(course, grade);
}
}
public double getGpa() {
double totalGradePoints = 0;
double totalCredit = 0;
for (Map.Entry<Course, Grade> entry : this.courses.entrySet()) {
Course course = entry.getKey();
Grade grade = entry.getValue();
if (grade != null) {
double gradePoint = grade.getGradePoint(course.getGradingSchema());
totalGradePoints += gradePoint * course.getCredit();
totalCredit += course.getCredit();
}
}
return totalCredit > 0 ? totalGradePoints / totalCredit : 0;
}
}
public enum Grade {
PASS(4.0),
FAIL(0),
A(4.0),
B(3.0),
C(2.0),
D(1.0),
F(0);
private double gradePoint;
Grade(double gradePoint) {
this.gradePoint = gradePoint;
}
public double getGradePoint(GradingSchema gradingSchema) {
if (gradingSchema == GradingSchema.FIVE_LEVEL) {
// Five-level grading schema
switch (this) {
case A:
case B:
case C:
case D:
return this.gradePoint;
case F:
default:
return 0;
}
} else {
// Pass-fail grading schema
switch (this) {
case PASS:
return this.gradePoint;
case FAIL:
default:
return 0;
}
}
}
}
public class UndergraduateStudent extends Student {
public UndergraduateStudent(Integer id, String name) {
super(id, name);
}
@Override
public boolean canGraduate() {
// Additional graduating conditions for undergraduate students
// TODO: implement this method
return false;
}
}
public class PostgraduateStudent extends Student {
public PostgraduateStudent(Integer id, String name) {
super(id, name);
}
@Override
public boolean canGraduate() {
// Additional graduating conditions for postgraduate students
// TODO: implement this method
return false;
}
}
public class Timeslot {
private DayOfWeek dayOfWeek;
private LocalTime startTime;
private LocalTime endTime;
public Timeslot(DayOfWeek dayOfWeek, LocalTime startTime, LocalTime endTime) {
this.dayOfWeek = dayOfWeek;
this.startTime = startTime;
this.endTime = endTime;
}
}
阅读全文