[Java core technology] five - Inheritance (enumerated classes)

Java enumeration type (enum)

  • Enumeration types are inherited Enum class (is an abstract class) of a class , we can add methods and variables to the enum class. Compile and then decompile can see the contents of the corresponding enumeration type of class.
  • Each instance of an enumeration constant corresponds Enum class.
  • example
public enum Day2 {
    MONDAY("星期一"),
    TUESDAY("星期二"),
    WEDNESDAY("星期三"),
    THURSDAY("星期四"),
    FRIDAY("星期五"),
    SATURDAY("星期六"),
    SUNDAY("星期日");//逗号分隔,分号结束

    private String desc;

    /**
     * 私有构造,防止被外部调用
     * @param desc
     */
    private Day2(String desc){
        this.desc=desc;
    }

    public String getDesc(){
        return desc;
    }
}

We write the constructor can only be called a compiler, in fact, MONDAY("星期一")show that the constructor call

     private Day2(String desc){
        this.desc=desc;
    }

And assign "Monday" as an example MONDAY attribute desc.

  • Good reference article, you can read: https: //www.cnblogs.com/alter888/p/9163612.html

Abstract class

  • Abstract classes and abstract methods modified with the abstract keyword.
  • An abstract class is not necessarily abstract methods, abstract methods must be abstract class or interface.
    • If an abstract class without abstract methods, can be defined as an abstract class, do only one purpose, do not let the other classes to create objects of this class, subclass to complete.
  • An abstract class can not be instantiated.
  • Subclass of the abstract class: either an abstract class, all the abstract methods abstract class or rewritten.

Guess you like

Origin www.cnblogs.com/coding-gaga/p/11723232.html