Java继承,在构造函数内对父类初始化的问题
2016-09-16 23:29
351 查看
source: https://www.hackerrank.com/challenges/30-inheritance?h_r=next-challenge&h_v=zen
Student的创造方法,这样子写:
Student(String firstName, String lastName, int id, int[] testScores) {
super(firstName, lastName, id);
this.testScores = testScores;
}
是可以的,
但,若这样子写:
Student(String firstName, String lastName, int id, int[] testScores) {
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = id;
this.testScores = testScores;
}
就会报错:如下:
import java.util.*;
class Person {
protected String firstName;
protected String lastName;
protected int idNumber;
// Constructor
Person(String firstName, String lastName, int identification){
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = identification;
}
// Print person data
public void printPerson(){
System.out.println(
"Name: " + lastName + ", " + firstName
+ "\nID: " + idNumber);
}
}
class Student extends Person{
private int[] testScores;
Student(String firstName, String lastName, int id, int[] testScores) {
super(firstName, lastName, id);
this.testScores = testScores;
}
public char calculate(){
int avg = Arrays.stream(testScores).sum()/testScores.length;
if (avg >= 90) return 'O';
if (avg >= 80) return 'E';
if (avg >= 70) return 'A';
if (avg >= 55) return 'P';
if (avg >= 40) return 'D';
return 'T';
}
}
class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String firstName = scan.next();
String lastName = scan.next();
int id = scan.nextInt();
int numScores = scan.nextInt();
int[] testScores = new int[numScores];
for(int i = 0; i < numScores; i++){
testScores[i] = scan.nextInt();
}
scan.close();
Student s = new Student(firstName, lastName, id, testScores);
s.printPerson();
System.out.println("Grade: " + s.calculate() );
}
}
Student的创造方法,这样子写:
Student(String firstName, String lastName, int id, int[] testScores) {
super(firstName, lastName, id);
this.testScores = testScores;
}
是可以的,
但,若这样子写:
Student(String firstName, String lastName, int id, int[] testScores) {
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = id;
this.testScores = testScores;
}
就会报错:如下:
Solution.java:26: error: constructor Person in class Person cannot be applied to given types; Student(String firstName, String lastName, int id, int[] testScores) { ^ required: String,String,int found: no arguments reason: actual and formal argument lists differ in length 1 error
import java.util.*;
class Person {
protected String firstName;
protected String lastName;
protected int idNumber;
// Constructor
Person(String firstName, String lastName, int identification){
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = identification;
}
// Print person data
public void printPerson(){
System.out.println(
"Name: " + lastName + ", " + firstName
+ "\nID: " + idNumber);
}
}
class Student extends Person{
private int[] testScores;
Student(String firstName, String lastName, int id, int[] testScores) {
super(firstName, lastName, id);
this.testScores = testScores;
}
public char calculate(){
int avg = Arrays.stream(testScores).sum()/testScores.length;
if (avg >= 90) return 'O';
if (avg >= 80) return 'E';
if (avg >= 70) return 'A';
if (avg >= 55) return 'P';
if (avg >= 40) return 'D';
return 'T';
}
}
class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String firstName = scan.next();
String lastName = scan.next();
int id = scan.nextInt();
int numScores = scan.nextInt();
int[] testScores = new int[numScores];
for(int i = 0; i < numScores; i++){
testScores[i] = scan.nextInt();
}
scan.close();
Student s = new Student(firstName, lastName, id, testScores);
s.printPerson();
System.out.println("Grade: " + s.calculate() );
}
}
相关文章推荐
- Java继承的初始化与构造函数的执行顺序问题
- Java子类和父类的构造函数继承的问题分析
- 关于继承与构造函数重载的问题(初始化子类构造函数中的父类对象)求解答。。。
- JAVA基础:子类、父类继承关系 静态块、非静态块、构造函数初始化顺序性、静态非静态方法覆盖
- Java语言深入-构造函数的继承问题 (转)
- Java中的类成员访问控制符和继承重写父类的方法时的类成员访问控制符的相关问题
- java代码继承。。。找出不能继承父类方法的问题
- java子类继承父类构造函数
- java类继承中父类调用子类函数的问题
- Java对象的构造过程---子类继承父类时(非)静态块及构造函数等的执行顺序
- java中子类继承父类时是否继承构造函数
- java 父类子类静态成员,实例成员,构造函数初始化的顺序
- Java类初始化顺序问题:静态初始化块,非静态初始化块以及构造函数
- java中子类继承父类时是否继承构造函数
- 再探java成员在继承时初始化的相关问题
- JAVA 子类继承父类的构造函数的重载
- java中子类继承父类时是否继承构造函数
- java中子类继承父类时是否继承构造函数呢?
- java中子类继承父类时是否继承构造函数呢?
- java继承,多态方面的一个问题,定义一个父类对象,用子类new