enum枚举认识

1 篇文章 0 订阅

enum特点:

  • 枚举不能继承其他类,但是所有的枚举都继承超类java.lang.Enum,可以实现接口
  • 枚举不能是超类,因为它是final修饰的
  • 枚举的构造函数修饰符只能是private和default,因此不能通过new出对象实例
  • 枚举常量都是 public static final类型的
  • 调用枚举实例的时候会调用所有实例的构造函数,并且只调用一次

调用枚举实例的时候会调用所有实例的构造函数

public enum Fruit {
    APPLE,BANANA;

    Fruit() {
        System.out.println("invoke constructor...");
    }
}

//test
@Test
public void enumConstructorTest(){
    System.out.println(Fruit.APPLE);
}

//console
invoke constructor...
invoke constructor...
APPLE

调用枚举实例的时候会调用所有实例的构造函数,并且只调用一次

//test
@Test
public void enumConstructorTest(){
   System.out.println(Fruit.APPLE);
   System.out.println(Fruit.BANANA);
}

//console
invoke constructor...
invoke constructor...
APPLE
BANANA

调用枚举实例的时候会调用所有实例的构造函数

public enum Fruit {
    APPLE,BANANA, ORANGE("orange");

    Fruit() {
        System.out.println("invoke constructor...");
    }

    Fruit(String name){
        System.out.println("invoke constructor with args...");
    }
}

//Test
@Test
public void enumConstructorTest(){
    System.out.println(Fruit.APPLE);
}


//console
invoke constructor...
invoke constructor...
invoke constructor with args...
APPLE

如果enum中有抽象方法,则在每个实例中都必须实现这个方法

public enum Fruit {
    APPLE{
        public void display() {
            System.out.println(this.name()+ "display()...");
        }
    },BANANA{
        public void display() {
            System.out.println(this.name()+ "display()...");
        }
    }, ORANGE("orange"){
        public void display() {
            System.out.println(this.name()+ "display()...");
        }
    };

    Fruit() {
        System.out.println("invoke constructor...");
    }

    Fruit(String name){
        System.out.println("invoke constructor with args...");
    }

    public abstract void display();
}

//Test
@Test
public void enumConstructorTest1(){
   Fruit.APPLE.display();
}

//console
invoke constructor...
invoke constructor...
invoke constructor with args...
APPLEdisplay()...

如果枚举类实现了接口,则须在enum类中实现这个方法或者是在每个实例中都实现这个方法

//interface
public interface Color {
    void color();
}

public enum Fruit implements Color{
    APPLE{

        public void color() {
            System.out.println(this.name()+"color()...");
        }
        public void display() {
            System.out.println(this.name()+ "display()...");
        }
    },BANANA{

        public void color() {
            System.out.println(this.name()+"color()...");
        }

        public void display() {
            System.out.println(this.name()+ "display()...");
        }
    }, ORANGE("orange"){
        public void color() {
            System.out.println(this.name()+"color()...");
        }

        public void display() {
            System.out.println(this.name()+ "display()...");
        }
    };

    Fruit() {
        System.out.println("invoke constructor...");
    }

    Fruit(String name){
        System.out.println("invoke constructor with args...");
    }

    public abstract void display();

//可以在enum类中实现接口的方法
//    public void color() {
//        System.out.println("color");
//    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值