简体   繁体   English

如何在其他类中更改枚举变量的值?

[英]How can I change the value of an enum variable in a different class?

I am currently working on a project that asks me to sort a collection of songs. 我目前正在一个项目中,要求我对一组歌曲进行排序。 I am using the solution presented in this question to accomplish this, while modifying it slightly: Sorting a collection of objects 我正在使用此问题中提出的解决方案来完成此任务,同时对其进行了一些修改: 对对象集合进行排序

import java.util.Comparator;

public class SongComparator implements Comparator<Song> {
    public enum Order {
        YEAR_SORT, RANK_SORT, ARTIST_SORT, TITLE_SORT
    }

    public Order sortingBy;

    @Override
    public int compare(Song song1, Song song2) {
        switch (sortingBy) {
        case YEAR_SORT:
            return Integer.compare(song1.year, song2.year);
        case RANK_SORT:
            return Integer.compare(song1.rank, song2.rank);
        case ARTIST_SORT:
            return song1.artist.compareTo(song2.artist);
        case TITLE_SORT:
            return song1.title.compareTo(song2.title);
        }
        throw new RuntimeException(
                "Practically unreachable code, can't be thrown");
    }

    public void setSortingBy(Order sortBy) {
            this.sortingBy = sortingBy;
    } 
}

Example sort method: 示例排序方法:

public void sortTitle() {
    SongComparator comparator = new SongComparator();
    SongComparator.Order sortingBy = SongComparator.Order.TITLE_SORT;

      Collections.sort(songs2, comparator);
    }

I want to be able to change the sortingBy variable when each respective sortField method is run, so that way it will run the right compare method. 我希望能够在运行每个相应的sortField方法时更改sortingBy变量,这样它将运行正确的compare方法。 However, the way that I'm defining it in the method is just making a new sortingBy variable without changing the one in the SongComparator class at all. 但是,我在方法中定义它的方式只是制作一个新的sortingBy变量,而sortingBy不更改SongComparator类中的SongComparator

You haven't set the sortingBy field of the comparator: 您尚未设置比较器的sortingBy字段:

comparator.setSortingBy(sortingBy);
Collections.sort(songs2, comparator);

You'd better pass this value as a constructor parameter, to make sure the comparator is always in a valid state, and that you don't reproduce the bug: 您最好将此值作为构造函数参数传递,以确保比较器始终处于有效状态,并且不要重现该错误:

SongComparator comparator = new SongComparator(SongComparator.Order.TITLE_SORT);
Collections.sort(songs2, comparator);

The sortingBy field shouldn't be public, by the way, and there should be no setter for the sortingBy field. sortingBy场不应该是公开的,对了,还有应该是没有setter sortingBy场。

To make the class easier and friendlier to use, I would create factory methods: 为了使类更易于使用和友好,我将创建工厂方法:

public static SongComparator byTitle() {
    return new SongComparator(SongComparator.Order.TITLE_SORT);
}

public static SongComparator byYear() {
    return new SongComparator(SongComparator.Order.YEAR_SORT);
}
...

So that the code in the callers become: 这样,调用者中的代码将变为:

Collections.sort(songs2, SongComparator.byTitle());

Note that with Java 8, this comparator class becomes unnecessary. 请注意,在Java 8中,不需要此比较器类。 You can simply use 您可以简单地使用

Collections.sort(songs2, Comparator.comparing(Song::getTitle));

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

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