该代码的最后两行示出了该问题:当我使用参考对象编译器的工作原理,但不是当我分配参照的数组元素。其余代码位于单独文件的相同包中。 BioStudent和ChemStudent是独立的班级,以及学生。多态行为没有得到执行
package pkgPoly;
public class Poly {
public static void main(String[] arg) {
Student[] stud = new Student[3];
// create a biology student
BioStudent s1 = new BioStudent("Tom");
// create a chemistry student
ChemStudent s2 = new ChemStudent("Dick");
// fill the student body with studs
stud[0] = s1;
stud[1] = s2;
// compiler complains that it can't find symbol getMajor on next line
System.out.println("major: " + stud[0].getMajor()); // doesn't compile;
System.out.println("major: " + s0.getMajor()); // works: compiles and runs correctly
}
}
你能发布错误信息吗?可能还有'Student'的代码。 – iamnotmaynard
编译器抱怨说在下一行找不到符号getMajor - 他在注释 – Raffaele
中写道错误消息是“source \ pkgPoly \ Poly.java:50:错误:找不到符号符号:method getMajor()location:class学生 –