日期:2014-05-20  浏览次数:20741 次

一段代码,两个类之间
废话不多,先上代码:
Java code
public class DbStore 
{
    private UserEntity[] entryArr;//声明一个数组变量

    public DbStore() //初始化用户实体数组
    {
        UserEntity ue1 = new UserEntity();
        ue1.setAge(21);
        ue1.setCellPhone("888888881");
        ue1.setEmail("damo1@qq.com");
        ue1.setRealName("大漠1");
        ue1.setUserId("001");
        ue1.setUserName("damo1");
        ue1.setUserPwd("1");

        UserEntity ue5 = new UserEntity();
        ue5.setAge(25);
        ue5.setCellPhone("888888885");
        ue5.setEmail("damo5@qq.com");
        ue5.setRealName("大漠5");
        ue5.setUserId("005");
        ue5.setUserName("damo5");
        ue5.setUserPwd("5");

        setEntryArr(new UserEntity[] { ue1, ue2, ue3, ue4, ue5 });
    }
    public static void main(String[] args) 
    {      
        DbStore uf = new DbStore();
        for(int i=0;i<uf.getEntryArr().length;i++)
        {
            System.out.println("用户编号:"+uf.getEntryArr()[i].getUserId());
            System.out.println("登陆名称:"+uf.getEntryArr()[i].getUserName());
            System.out.println("登陆密码:"+uf.getEntryArr()[i].getUserPwd());
            System.out.println("真实姓名:"+uf.getEntryArr()[i].getRealName());
            System.out.println("年龄:"+uf.getEntryArr()[i].getAge());
            System.out.println("手机号码:"+uf.getEntryArr()[i].getCellPhone());
            System.out.println("电子邮箱:"+uf.getEntryArr()[i].getEmail());
            System.out.println("\r\n");
            
        }
    }
    public void setEntryArr(UserEntity[] entryArr) {
        this.entryArr = entryArr;
    }
    public UserEntity[] getEntryArr() {
        return entryArr;
    }
}



Java code
package User;
import java.util.Scanner;
public class UserManager 
{
    public static void main(String[] args) 
    {
        DbStore abc = new DbStore();
        
        Scanner input = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name = input.next();
        System.out.println("请输入密码:");
        String password = input.next();
        
        if((abc.getEntryArr()[i].userName!= name)||(abc.getEntryArr()[i].userPwd != password))
        {
            System.out.println("用户不存在或密码错误!");
        }
        else
        {
            System.out.println("该用户的真实姓名为"+abc.getEntryArr()[i].realName);
        }
    }
}


问题就出在下面一段,那个条件判定,报错是The field UserEntity.userName is not visible

求解答

------解决方案--------------------
贴一下这个类:UserEntity

UserEntity类的userName属性是public的吗。还是其它??
------解决方案--------------------
if((abc.getEntryArr()[i].getUserName!= name)||(abc.getEntryArr()[i].getUserPwd != password))
改成这样应该可以了。
------解决方案--------------------
输出
System.out.println(abc.getEntryArr()[i].userName);
看看能得到啥??
------解决方案--------------------
if((abc.getEntryArr()[i].userName!= name)||(abc.getEntryArr()[i].userPwd != password))
报错是The field UserEntity.userName is not visible
说明UserEntity类的userName属性是非public的,所以不能直接访问
可以通过方法来访问
if((!abc.getEntryArr()[i].getUserName().equals(name))||(!abc.getEntryArr()[i].getUserPwd().equals(password)))
注意字符串的比较用! equals,而不是用 !=




------解决方案--------------------