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

如何让另一个类中的方法访问主类中的对象数组?
这是源文件。
import java.util.*;

public class AddressList
{


public static void main(String[] args)
{
People[] apeople = new People[5];
apeople[0] = new People("01", "AAA", "男", "123456","北京");
apeople[1] = new People("02", "BBB", "女", "456789","福建");
apeople[2] = new People("03", "CCC", "男", "555555","广东");
apeople[3] = new People(null, null, null, null, null);
apeople[4] = new People(null, null, null, null, null);

   
Scanner in = new Scanner(System.in);
for(;;)
{
System.out.println("1.查询\n" + "2.增加\n" + "3.修改\n" + "4.删除\n" +"5.退出");
System.out.println("请输入操作:");
 
int ch = in.nextInt();
switch(ch)
{
case 1: query();
break;
case 2: add();
break;
case 3: modify();
break;
case 4: remove();
break;
case 5: exit();
break;
 
}
}

}
}

class People
{
public People(String n1,String n2,String s, String p, String a)
{
num = n1;
name = n2;
sex = s;
phone = p;
address = a;
}

public void query()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the number you want to query: ");
String n = in.nextLine();
for(int i = 0; i < apeople.length; i++)
{
int a = apeople[i].getnum().compareTo(n);
if(a == 0)
{
System.out.println("It has this note");
display();
}
else continue;
}

}
public void display()
{
System.out.println("编号" + " " + "姓名" + " " + "性别" + " " + "电话" + " " +
"地址" );
for(People p: ap)
System.out.println(p.getnum() + " " + p.getname() + " " + p.getsex() + " " + p.getphone() + " "+ p.getaddress());
}
public String getnum()
{
return num;
}

public String getname()
{
return name;
}

public String getsex()
{
return sex;
}

public String getphone()
{
return phone;
}

public String getaddress()
{
return address;
}
private String num ;

private String name ;

private String sex ;

private String phone ;

private String address ;
}

编译器指出在query方法和display方法中无法解析apeople,就是说访问不了主类中apeople数组。请教高手应如何解决这个问题?


------解决方案--------------------
楼主需要重新设计,不然问题会很多,远远超过你所预料。
就算是“学c/c++出身”,这种设计也是很糟糕的。
我重新设计了你的程序,你可以比较一下不同之处,分析一下那种设计方法更好:
Java code

public class AddressList {
    
    private People[] peoples;
    private int top;
    private java.util.Scanner in = new java.util.Scanner(System.in); 
    
    public AddressList(int size) {
        peoples = new People[size];
    }
    
    public boolean addPeople(People p) {
        if (top < peoples.length) {
            peoples[top++] = p;
            return true;
        }
        return false;
    }
    
    public String toString() {
        if (top == 0) return "无记录";
        StringBuffer result = new StringBuffer();
        result.append("编号\t姓名\t性别\t电话\t地址\n");
        for (int i = 0; i < top; i++)
            result.append(peoples[i].toString() + '\n');
        return result.toString();
    }
    
    public void prompt() {
        System.out.println("1.查询\n2.增加\n3.修改\n4.删除\n5.退出");
        System.out.println("请输入操作:");
        switch(in.nextInt()) {
        case 1: // 查询
            System.out.println(this);
            prompt();
            break;

        case 2: // 添加
            in.nextLine();
            if (addPeople(new People(in.nextLine(),
                    in.nextLine(),
                    (in.nextLine().equals("男") ? People.Sex.MALE : People.Sex.FEMALE),
                    in.nextLine(),
                    in.nextLine()))) System.out.println("添加成功");
            else System.out.println("添加失败");
            prompt();
            break;

        case 3: // 查询
            break;

        case 4: // 删除
            break;

        case 5: // 退出
            break;
        }
    }

    public static void main(String[] args) {
        AddressList al = new AddressList(5);
        al.prompt();
    }

}

class People {
    
    public enum Sex { MALE, FEMALE }
    
    private String num;
    private String name;
    private Sex sex;
    private String phone;
    private String address;
    
    public People(String n1, String n2, Sex s, String p, String a) {
        num = n1;
        name = n2;
        sex = s;
        phone = p;
        address = a;
    }

    public String getnum() {
        return num;
    }

    public String getname() {
        return name;
    }

    public Sex getsex() {
        return sex;
    }

    public String getphone() {
        return phone;
    }

    public String getaddress() {
        return address;
    }
    
    public String toString() {
        return num + '\t' + name + '\t' + (sex==Sex.MALE?'男':'女') + '\t' + phone + '\t'+ address;
    }
    
}