一:报错分析
如果你想定义一个外部类 但 定义成 内部类 了,但是你还是按照 外部类来实例化对象 就会报这个错误。
二:代码分析
报错码
package cn.wyj.one;
public class Demo1_泛型 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Mycollection my = new Mycollection();//此处会报错
my.set("wyj", 0);
my.set(1000, 1);
Integer b = (Integer) my.get(1);//
}
class Mycollection{
Object objs[] = new Object[5];
public void set(Object obj,int index)
{
objs[index] = obj;
}
public Object get(int index)
{
return objs[index];
}
}
}
上方码 如果是想定义一个外部类 则
package cn.wyj.one;
public class Demo1_泛型 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Mycollection my = new Mycollection();
my.set("wyj", 0);
my.set(1000, 1);
Integer b = (Integer) my.get(1);//
}
}
class Mycollection{
Object objs[] = new Object[5];
public void set(Object obj,int index)
{
objs[index] = obj;
}
public Object get(int index)
{
return objs[index];
}
}
如果确实是想定义一个内部类 并想实例化内部类对象 则可以看下方代码
package cn.wyj.one;
public class Demo1_泛型 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo1_泛型 p = new Demo1_泛型();
Mycollection my = p.new Mycollection();
my.set("wyj", 0);
my.set(1000, 1);
Integer b = (Integer) my.get(1);//
}
class Mycollection{
Object objs[] = new Object[5];
public void set(Object obj,int index)
{
objs[index] = obj;
}
public Object get(int index)
{
return objs[index];
}
}
}