public class Demo { /**
 * @param args
 */
public static void main(String[] args) {
MyThis a = new MyThis(); 
        a.setId(12);
        a.getId();
        
        MyThis b = new MyThis();
        b.setId(13);
        b.getId();
}class MyThis{
private int id;

public void setId(int id){
this.id = id;
}

public void getId(){
System.out.println(this.id);
}
}
}

解决方案 »

  1.   

    public class Demo { 
    class MyThis{ 
    private int id; 
    public void setId(int id){ 
    this.id = id; 

    public void getId(){ 
    System.out.println(this.id); 

    } public static void main(String[] args) { 
    Demo d = new Demo();
    MyThis a = d.new MyThis();  
            a.setId(12); 
            a.getId(); 
             
            MyThis b = d.new MyThis(); 
            b.setId(13); 
            b.getId(); 
    }
    }
      

  2.   

    在内部类的前面加上修饰 静态的修饰符static static class MyThis {}
      

  3.   

    public class Demo {  /** 
     * @param args 
     */  
    public static void main(String[] args) { 
            MyThis a = new MyThis();  
            a.setId(12); 
            a.getId(); 
             
            MyThis b = new MyThis(); 
            b.setId(13); 
            b.getId(); 

    }
    class MyThis{ 
    private int id;  public void setId(int id){ 
    this.id = id; 

    public void getId(){ 
    System.out.println(this.id); 

    }
      

  4.   

    MyThis类你现在定义成Demo的内部类了。因而代码这样写是不对的。
    改正方法:
    方法一:将MyThis类从Demo类中移出来。不定义为内部类。
    方法二:若一定要定义成内部类,则代码改成:
    Demo d = new Demo();
    MyThis a = d.new MyThis(); 
    及MyThis b= d.new MyThis(); 
      

  5.   

    public class Test4 {

    public static void main(String[] args) {
    Test4 t = new Test4();
    MyThis a = t.new MyThis(); //内部类
    a.setId(12);
    a.getId();
    MyThis b = t.new MyThis();
    b.setId(13);
    b.getId();
    } class MyThis {
    private int id; public void setId(int id) {
    this.id = id;
    } public void getId() {
    System.out.println(this.id);
    }
    }
      

  6.   

    MyThis类为什么不能定义成Demo的内部类