Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How java specifies the behavior of enumerations

Shulou Source: shulou.com Published: 2022-05-31 10:52:58 09月11日 Update

Description

1. Enumerations can not only be used to represent constants, but also sometimes write some simple computational logic in enumerations.

2. You can use abstract methods to define the behavior required by each enumeration.

Example

Package com.tea.modules.java8.enums; import lombok.Getter; / * com.tea.modules.java8.enums

* operator enumeration * * @ author jaymin * @ since 2021-6-10 * / @ Getterpublic enum OperationEnum {/ * plus * / PLUS ("+") {@ Override public double apply (double x, double y) {return x + y }}, / * * minus * / MINUS ("-") {@ Override public double apply (double x, double y) {return x-y }}, / * multiply * / TIMES ("*") {@ Override public double apply (double x, double y) {return x * y }}, / * except * / DIVIDE ("/") {@ Override public double apply (double x, double y) {return x / y;}}; / * * operator * / private final String symbol; OperationEnum (String symbol) {this.symbol = symbol } public abstract double apply (double x, double y);} above is all the content of the article "how java specifies the behavior of enumerations". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Knowledge articles behavior content operators logic operations differences great most examples that is constants methods more knowledge points industries information channels faceted Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MariaDB macOS MySQL Redmi