不能对非静态字段coursestoselect进行静态引用

来源:4-4 学生选课---添加课程 Ⅰ

qq_夜未央_13

2017-02-15 08:35

程序完全按照视频输入,运行时出现异常提示不能对非静态字段coursestoselect进行静态引用,求解决方案        

写回答 关注

3回答

  • Hiro丶alex
    2017-03-15 14:10:01

    楼主看下我擅自修改的代码,看看有没有违背楼主的意思,首先我把你的 Iterator it =  CourseToSelect.iterator();改成了TestCourse it =  new TestCourse();然后还有你的一个while循环,我改过后是正常运行了,楼主的循环不知道有没有问题,不过,Iterator it =  CourseToSelect.iterator();改成TestCourse it =  new TestCourse();没错吧,后面的课程我也没看,我要是理解错了,莫怪哈

  • Hiro丶alex
    2017-03-15 14:05:55

    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();

    }

    }


  • ziom
    2017-02-15 09:05:44

    代码贴出来看看

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409792 学习 · 4340 问题

查看课程

相似问题