不能对非静态字段coursestoselect进行静态引用
程序完全按照视频输入,运行时出现异常提示不能对非静态字段coursestoselect进行静态引用,求解决方案
程序完全按照视频输入,运行时出现异常提示不能对非静态字段coursestoselect进行静态引用,求解决方案
2017-02-15
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();
}
}
举报