简体   繁体   English

在java中添加类的值

[英]adding values from a class in java

There was a question in my book that said this (there are no answers): 我的书中有一个问题说(没有答案):

Suppose we have a class Beta declared with the header: 假设我们有一个使用标头声明的类Beta:

 class Beta extends Alpha 

so Beta is a subclass of Alpha, and in class Alpha there is a method with header: 所以Beta是Alpha的子类,而在Alpha类中有一个带头的方法:

 public int value(Gamma gam) throws ValueException 

Write a static method called addValues which takes an object which could be of type ArrayList<Alpha> or ArrayList<Beta> , and also an object of type Gamma . 编写一个名为addValues的静态方法,它接受一个可以是ArrayList<Alpha>ArrayList<Beta>类型的对象,以及一个Gamma类型的对象。 A call to the method addValues must return the sum obtained by adding the result of a call of value on each of the objects in the ArrayList argument, with the Gamma argument as the argument to each call of value. 对方法addValues的调用必须返回通过在ArrayList参数中的每个对象上添加值调用的结果而获得的总和,其中Gamma参数作为每次值调用的参数。 Any call of value which causes an exception of type ValueException to be thrown should be ignored in calculating the sum. 在计算总和时,应忽略任何导致抛出ValueException类型异常的值调用。

My Attempt: 我的尝试:

public static int addValues(ArrayList<? extends Alpha> arr, Gamma gam) {
    int sum = 0;
    for (int i = 0; i < arr.size(); i++) {
        try {
            sum += arr.get(i) + gam;
        } catch (Exception e) {
            i++;
        }
    }
    return sum;
}

Although I know for starters that the line sum += arr.get(i) + gam is going to give me an error, because they are not straight forward int s that can be added. 虽然我知道对于初学者来说,行sum += arr.get(i) + gam会给我一个错误,因为它们不是可以添加的直接int The book provides no more information on the question so what I have written here is everything required for the question. 这本书没有提供关于这个问题的更多信息,所以我在这里写的是问题所需的一切。

You are supposed to call the value method in order to get the values you are supposed to be adding. 您应该调用value方法以获取您应该添加的值。

Beside that, you can use the enhanced for loop to make your code cleaner. 除此之外,您还可以使用增强的for循环来使代码更清晰。

public static int addValues(ArrayList<? extends Alpha> arr, Gamma gam) 
{
    int sum = 0;
    for (Alpha alpha : arr) {
        try {
            sum += alpha.value(gam);
        } catch (ValueException e) {
        }
    }
    return sum;
}

Use wild card with bounded type as Alpha as you want to access the value then use arr. 使用有界类型的通配符作为Alpha,因为您想要访问该值然后使用arr。

public static int addValues(ArrayList<? extends Alpha> arr, Gamma gam) 
{
    int sum = 0;
    for (Alpha alpha : arr) {
        try {
            sum += alpha.value(gam);
        } catch (Exception e) {
        }
    }
    return sum;
}

Regarding the second question of overriding the equal function 关于压倒同等职能的第二个问题

public boolean equals(Gamma g) {
        if(g!= null && g instanceof Alpha && value(g) == value(this.myGam)){
            return true;
        }else{
            return false;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM