如何正确使用继承和多态性 - 以数组的形式实现的队列
我只需要在遗传和多态性方面提供一些帮助。我们正在研究队列&堆栈,并被要求将它们作为数组实现,因此我们正确理解它们,而不仅仅是使用Java的方法。
任务是将改组和循环队列作为一般队列类的子类实施。
改组或线性队列:以一个接一个的顺序排列数据。解决方案是在排出第一个值后将所有元素移至1,从而导致“改组效果”。
圆形队列:一个圆形队列“追逐前部”,当前部通过阵列的'端'时绕阵列循环。它安排了类似于圆的数据,最后一个元素连接回第一个元素。
我首先仅将改组队列作为自己的一类实施,并使其正常工作。这是代码的第一部分:
public class myQueue
{
//class fields
private Object[] queue;
private int rear, front, maxSize;
public static final int DefaultCap = 100;
//default constructor
public myQueue()
{
maxSize = DefaultCap;
queue = new Object[DefaultCap];
rear = 0;
front = 0;
}
//alternate Constructor
public myQueue(int maxCapacity)
{
if(maxCapacity < 0)
{
throw new IllegalArgumentException("Capacity cannot be negative");
}
else
{
queue = new Object[maxCapacity];
maxSize = maxCapacity;
rear = 0;
front = 0;
}
}
我在那里也有方法,例如enqueue()
和dequeue()
等
。使用我的父班类,例如后部,前,容量。他们需要公开吗?
目前,我有:
public class ShufflingQueue extends MyQueue
{
public ShufflingQueue(int Capacity)
{
super(Capacity);
}
public void enqueue(Object n)
{
super.enqueue(n);
}
public void dequeue()
{
super.dequeue();
}
public int getSize()
{
return super.getSize();
}
}
目前,上述很好,它可以按照我的期望。但是,这是因为我仍然有inqueue()
和dequeue()
作为父类的一部分。我应该更改它,以便我的洗牌队列和圆形队列具有不同的顾问和登录方法。我知道 code in squeue and Dequeue,无论是改组和圆形队列,但我不确定如何使用这两个队列的这些不同的方法。以及我如何能够使用父母类中的后
, front 。我还需要在子类中的后部,前部和容量的新课程吗?
I just needed a little help with inheritance and polymorphism. We're working on queues & stacks, and were asked to implement them as arrays so we understand them properly, instead of just using Java's methods.
The task is to implement a Shuffling and Circular queue as subclasses of a general Queue class.
A Shuffling or Linear Queue: Arranges data in a sequential order, one after another. The solution is to shift all elements up by 1 after dequeuing the first value, causing a "Shuffling Effect".
A Circular Queue: A circular queue "chases the front" and cycles around the array when the front passes the 'end' of the array. It arranges data similar to a circle, with the last element connecting back to the first element.
I first implemented just the shuffling queue as a class of its own, and got it working properly. Here's the first section of code:
public class myQueue
{
//class fields
private Object[] queue;
private int rear, front, maxSize;
public static final int DefaultCap = 100;
//default constructor
public myQueue()
{
maxSize = DefaultCap;
queue = new Object[DefaultCap];
rear = 0;
front = 0;
}
//alternate Constructor
public myQueue(int maxCapacity)
{
if(maxCapacity < 0)
{
throw new IllegalArgumentException("Capacity cannot be negative");
}
else
{
queue = new Object[maxCapacity];
maxSize = maxCapacity;
rear = 0;
front = 0;
}
}
I also have methods in there like enqueue()
and dequeue()
etc.
Can't quite wrap my head around how I'm going to use the parameters from my parent class like rear, front, capacity. Do they need to be public?
At the moment, I've got:
public class ShufflingQueue extends MyQueue
{
public ShufflingQueue(int Capacity)
{
super(Capacity);
}
public void enqueue(Object n)
{
super.enqueue(n);
}
public void dequeue()
{
super.dequeue();
}
public int getSize()
{
return super.getSize();
}
}
At the moment, the above is fine and it works as I expect it to. However, this is because I still have enqueue()
and dequeue()
as part of the parent class. I'm supposed to change it so that my shuffling queue and circular queue have different methods for enqueue and dequeue. I know the code to enqueue and dequeue, for both Shuffling and Circular queues but I'm not sure how to have these different methods for the two queues. And how would I be able to use rear
, front
from my parent class. Would I need new class fields for rear, front and capacity in my subclasses as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论