单身,枚举和匿名内部班 [英] Singletons, Enums and anonymous inner classes

查看:147
本文介绍了单身,枚举和匿名内部班的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可能知道,有些人正在使用1个枚举的枚举来声明单例,因为JVM保证总是有一个没有并发问题的单一实例来处理...



那么一个具有多个实例的枚举呢?
我们可以说像Enum这样的东西是一种共享一个通用界面的单个有序单元吗?
为什么?

  public enum EnumPriceType {

WITH_TAXES {
@Override
public float getPrice(float input){
return input * 1.20f;
}
public String getFormattedPrice(float input){
return input * 1.20f +€;
}
},

WITHOUT_TAXES {
@Override
public float getPrice(float input){
return input;
}
},
;

public abstract float getPrice(float input);

public static void main(String [] args){
WITH_TAXES.getFormattedPrice(33f);
}

}

在这段代码中, t工作:
WITH_TAXES.getFormattedPrice(33f);
如果在不通过公共接口的情况下不能调用公共方法,那么有什么兴趣?
我想这就是为什么我没有看到任何语法能够为枚举的一个实例声明一个接口。






编辑:



似乎枚举实例是一种特殊类型的匿名类。
因此,我明白为什么你不能调用该方法。



我的问题与...有关:为什么匿名类不能实现界面(除了它可能已经实现的界面!)



我完全明白为什么我们可以这样做:

  Vehicle veh = new Vehicle(){
public String getName(){
returntoto;
}
};
veh.getName();

(getName这里不是覆盖)



为什么我不明白为什么我们不能用匿名类来做这个事情:

  Runnable veh = new Vehicle )实现Runnable {
@Override
public void run(){
System.out.println(i run!);
}
};
veh.run();

或者会导致同样的事情。
想想:如果你不使用匿名类,你可以绝对地扩展Vehicle类,然后使该子类实现你想要的任何其他接口...



我很确定如果有可能,我们可以以类型安全的方式调用WITH_TAXES.getFormattedPrice(33f),因为WITH_TAXES不会是一个真正的EnumPriceType,但它不过是具有自己的接口的EnumPriceType子类,并且通过使用硬编码的WITH_TAXES调用WITH_TAXES.getFormattedPrice(33f),您可以在编译时知道您正在调用哪个EnumPriceType子节点。



所以我的问题是:有什么原因为什么这是不可能的?还是还没有完成?

解决方案

您的枚举等同于以下正常类(实际上,这几乎是编译器变成的): / p>

  public abstract class EnumPriceType {

public static final EnumPriceType WITH_TAXES = new EnumPriceType(){
// getPrice(){...}
// getFormattedPrice(){...}
};

public static final EnumPriceType WITHOUT_TAXES = new EnumPriceType(){
// getPrice(){...}
};

public abstract float getPrice(float input);

public static void main(String [] args){
WITH_TAXES.getFormattedPrice(33f);
}
}

getFormattedPrice() / code>方法在抽象类型上不可用,因此不能从main方法调用。考虑如果main方法被重写为使用局部变量会发生什么:

  public static void main(String [] args) {
EnumPriceType foo = EnumPriceType.WITH_TAXES;
foo.getFormattedPrice(33f);
}

这不编译,因为 getFormattedPrice() / code>在基类上不可用。由于 WITH_TAXES 实例是 EnumPriceType 匿名子类,因此无法定义该局部变量为 getFormattedPrice()方法的类型。



作为元观察,这是强类型语言(如Java)和鸭式语言(如Ruby)之间的关键区别。如果碰巧在这里,Ruby将很乐意地调用 getFormattedPrice()方法,无论 foo 变量



作为另一个元观察结果,对于相同枚举的不同常量来说,这并不太有意义有不同的集合方法。如果您无法将基本枚举类型的抽象(或具体)方法所需的所有内容全部放置,则可能使用错误的工具来解决问题。


As you may know, some people are declaring singletons with an Enum of 1 instance, because the JVM guarantees that there will always be a single instance with no concurrency problems to handle...

Thus what about an Enum with multiple instances? Can we say something like an Enum is a kind of ordered set of singletons sharing a common interface? Why?

public enum EnumPriceType {

    WITH_TAXES {
        @Override
        public float getPrice(float input) {
            return input*1.20f;
        }
        public String getFormattedPrice(float input) {
            return input*1.20f + " €";
        }
        },

    WITHOUT_TAXES {
        @Override
        public float getPrice(float input) {
            return input;
        }
    },
    ;

    public abstract float getPrice(float input);

    public static void main(String[] args) {
        WITH_TAXES.getFormattedPrice(33f);
    }

}

In this code why this doesn't work: WITH_TAXES.getFormattedPrice(33f); What is the interest of declaring a public method if it can't be called without passing through the common interface? I guess this is why i don't see any syntax to be able to declare an interface just for one of the instances of an Enum.


Edit:

It seems that enum instances are a special kind of anonymous classes. Thus i understand why you can't call that method.

My question is kinda related to: why can't an anonymous class implement an interface (in addition to the interface it may already implement!)

I totally understand why we CANT do that:

Vehicle veh = new Vehicle() {
    public String getName() {
        return "toto";
    }
};
veh.getName();

(getName here is not an override)

Why i don't understand is why we can't do that with anonymous classes:

Runnable veh = new Vehicle() implements Runnable {
    @Override
    public void run() {
        System.out.println("i run!");
    }
};
veh.run();

Or something that would result in the same thing. Think about it: if you do not use anonymous classes you can absolutely extend the Vehicle class and then make that subclass implement any other interfaces you want...

I'm pretty sure that if it was possible we would be able to call WITH_TAXES.getFormattedPrice(33f) in a typesafe way, since WITH_TAXES would not be a real EnumPriceType but it would but a subclass of EnumPriceType, with its own interface, and by calling WITH_TAXES.getFormattedPrice(33f) with a hardcoded WITH_TAXES, you know at compile that which EnumPriceType child you are calling.

So my question is: are there any reasons why this is not possible? Or it just haven't be done yet?

解决方案

Your enum is equivalent to the following normal class (in fact, that's pretty much what the compiler turns it into):

public abstract class EnumPriceType {

    public static final EnumPriceType WITH_TAXES = new EnumPriceType() {
        //getPrice() {...}
        //getFormattedPrice() {...}
    };

    public static final EnumPriceType WITHOUT_TAXES = new EnumPriceType() {
        //getPrice() {...}
    };

    public abstract float getPrice(float input);

    public static void main(String[] args) {
        WITH_TAXES.getFormattedPrice(33f);
    }
}

The getFormattedPrice() method is unavailable on the abstract type, and therefore can't be called from the main method. Consider what would happen if the main method is rewritten to use a local variable:

public static void main(String[] args) {
    EnumPriceType foo = EnumPriceType.WITH_TAXES;
    foo.getFormattedPrice(33f);
}

This doesn't compile because getFormattedPrice() is not available on the base class. Since the WITH_TAXES instance is an anonymous subclass of EnumPriceType, there's no way you can define the local variable to a type where the getFormattedPrice() method is visible.

As a meta observation, this is a key difference between strongly typed languages such as Java and "duck typed" languages such as Ruby. Ruby will happily invoke the getFormattedPrice() method if happens to be there, regardless of what type of object is held in the foo variable.

As another meta observation, it doesn't make much sense for different constants of the same enum to have different sets methods. If you can't put everything you need as abstract (or concrete) methods on the base enum type, you're probably using the wrong tool to solve the problem.

这篇关于单身,枚举和匿名内部班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆