如何遍历对象数组并调用对象的方法?

时间:2017-03-01 00:37:46

标签: java arrays methods

这是我的代码。如果可能的话,我希望它不那么笨重。足球和篮球是球员的子类。 arr [i] .methodName()仅适用于超类中的方法,我希望在可能的情况下以相同的方式调用子类中的方法。

import java.util.Arrays;
    public class PlayerClient {
        public static void main(String[] args) {
            Soccer s1 = new Soccer("Robert", 12, 200000, 3, 1, 17);
            Soccer s2 = new Soccer("Carrot", 7, 400000, 1, 4, 35);
            Basketball b1 = new Basketball("Jeremy", 37, 300000, 70, 6.3);
            Basketball b2 = new Basketball("Lebron", 65, 1000000, 130, 9.0);
            Player[] arr = {s1, s2, b1, b2};
            for (int i = 0; i < arr.length; i++) {
                System.out.println("\n\n" + arr[i].getName());
                System.out.println(arr[i].getNumber());
                System.out.println(arr[i].getSalary());
                if (arr[i].equals(s1)) {
                    System.out.println(s1.getYellowFlags());
                    System.out.println(s1.getRedFlags());
                    System.out.println(s1.getGoalsScored() + "\n\n");
                } else if (arr[i].equals(s2)) {
                    System.out.println(s2.getYellowFlags());
                    System.out.println(s2.getRedFlags());
                    System.out.println(s2.getGoalsScored() + "\n\n");
                } else if (arr[i].equals(b1)) {
                    System.out.println(b1.getjumpHeight());
                    System.out.println(b1.getjumpHeight() + "\n\n");
                } else if (arr[i].equals(b2)) {
                    System.out.println(b2.getjumpHeight());
                    System.out.println(b2.getBasketsScored() + "\n\n");
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为你要找的是模板方法设计模式:

https://www.tutorialspoint.com/design_pattern/template_pattern.htm

在基类上定义抽象方法,以强制它们在子类中实现。然后,您可以在基类型上调用该方法,而每个子类可能有不同的实现。

import java.util.Arrays;
public class PlayerClient {
    public static void main(String[] args) {
        Soccer s1 = new Soccer("Robert", 12, 200000, 3, 1, 17);
        Soccer s2 = new Soccer("Carrot", 7, 400000, 1, 4, 35);
        Basketball b1 = new Basketball("Jeremy", 37, 300000, 70, 6.3);
        Basketball b2 = new Basketball("Lebron", 65, 1000000, 130, 9.0);
        Player[] arr = {s1, s2, b1, b2};
        for (int i = 0; i < arr.length; i++) {
            System.out.println("\n\n" + arr[i].getName());
            System.out.println(arr[i].getNumber());
            System.out.println(arr[i].getSalary());
            System.out.println(arr[i].printScore());
        }
    }
}


public abstract class Player {
    abstract void printScore();

    // existing code
}

public class Soccer extends Player {
    @Override        
    void printScore() {
        System.out.println(getYellowFlags());
        System.out.println(getRedFlags());
        System.out.println(getGoalsScored() + "\n\n");
    }

    // existing methods
}

public class Basketball extends Player {
    @Override        
    void printScore() {
        System.out.println(getjumpHeight());
        System.out.println(getjumpHeight() + "\n\n");
    }

    // existing methods
}