在另一个对象java中使用一个对象

时间:2013-12-04 19:20:51

标签: java eclipse

所以我设置一个节点类,显示一个节点和另一个节点之间的链接以找到最短路径。我决定使用一个单独的对象类来存储节点A和B之间的链接,以避免使用二维数组。

在eclipse中保存项目后,就像检查点一样,我没有丢失数据,在DistanceBetween的任何调用下突然出现红线,说该方法在该对象类中不存在,尽管它确实如你所见。

注意:任何粗体部分都会在Node中抛出错误。通常它声明该方法在DistanceBetween中不存在,或者构造函数(int,int)是错误的,而不是。

我应该使用extend,包吗?

public class DistanceBetween{

    private int thisAddress;
    private int distanceBetween;

    public DistanceBetween(int myAddress, int myDistance){
        thisAddress = myAddress;
        myDistance = distanceBetween;
    }

    public int getAddress(){
        return thisAddress;
    }

    public void setAddress(int newAddress){
        thisAddress = newAddress;
    }

    public int getDistance(){
        return distanceBetween;
    }

    public void setDistance(int newDistance){
        distanceBetween = newDistance;
    }

}










public class Node{

    private int address;
    private int distance;
    private Node[] connectedNodes;
    private DistanceBetween[] distances;
    private boolean intersection;

    public Node(int myAddress, Node[] myConnected, DistanceBetween[]
            myDistances, boolean isIntersection){
        address = myAddress;
        connectedNodes = myConnected;
        distances = myDistances;
        intersection = isIntersection;
    }

    public int getThisAddress(){
        return address;
    }

    public void setThisAddress(int newAddress){
        address = newAddress;
    }

    public Node[] getConnected(){
        return connectedNodes;
    }

    public void connectTwo(Node a, Node b){
        for(int x = 0; x < a.getConnected().length; x++){
            if(a.getConnected()[x].getThisAddress() == 0){

            }
        }
    }

    public DistanceBetween[] getDistances(){
        return distances;
    }

    public void setDistances(DistanceBetween[] newDistances){
        distances = newDistances;
    }

    public void addLink(Node a, Node b, int thisDistance){
        DistanceBetween[] holderDistanceA = a.getDistances();
        DistanceBetween[] holderDistanceB = a.getDistances();
        int flags = 0;
        for(int x = 0; x < holderDistanceA.length; x++){
            if(holderDistanceA[x].**getAddress()** == 0){
                DistanceBetween aAndB = new **DistanceBetween(b.getThisAddress(),thisDistance);**
                holderDistanceA[x] = aAndB;
                flags++;
                break;
            }

        }

        for(int x = 0; x < holderDistanceB.length; x++){
            if(holderDistanceB[x].**getAddress()** == 0){
                DistanceBetween bAndA = new    **DistanceBetween(a.getThisAddress(),thisDistance);**
                holderDistanceB[x] = bAndA;
                break;
            }

        }

        if(flags < 1){
            System.out.println("Error, cannot add a link, link load is full.");
        }

        a.setDistances(holderDistanceA);
        b.setDistances(holderDistanceB);
    }

    public int getDistanceBetween(Node a, Node b){
        int result = 0;
        for(int x = 0; x < a.getDistances().length; x++){
            if(a.getDistances()[x].**getAddress()** == b.getThisAddress()){
                result = a.getDistances()[x].**getDistance()**;
            }
        }
        return result;
    }

    public boolean equals(Node a, Node b){
        if(a.address == b.address){
            return true;
        }else
            return false;
    }

}

2 个答案:

答案 0 :(得分:0)

您的代码编译得很好,看起来您在不同的包中有2个类。要修复你,可以:

  1. 将2个类放入同一个包中

  2. DistanceBetween包导入Node类。

    import yourpackagename.DistanceBetween;

答案 1 :(得分:0)

确保项目中的其他位置没有名为DistanceBetween的类,或者,如果您这样做,则导入正在使用您要发布的DistanceBetween代码的正确类。

Eclipse中的Open Type工具(Ctrl + Shift + T)允许您搜索工作区中类的位置。