喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 帮助

RSS 订阅当前论坛  

喜悦证交所已经关闭

上一主题 下一主题
     
标题: java中this使用的问题  
 
kayej (kayej)
注册会员
Rank: 2
中级会员



UID 27025
精华 0
积分 155
帖子 153
金钱 155 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2004-3-17
状态 离线
java中this使用的问题

我在使用this出现了问题:
我的代码如下:

/*
* RangeClass.java
*
* Created on 2005年4月3日, 下午8:33
*/

package lesson5;

/**
*
* @author  Administrator
*/
public class RangeClass {
    int test = 10;
    /** Creates a new instance of RangeClass */
    public RangeClass() {
    }
    int[] makeRange (int lower,int upper){
        int[] arr = new int[(upper-lower) + 1];
        for (int i=0; i<arr.length; i++)
            arr[i] = lower++;
        return arr;
    }
    void printTest(){
        int test = 20;
        System.out.println("Test:" + this.test);
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int[] theArray;
        RangeClass theRange = new RangeClass();

        theArray = theRange.makeRange(5, 10);
        System.out.print("The array: [");
        for (int i=0; i<theArray.length; i++)
              System.out.print(theArray[i] + " ");
        System.out.println("]");     
        theRange.printTest();
        System.out.println("Test:" + this.test); //如果我不运行这句就对了
    }   
}
报错:
lesson5/RangeClass.java [42:1] non-static variable this cannot be referenced from a static context
        System.out.println("Test:" + this.test);
                                     ^
1 error
Errors compiling test.

问题:
1.如果我要在main函数中调用this.test怎么操作?
2.是不是在static的函数中都不能用this.test调用吗?如果不能,怎么调用
3.我吧:
public class RangeClass {
    int test = 10;
改成了:
public class RangeClass {
   static int test = 10;
也不行

如果要调用static的变量,怎么调用?
2005-5-23 09:58 PM#1
查看资料  发短消息  顶部
 
雨伞 (时间过的真快)
版主
Rank: 7Rank: 7Rank: 7
CTO


UID 25085
精华 6
积分 29921
帖子 4256
金钱 29861 喜悦币
威望 60
人脉 0
阅读权限 100
注册 2003-12-8
来自 梅州
状态 离线
[推荐阅读] 不用gd库,能否实现图片这样的尺寸缩放?
static修饰的main方法在运行的时候实际上还没有this的存在。

static int test = 10;
...
System.out.println("Test:" + test);




------------------------------
寻找兼职!
------------------------------
2005-5-23 11:52 PM#2
查看资料  Blog  发短消息  QQ . .   顶部
 
tokeng (tokeng)
新手上路
Rank: 1
初级会员



UID 63579
精华 0
积分 8
帖子 8
金钱 8 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2005-3-19
状态 离线
[推荐阅读] 用php的类发送邮件没有类容
[发现]一个this的用法

类的成员变量名与当前方法中的形参名或者
与当前方法中的局部变量名相同时,就要求
用this关键字引用类的成员变量。
2005-5-30 04:40 PM#3
查看资料  发短消息  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.023115 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP