3 回答
TA贡献1943条经验 获得超7个赞
您正在覆盖args变量。这应该是这样的:
public class StatDriver
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double total;
double mean;
double standardDeviation;
double average;
total = 0;
String[] possibleArgs = new String[2];
possibleArgs[0] = "mean";
possibleArgs[1] = "std";
ArrayList<Double> data = new ArrayList<Double>();
while(input.hasNextDouble())
{
data.add(input.nextDouble());
}
if(possibleArgs[0].equals(args[0]))
{
mean = Stats.mean(data);
System.out.println("Mean: " + mean);
}
else if(possibleArgs[1].equals(args[1]))
{
standardDeviation = Stats.stdDev(data);
System.out.println("StdDev: " + standardDeviation);
}
else
{
mean = Stats.mean(data);
System.out.println("Mean: " + mean);
}
}
}
TA贡献1946条经验 获得超3个赞
使用 Java 反射特性时,解决方案简单易行。这是代码
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Scanner;
public class StatDriver {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
ArrayList<Double> data = new ArrayList<Double>();
while(input.hasNextDouble()) {
data.add(input.nextDouble());
}
int length = args.length;
if(length == 0) {
double mean = Stats.mean(data); // By default , mean method is called
System.out.println(" Result (Mean) :"+mean);
}
else if(length > 0) {
String methodName = args[0];
try {
Method method = Stats.class.getMethod(methodName, ArrayList.class);
method.setAccessible(true);
Object result = method.invoke(null , data);
System.out.println("Result of the method :"+methodName+" is "+result);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
class Stats{
public static double mean(ArrayList aList) {
double result = 0.0D;
//Logic here
return result;
}
public static double stdDev(ArrayList aList) {
double result = 0.0D;
//Logic here
return result;
}
}
TA贡献1796条经验 获得超4个赞
如何在其中使用一个interface与类的方法具有相同覆盖签名的方法Stat:
interface StatsOperation{
double compute(List<Double> data);
}
而Stat类将如下:
class Stats{
public static double mean(List<Double> data){
System.out.println("mean invoked");
//compute mean
}
public static double stdDev(List<Double> data){
System.out.println("stddev invoked");
//compute std dev
}
}
现在在 main 方法中,您可以使用 aMap来保存类的方法引用Stats(假设您使用的是Java-8):
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Map<String, StatsOperation> lookup = new HashMap<>();
lookup.put("mean", Stats::mean); //"mean" corresponds to Stats::mean implementation
lookup.put("stddev", Stats::stdDev); //"stddev" corresponds to Stats::mean implementation
ArrayList<Double> data = new ArrayList<Double>();
while(input.hasNextDouble())
{
data.add(input.nextDouble());
if(data.size() == 5) break; //breaking after 5 elements
}
StatsOperation operation = args.length > 0 && lookup.get(args[0]) != null ? lookup.get(args[0]) : lookup.get("mean"); //by default calling mean if nothing is sent in args array
double result = operation.compute(data);
System.out.println("Output " + result);
// other business logic below
}
因此,根据您在args数组中传递的值,我们将通过从Map. 这采用运行时多态性来决定在运行时调用哪个方法。
添加回答
举报