"testvector.java": Error #: 303 : 'this' cannot be referenced from a static context

handsome1234 2003-03-03 12:21:27
) package testvector;
import java.util.Vector;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/

public class testvector {
public testvector() {
}

public static void main(String[] args) {
testvector testvector1 = new testvector();
int i = 0;
Vector userList = new Vector(10,5);
for(i=0; i<17; i++)
{
String username = "user"+i;
String password = "password"+i;
User user = new User (username, password); //错误定位在这里,
//可这里实在是没有错啊???
userList.add(user);
}
System.out.println("number of users"+userList.size());
for(i=0; i<17; i++)
System.out.println("user name at "+i+":"+((User)userList.elementAt(i)).getName());
//此处的向下强制类型转换要注意
}


class User
{
String username;
String password;

public User(String name, String passwd)
{
username = name;
password = passwd;
}

public String getName()
{
return username;
}
}

}

...全文
161 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaise 2003-03-03
  • 打赏
  • 举报
回复
改成这样
User user = testvector1.new User (username, password);
dtmark 2003-03-03
  • 打赏
  • 举报
回复
晕,你干吗把User类定为内部类?有必要吗?我没看出什么必要来。

如果非要定义User类为内部类,那声明一个内部类对象也不是这样的。应该是
testvector1.User user=testvector.new User();

或者就把User类定义为static。
Koham 2003-03-03
  • 打赏
  • 举报
回复
好好看看inner class的说明档
EkaEruest 2003-03-03
  • 打赏
  • 举报
回复
要么把你的User类定义为static,要么,把你的User类放到testvector类外面。

因为如果把User放在testvector中,是把它看作类似变量,而在main中调用,必须是static的。

如果放在testvector外面,那是把它看作一个类。
#ifndef TOMCRYPT_H_ #define TOMCRYPT_H_ #include #include #include #include #include #include #include /* use configuration data */ #include #ifdef __cplusplus extern "C" { #endif /* version */ #define CRYPT 0x0116 #define SCRYPT "1.16" /* max size of either a cipher/hash block or symmetric key [largest of the two] */ #define MAXBLOCKSIZE 128 /* descriptor table size */ /* Dropbear change - this should be smaller, saves some size */ #define TAB_SIZE 4 /* error codes [will be expanded in future releases] */ enum { CRYPT_OK=0, /* Result OK */ CRYPT_ERROR, /* Generic Error */ CRYPT_NOP, /* Not a failure but no operation was performed */ CRYPT_INVALID_KEYSIZE, /* Invalid key size given */ CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */ CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */ CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */ CRYPT_INVALID_PACKET, /* Invalid input packet given */ CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */ CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */ CRYPT_INVALID_CIPHER, /* Invalid cipher specified */ CRYPT_INVALID_HASH, /* Invalid hash specified */ CRYPT_INVALID_PRNG, /* Invalid PRNG specified */ CRYPT_MEM, /* Out of memory */ CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */ CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */ CRYPT_INVALID_ARG, /* Generic invalid argument */ CRYPT_FILE_NOTFOUND, /* File Not Found */ CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */ CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */ CRYPT_PK_DUP, /* Duplicate key already in key ring */ CRYPT_PK_NOT_FOUND, /* Key not found in keyring */ CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */ CRYPT_INVALID_PRIME_SIZE,/* Invalid size of prime requested */ CRYPT_PK_INVALID_PADDING /* Invalid padding on input */ }; #include #include #include #include #include #include #include #include #include #include #include #ifdef __cplusplus } #endif #endif /* TOMCRYPT_H_ */ /* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt.h,v $ */ /* $Revision: 1.20 $ */ /* $Date: 2006/11/26 01:45:14 $ */

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧