包含接口对象作为参数的数组的Java方法

时间:2015-10-25 23:50:24

标签: java interface

所以我有一个接口,它由一个名为Vehicle的类实现,它接受一个double值作为参数:

public class Vehicle implements Efficiency
{
  //instance variable
  private double efficiency;

  public Vehicle(double x)
  {
  //parameter initializes the instance variable 
  x = efficiency; 
}

这是界面:

public interface Efficiency 
{
  double getEfficiency(); 
}

我需要创建一个名为getFirstBelowT()的方法,它接受一个效率数组和一个double作为参数。该方法应该返回一个Efficiency对象,该对象是数组中第一个小于参数中double值的对象。如何将效率数组中的元素与double值进行比较?这就是我到目前为止所做的:

//a method that returns an Efficiency object that is the first
//object in the array with a efficiency below the double parameter

public static Efficiency getFirstBelowT(Efficiency[] x, double y)
{ 
    //loop through each value in the array
    for(Efficiency z: x)
    {
        //if the value at the index is less than the double
        if(z < y)
        {   //returns the first value that is less than y and stops looping
            // through the array.
            return z; 
            break; 
        }
    }
    //returns null if none of the element values are less than the double                
    return null;
}

1 个答案:

答案 0 :(得分:1)

你基本上就在那里:

  1. 您需要在条件中调用if(z.getEfficiency() < y) 方法:

    break
  2. return:无法访问的代码之后摆脱javax.servlet.UnavailableException: com.google.appengine.api.modules.ModulesException: Unknown error: '4' at org.mortbay.jetty.servlet.ServletHolder.makeUnavailable(ServletHolder.java:415) at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:458) at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166) at com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:37) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.java:60) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at