使用非静态方法时遇到问题

时间:2016-05-22 09:59:45

标签: methods constructor instance public

我有这个方法:

public Route getSubroute(int startOffset, int endOffset) {

    if (!(0 <= startOffset && startOffset < endOffset && endOffset <= this
            .getLength())) {
        throw new IllegalArgumentException("Subroute is out of bounds.");
    }
    // the sub-segments that will make up the sub-route
    List<Segment> subsegments = new ArrayList<>();
    // variable offset is the distance along the route to the start of the
    // segment currently being examined
    int offset = 0;
    for (Segment segment : segments) {
        if (startOffset - offset < segment.getLength()
                && 0 < endOffset - offset) {
            // part of the segment belongs on the sub-route
            int subsegmentStartOffset =
                    segment.getStartOffset()
                            + Math.max(startOffset - offset, 0);
            int subsegmentLength =
                    (Math.min(endOffset - offset, segment.getLength()) - 
                            Math.max(startOffset - offset, 0));
            subsegments.add(new Segment(segment.getSection(), segment
                    .getDepartingEndPoint(), subsegmentStartOffset,
                    subsegmentStartOffset + subsegmentLength));
        }
        offset += segment.getLength();
    }
    return new Route(subsegments);
}

当我尝试在调用此方法的新类中创建另一个方法时,我收到错误:

  1. 类路由中的构造函数路由无法应用于给定类型;
  2. 必需:列表
  3. 发现:没有参数
  4. 原因:实际和正式的参数列表长度不同
  5. 我的代码如下,此错误出现在[Route newRoute = new Route()]

    public Route subRouteV(int startOffset, int endOffset){
            Route newRoute = new Route();
            Route vSubR = newRoute.getSubroute(startOffset, endOffset);
        }
    

1 个答案:

答案 0 :(得分:0)

问题来自那条线     Route newRoute = new Route();

你必须

  1. 使用参数
  2. 调用Route的构造函数
  3. Route构建一个不带参数的新构造函数。