Java-试图理解抽象类继承和子类对象比较
我首先要说,如果标题太模糊/不够好,我很抱歉,我很难用一行标题来表达我的问题 我正在为我的data structures类做一个项目,我一直很难理解抽象类继承,以及如何通过子类、对象比较、方法参数等操作抽象类继承。我整晚都在读StackOverflow posts/java教程(现在差不多4个小时了),虽然我学到了很多东西,但我仍然不理解一些东西。我已经2年多没有用java编写代码了,最近大部分时间都在使用C,所以我感到非常生疏和不知所措:/ 项目概述 我被要求建立一个各种各样的数据库,为我的大学生和教授提供数据。我有一个抽象的超类,Java-试图理解抽象类继承和子类对象比较,java,arrays,inheritance,abstract-class,Java,Arrays,Inheritance,Abstract Class,我首先要说,如果标题太模糊/不够好,我很抱歉,我很难用一行标题来表达我的问题 我正在为我的data structures类做一个项目,我一直很难理解抽象类继承,以及如何通过子类、对象比较、方法参数等操作抽象类继承。我整晚都在读StackOverflow posts/java教程(现在差不多4个小时了),虽然我学到了很多东西,但我仍然不理解一些东西。我已经2年多没有用java编写代码了,最近大部分时间都在使用C,所以我感到非常生疏和不知所措:/ 项目概述 我被要求建立一个各种各样的数据库,为我的大
Person
,它有两个子类Student
和讲师
。最后(我目前正在学习的课程)是一个名为UniversityPeople
的课程,它引用了Student[]
对象的数组以及讲师[]
对象的数组。UniversityPeople
类有方法来add()
,delete()
,search()
,验证数据等。对于2个对象数组。我知道这个实现看起来很奇怪,但不幸的是,我不得不按照说明进行操作。我试图使这些方法能够无缝地与两个阵列协同工作,这部分是我感到困惑的地方。我将尽力组织我的问题:
1.)这是一个一般性的问题,但我觉得这对理解继承很重要。我已经读了很多这方面的书,但我还是很困惑。如果我创建了一个使用超类类型的对象,但它是一个子类对象,比如:Person newStudent=new Student()
,那么这个对象是否有权访问它自己类中的方法,还是仅限于超类方法?我的理解是,如果超类是抽象的
,那么当以这种方式实例化时,子类仍然继承它们的方法
2.)我很难理解如何区分方法中的两个对象数组。例如,我正在编写一个clear()
函数,该函数在调用时将指定的对象数组(Student[]
或讲师[]
)设置为null。我第一次尝试使用instanceOf
方法测试数组的类型,但编译器警告我不能将类型Student转换为Person或类似的类型。这是该类的构造函数以及我遇到问题的clear函数。我不确定我是否理解继承在这里是如何充分发挥作用的,因此如果有更好的方法,请告诉我:
建造商:
//Global declarations
//Declares arrays of student and instructor classes
private Student[] studentArray;
private Instructor[] instArray;
private int studentCount;
private int instCount;
/*
checks user input. If user enters 1, create student array.
if user enters 2, create Instructor array
*/
public UniversityPeople(int type, int size)
{
if (type == 1)
{
studentArray = new Student[size];
}
else if (type == 2)
{
instArray = new Instructor[size];
}
}
为学生/教师单独插入方法,因为我无法找到一种方法来完成1,
如果可能的话:
/*
checks if array is full or duplcate id exists.
If not, new Student object is created and
inserted into the student array
*/
public void insertStudent(String lastName, String firstName, int id,
int age, String yearStanding, double GPA)
{
boolean checkFull = isFull();
boolean checkId = containsID(studentArray, id);
if (checkFull)
{
System.out.println("Array is full. Please delete a data member.");
}
else if (checkId)
{
System.out.println("Duplicate ID. Please check data and re-enter.");
}
if (!checkFull && !checkId)
{
Student newStudent = new Student(lastName, firstName, id, age, yearStanding, GPA);
newStudent = studentArray[studentCount];
studentCount++;
}
else
System.err.println("There was an error while attempting to \n" +
"process your request. Please check your \n" +
"data entry and try again.");
}//end insertStudent
/*
checks if array is full or duplicate id exists.
If not, new Instructor object is created and
inserted into instructor array
*/
public void insertInstructor (String lastName, String firstName, int id,
int age, int officeNum, String subjectTaught)
{
boolean checkFull = isFull();
boolean checkId = containsID(instArray, id);
if (checkFull)
{
System.out.println("Array is full. Please delete a data member.");
}
else if (checkId)
{
System.out.println("Duplicate ID. Please check data and re-enter.");
}
if (!checkFull && !checkId)
{
Instructor newInstructor =
new Instructor(lastName, firstName, id, age, officeNum, subjectTaught);
newInstructor = instArray[instCount];
instCount++;
}
else
System.err.println("There was an error while attempting to \n" +
"process your request. Please check your \n" +
"data entry and try again.");
}//end insertInstructor
最后,是一个从id中查找数组中某个人的方法,以及一个清除指定的任何数组的方法。我已经在大多数方法中创建了arg typePerson[]
数组,我希望这是可以接受的
public int findPerson(Person[] array, int id)
{
for (int i = 0; i < array.length; i++)
{
if (array[i].getId() == id)
{
return i;
}
}
return -1;
}
//Sets specified array to null. I need to reset the count of objects in
//the specified array to 0, but i'm really confused about how that works. Below is the best
//understanding I could come up with
public void clear(Person[] array)
{
for (int i = 0; i < array.length; i++)
{
array[i] = null;
}
if (array.getClass().equals(Student.class))
{
studentCount = 0;
}
else if (array.getClass().equals(Instructor.class))
{
instCount = 0;
}
else
{
System.out.println("There was an issue validating the object type.");
}
}
public int-findPerson(Person[]数组,int-id)
{
for(int i=0;i
我感谢您的任何意见或建议。我对clear()
方法的实现尤其感到困惑,因为我以前从未做过这种类型的对象比较。我不知道为什么操作符的实例比较会给我奇怪的结果。我真的很想了解这些东西,它真的很棒,看起来很有用。如果你想让我发布更多的代码,我可以,但我觉得这可能足以让我的观点得到理解。再次感谢:)的清除方法:
array = new Person[array.length] // re-init arrayt ot it's inital size.
studentCount = studentArrayl.length;
instCount = instArray.length;
这应该将给定数组重新初始化为正确长度的新空数组,并将正确的数字重新指定给studentCount和instCount
问候
复仇女神
[…]此对象是否有权访问其自己类中的方法,或者
它仅限于超类方法?我的理解是,如果
超类是抽象的,子类仍然继承它们的方法
当以这种方式实例化时
不管超类是否是。子类包含父类的方法和属性。子类是否可以访问它们取决于
我很难理解如何区分
方法中有两个对象数组。例如,我正在编写一个clear()函数,该函数在调用时将指定的对象数组(Student[]或讲师[])设置为null
您可以重载clear()
方法,如下所示:
public void clear(Student[] array) {...}
public void clear(Instructor[] array) {...}
或者(如果我正确理解了示例的目的)将输入数组的引用与类定义中的数组引用进行比较:
public void clear(Person[] array) {
if(array == this.studentArray) {
// clear students
} else if(array == this.instArray) {
// clear instructors
}
}
但是,如果要实际比较阵列的类型,可以执行以下操作:
array.getClass().equals(Student[].class)
或
对于#1,是的,newStudent
可以访问它自己的方法以及它的超类
array instanceof Student[]