class Df
{
float a,b;
Df(int x,int y)
{
a=x;
b=y;
}
public static void main(String args[])
{
Df gg=new Df(2,3);
System.out.println("a="+this.a+"b="+this.b);  //this就是指的是gg , 为什么编译出错,不解
}
}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【sure2003】截止到2008-06-27 16:11:46的历史汇总数据(不包括此帖):
    发帖数:188                发帖分:8827               
    结贴数:188                结贴分:8827               
    未结数:0                  未结分:0                  
    结贴率:100.00%            结分率:100.00%            
    敬礼!
      

  2.   

      public static void main(String args[])
        {
            Df gg=new Df(2,3);
            System.out.println("a="+this.a+"b="+this.b);  //this就是指的是gg , 为什么编译出错,不解
        }this.只能用在实例方法中,不能用在类方法中
      

  3.   

    在静态函数中不能够使用this这个关键字的
    你在main方法中用this,所以他报错了啊
      

  4.   

    main时静态方法,当有类的时候,就有他了。但是有他的时候没有实例呀。所以,他就不明白。所以就报错了。
      

  5.   

    this比较常用在类字段与构造函数传参数的时候吧~
    我经常就是,看起来更容易理解
      

  6.   

    必须用this关键字的三种情况:    1、我们想通过构造方法将外部传入的参数赋值给类的成员变量,构造方法的形式参数名称与类的成员变量名相同。例如:         class Person
             {
                  String name;
                  public Person(String name)
                  {
                       this.name = name;
                  } 
             }    2、假设有一个容器类和一个部件类,在容器类的某个方法中要创建部件类的实例对象,而部件类的构造方法要接受一个代表其所在容器的参数。例如:         class Container
             {
                  Component comp;
                  public void addComponent()
                  {
                       comp = new Component(this);
                  }
             }
             class Component
             {
                  Container myContainer;
                  public Component(Container c)
                  {
                       myContainer = c;
                  }
             }    3、构造方法是在产生对象时被java系统自动调用的,我们不能再程序中像调用其他方法一样去调用构造方法。但我们可以在一个构造方法里调用其他重载的构造方法,不是用构造方法名,而是用this(参数列表)的形式,根据其中的参数列表,选择相应的构造方法。例如:         public class Person
             {
                  String name;
                  int age;
                  public Person(String name)
                  {
                       this.name = name;
                  }
                  public Person(String name,int age)
                  {
                       this(name);
                       this.age = age;
                  }
             }
      

  7.   

    class Df
    {
        float a,b;
        Df(int x,int y)
        {
            a=x;
            b=y;
        }
        public static void main(String args[])
        {
            Df gg=new Df(2,3);
            System.out.println("a="+gg.a+"b="+gg.b);  //this就是指的是gg , 为什么编译出错,不解
        }
    }
    LZ想使用的是this指代当前对象,只有在类方法中这样指代才有意义。如果你定义了:
    Df gg1=new Df(2,3);
    Df gg2=new Df(4,5);
    你在main中用this 指代,你是编译器的话你知道是指的gg1还是gg2?
      

  8.   

    this和对象的实例是联系在一起的,如果this出现的地方可以在对象不被实例化时就运行,那么就会报错
      

  9.   

    class Df
    {
        static float a,b;
        Df(int x,int y)
        {
            a=x;
            b=y;
        }
        public static void main(String args[])
        {
            //Df gg=new Df(2,3);
            System.out.println("a="+a+"b="+b);  
        }
    }
    你在main方法中去调用,a,b当然用不了了,静态方法是不以调用非静态变量的。你写成上面的那这样,就可以了。
      

  10.   

    static 方法哪里来的 this.
      

  11.   

    staitc方法中是不能用this的不对吧可以用像这样
    package date;public class Test {
        String b;
    /**
     * @param args
     */
    public static void main(String[] args) {

    System.out.println(new Test().b);
    }}
      

  12.   

    this不能在静态方法中使用。
    并且楼主程序里的this 也不就是gg对象。