Java泛型类型-无法将泛型类型用作参数 公共类铅笔{ 私人T[]a; 公共铅笔(T[]a){ 这个.a=(T[])a; } 公用铅笔1(){ 对于(int i=0;i
您感到困惑,因为您对实例及其属性使用了相同的名称Java泛型类型-无法将泛型类型用作参数 公共类铅笔{ 私人T[]a; 公共铅笔(T[]a){ 这个.a=(T[])a; } 公用铅笔1(){ 对于(int i=0;i,java,arrays,class,generics,types,Java,Arrays,Class,Generics,Types,您感到困惑,因为您对实例及其属性使用了相同的名称queue 对于pencil存在的唯一构造函数将T[]作为参数,您可以这样使用它 public class pencil<T> { private T []a; public pencil(T[] a) { this.a=(T[]) a; } public void pencil1() { for (int i = 0;i<a.length;i++) { if (a[i]== "
queue
对于pencil
存在的唯一构造函数将T[]
作为参数,您可以这样使用它
public class pencil<T> {
private T []a;
public pencil(T[] a) {
this.a=(T[]) a;
}
public void pencil1() {
for (int i = 0;i<a.length;i++) {
if (a[i]== "pen") {
System.out.println("pen");
}
}
}
public class objQueue<T> {
private T[] queue;
private int frontIndex;
private int backIndex;
@SuppressWarnings("unchecked")
public objQueue() {
T[] Queue1=(T[]) new Object[10];
queue=Queue1;
frontIndex=-1;
backIndex=-1;
}
public void enqueue(T newEntry) {
if (isFull()) {
System.out.println("Queue is full");
}
else {
if(frontIndex== -1) {
frontIndex=0;
}
backIndex =(backIndex+1)% queue.length;
queue[backIndex]= newEntry;
}
}
public class main {
static objQueue<Object> queue=new objQueue<Object> ;
static pencil pen=new pencil(queue); //it gives error that The constructor pencil(objQueue<Object>) is undefined
public static void main(String[] args) {
queue.enqueue("pen")
pen.pencil1();
}
static objQueue queue=new objQueue;
//您需要为队列添加一个getter
静态铅笔笔=新铅笔(queue.getQueue());//或queue.queue
顺便说一句,如果你开始使用camelcase作为你的类名
objQueue->objQueue
和pencil->pencil
的话,你的代码也会更符合标准。如果你有一个无参数的构造函数和一个带数组的构造函数,为什么你希望它知道什么是没有参数的呢o当您传入一个objQueue
对象时该怎么办?此代码是可疑的。这些语句中的一些肯定不会因语法错误而编译。不遵循公认的命名约定也不是一个好主意。
static objQueue<Object> queue = new objQueue<Object> ;
// You'll need to have a getter added for queue
static pencil pen=new pencil(queue.getQueue()); // or queue.queue