首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用包含接口对象的数组作为参数的Java方法

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

Stack Overflow用户
提问于 2015-10-26 07:50:24
回答 1查看 100关注 0票数 1

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

代码语言:javascript
复制
public class Vehicle implements Efficiency
{
  //instance variable
  private double efficiency;

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

这是一个界面:

代码语言:javascript
复制
public interface Efficiency 
{
  double getEfficiency(); 
}

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

代码语言:javascript
复制
//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;
}
EN

回答 1

Stack Overflow用户

发布于 2015-10-26 07:59:59

你基本上做到了:

  1. 你需要在条件中调用getEfficiency方法:

if(z.getEfficiency()

  • 去掉break之后的return:这是无法访问的代码。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33336450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档