qq_夜未央_13
2017-02-15 08:35
程序完全按照视频输入,运行时出现异常提示不能对非静态字段coursestoselect进行静态引用,求解决方案
楼主看下我擅自修改的代码,看看有没有违背楼主的意思,首先我把你的 Iterator it = CourseToSelect.iterator();改成了TestCourse it = new TestCourse();然后还有你的一个while循环,我改过后是正常运行了,楼主的循环不知道有没有问题,不过,Iterator it = CourseToSelect.iterator();改成TestCourse it = new TestCourse();没错吧,后面的课程我也没看,我要是理解错了,莫怪哈
package com.imooc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class TestCourse {
public List CourseToSelect;
public TestCourse(){
this.CourseToSelect =new ArrayList();
}
@SuppressWarnings("unchecked")
public void TestAdd(){
Course c1 = new Course("1","数据结构");
CourseToSelect.add(c1);
Course temp1 = (Course)CourseToSelect.get(0);
System.out.println("添加的课程是:"+temp1.id+":"+temp1.name);
Course c2 = new Course("2","C语言");
CourseToSelect.add(0,c2);
Course temp2 = (Course)CourseToSelect.get(0);
System.out.println("添加的课程是:"+temp2.id+":"+temp2.name);
Course[] course1 = {new Course("3","离散数学"),new Course("4","汇编语言")};
CourseToSelect.addAll(Arrays.asList(course1));
Course temp3 = (Course)CourseToSelect.get(2);
Course temp4 = (Course)CourseToSelect.get(3);
System.out.println("添加的两门课程是:"+temp3.id+":"+temp3.name+temp4.id+":"+temp4.name);
Course[] course2 = {new Course("5","高等数学"),new Course("6","大学英语")};
CourseToSelect.addAll(2,Arrays.asList(course2));
Course temp5 = (Course)CourseToSelect.get(2);
Course temp6 = (Course)CourseToSelect.get(3);
System.out.println("添加的两门课程是:"+temp5.id+":"+temp5.name+temp6.id+":"+temp6.name);
}
public static void main(String[] args){
TestCourse it = new TestCourse();//这里提示错误不能对非静态字段 CourseToSelect 进行静态引用
it.TestAdd();
}
}
代码贴出来看看
Java入门第三季
409792 学习 · 4340 问题
相似问题