Java 接口中的静态方法
我正在尝试向可测量的接口添加最大和最小的静态方法。 这些方法应该从一系列可测量的国家对象中返回具有最大或最小度量值(双精度)的对象。我尝试在接口中这样做,但有人建议我使用Comparator接口。如何通过使用可测量接口来实现这一点Java 接口中的静态方法,java,oop,interface,Java,Oop,Interface,我正在尝试向可测量的接口添加最大和最小的静态方法。 这些方法应该从一系列可测量的国家对象中返回具有最大或最小度量值(双精度)的对象。我尝试在接口中这样做,但有人建议我使用Comparator接口。如何通过使用可测量接口来实现这一点 class Main { public static void main(String args[]) { Measurable[] countries = new Measurable[3]; countries[0] = n
class Main {
public static void main(String args[]) {
Measurable[] countries = new Measurable[3];
countries[0] = new Country("Uruguay", 176220);
countries[1] = new Country("Thailand", 514000);
countries[2] = new Country("Belgium", 30510);
Measurable maximum = Measurable.largest(countries);
Measurable smallest = Measurable.smallest(countries);
}
}
class Country implements Measurable {
private String name;
private double area;
public Country(String name, double area) {
this.name = name;
this.area = area;
}
}
interface Measurable {
static Measurable largest(Measurable[] countries) {
public static Measurable largest(Measurable[]objects){
if (objects == null || objects.length == 0) {
return new Country("", 0);
}
Measurable max = new Country("", 0);
for (Measurable obj : objects) {
if (obj.getMeasure() > max.getMeasure()) {
max = obj;
}
}
return max;
}
}
static Measurable smallest(Measurable[] objects) {
if (objects == null || objects.length == 0) {
return new Country("", 0);
}
Measurable max = new Country("", 0);
for (Measurable obj : objects) {
if (obj.getMeasure() < min.getMeasure()) {
min = obj;
}
}
return min;
}
}
double getMeasure();
}
主类{
公共静态void main(字符串参数[]){
可测量[]国家=新的可测量[3];
国家[0]=新国家(“乌拉圭”,176220);
国家[1]=新国家(“泰国”,514000);
国家[2]=新国家(“比利时”,30510);
可测量的最大值=可测量的最大值(国家);
可测量最小=可测量。最小(国家);
}
}
阶级国家实行可衡量的政策{
私有字符串名称;
私人双区;
公共国家/地区(字符串名称,双区域){
this.name=名称;
这个面积=面积;
}
}
界面可测量{
静态可测量最大(可测量[]个国家){
公共静态可测量最大(可测量[]对象){
if(objects==null | | objects.length==0){
返回新国家(“,0);
}
可测量最大值=新国家(“,0);
用于(可测量对象:对象){
如果(obj.getMeasure()>最大getMeasure()){
max=obj;
}
}
返回最大值;
}
}
静态可测量最小(可测量[]对象){
if(objects==null | | objects.length==0){
返回新国家(“,0);
}
可测量最大值=新国家(“,0);
用于(可测量对象:对象){
if(obj.getMeasure()
如果要使用Comparator/Comparable,则无需创建可测量接口
只需在国家/地区实现Compariable,然后在数组中循环查找最小值和最大值
class Main {
public static void main(String args[]) {
Country[] countries = new Country[3];
countries[0] = new Country("Uruguay", 176220);
countries[1] = new Country("Thailand", 514000);
countries[2] = new Country("Belgium", 30510);
Country max = null;
Country min = null;
for (Country c : countries) {
if (max == null || max.compareTo(c) < 0) {
max = c;
}
if (min == null || min.compareTo(c) > 0) {
min = c;
}
}
System.out.printf("max: %s (%s)%n", max.name, max.area);
System.out.printf("min: %s (%s)%n", min.name, min.area);
}
}
class Country implements Comparable<Country> {
String name;
double area;
public Country(String name, double area) {
this.name = name;
this.area = area;
}
@Override
public int compareTo(Country other) {
// Returns int <0 if this is smaller than other
// 0 if they are equal
// int >0 if this is greater than other
return Double.compare(this.area, other.area);
}
}
如果仍要使用可测量接口,可以扩展ArrayList并让该类按如下方式实现它:
public static void main(String args[]) {
List<Country> countries = new ArrayList<>();
countries.add(new Country("Uruguay", 176220));
countries.add(new Country("Thailand", 514000));
countries.add(new Country("Belgium", 30510));
Country max = Collections.max(countries);
Country min = Collections.min(countries);
System.out.printf("max: %s (%s)%n", max.name, max.area);
System.out.printf("min: %s (%s)%n", min.name, min.area);
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Main {
public static void main(String args[]) {
CountryList countries = new CountryList();
countries.add(new Country("Uruguay", 176220));
countries.add(new Country("Thailand", 514000));
countries.add(new Country("Belgium", 30510));
Country max = countries.getLargest();
Country min = countries.getSmallest();
System.out.printf("max: %s (%s)%n", max.name, max.area);
System.out.printf("min: %s (%s)%n", min.name, min.area);
}
}
class CountryList extends ArrayList<Country> implements Measurable{
@Override
public Country getSmallest() {
return Collections.min(this);
}
@Override
public Country getLargest() {
return Collections.max(this);
}
}
interface Measurable{
Country getSmallest();
Country getLargest();
}
class Country implements Comparable<Country> {
String name;
double area;
public Country(String name, double area) {
this.name = name;
this.area = area;
}
@Override
public int compareTo(Country o) {
return Double.compare(this.area, o.area);
}
}
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
班长{
公共静态void main(字符串参数[]){
CountryList国家=新的CountryList();
添加(新国家(“乌拉圭”,176220));
添加(新国家(“泰国”,514000));
添加(新国家(“比利时”,30510));
Country max=countries.getmax();
Country min=countries.getminimate();
System.out.printf(“最大:%s(%s)%n”,最大名称,最大面积);
System.out.printf(“最小:%s(%s)%n”,最小名称,最小区域);
}
}
类CountryList扩展了ArrayList{
@凌驾
公共国家/地区(){
return Collections.min(这个);
}
@凌驾
公共国家(地区){
return Collections.max(这个);
}
}
界面可测量{
国家/地区();
国家(最大的);
}
阶级国家实行可比性{
字符串名;
双区;
公共国家/地区(字符串名称,双区域){
this.name=名称;
这个面积=面积;
}
@凌驾
公共国际比较(o国){
返回Double.compare(此区域,o区域);
}
}
这会有帮助吗?不过,我有点想使用可测量的接口。但是,如果不使用可比较的方法,是否有可能?无论如何,您需要将整个国家阵列传递给可测量的接口方法,因此让国家实施它是没有意义的。您可以为List创建一个包装器对象,甚至可以扩展List,并让它实现可测量的接口。