我正在尝试解决可能导致此问题的问题。我是Java和BlueJ的新手,但已经看了好几个小时,无法弄清楚导致问题的原因。继续收到错误消息“不兼容的类型:Plasterer无法转换为Apprentice”。非常感谢任何帮助。
//程序的目的:创建一个混合数组来容纳2个不同的Apprentice对象 - Plasterer和Carpenter //泥水匠和木匠 //然后将每个对象的所有细节显示在屏幕上。
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.3.0/knockout-min.js"></script>
<span data-bind="myText: { text: testProperty, required: true }"></span>
<br>
<span data-bind="myText: { text: testProperty, required: false }"></span>
<br>
<span data-bind="myText: testProperty"></span>
学徒班就是这样
public class MixedLists
{
public static void main(String args[])
{
//Declaration of object list to hold 2 student objects
Apprentice [] apprenticeList = new Apprentice[2];
////////////////////////////////////////////////////////////////////
//Plasterer
//Taking in values
String NameIn, NumberIn, AddressIn, StudentIDIn, EmployerNameIn, FinalSem1GradeIn;
System.out.println("Entering data for Plasterer");
//Apprentice class attributes
System.out.print("\tEnter Name: ");
NameIn = EasyScanner.nextString();
System.out.print("\tEnter telephone number: ");
NumberIn = EasyScanner.nextString();
System.out.print("\tEnter Address: ");
AddressIn = EasyScanner.nextString();
System.out.print("\tEnter student id: ");
StudentIDIn = EasyScanner.nextString();
//Plasterer class attributes
System.out.print("\tEnter in Employer Name: ");
EmployerNameIn = EasyScanner.nextString();
System.out.print("\tEnter Final Semester 1 Grade: ");
FinalSem1GradeIn = EasyScanner.nextString();
//Initializing what is referenced by position 0 of the object list with a plasterer object
apprenticeList[0]=new Plasterer(NameIn, NumberIn, AddressIn, StudentIDIn, EmployerNameIn, FinalSem1GradeIn);
System.out.println("\n");
/////////////////////////////////////////////////////////////////
//Carpenter
String NameIn1,AddressIn1,Number1,StudentID1;
//additional variables
String awardSought,specialisation;
double gpaModuleGrade, industrialExperienceMark;
//Entering values for Carpenter
System.out.println("Entering data for Carpenter");
//Apprentice class attributes
System.out.print("\tEnter Name: ");
NameIn1 = EasyScanner.nextString();
System.out.print("\tEnter telephone number: ");
Number1 = EasyScanner.nextString();
System.out.print("\tEnter Address: ");
AddressIn1= EasyScanner.nextString();
System.out.print("\tEnter student id: ");
StudentID1= EasyScanner.nextString();
//Carpenter class attributes
System.out.print("\tEnter award sought: ");
awardSought = EasyScanner.nextString();
System.out.print("\tEnter specialist area: ");
specialisation = EasyScanner.nextString();
//Carpenter class attributes
System.out.print("\tEnter GPA Module Grade: ");
gpaModuleGrade = EasyScanner.nextString();
System.out.print("\tEnter industrial experience Grade: ");
industrialExperienceMark = EasyScanner.nextString();
//Initializing what is referenced by position 1 of the object list with a Carpenter object
apprenticeList[1]=new Carpenter(NameIn1, AddressIn1, Number1, StudentID1,awardSought,specialisation, gpaModuleGrade,industrialExperienceMark);
System.out.println("\n");
////////////////////////////////////////////////////////////////////
//for loop to display out contents of mixed object array
//Display out all information associated with Carpenter, Carpenter and CabinetMaker
System.out.println("Award Details for all Apprentices in the System\n");
for(int i = 0; i < apprenticeList.length; i++)
{
System.out.println(apprenticeList[i].printAwardDetails());
}
}
}
答案 0 :(得分:0)
您需要使用继承。
创建一个名为“Worker”的类,然后Plasterer和Apprentice都应该继承该类。然后,您可以获得包含Plasterers和Apprentice的工人列表。
或者如果“学徒”只是一种泥水匠,让学徒继承泥水匠(或者如果泥水匠是一种学徒则相反)