Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How java specifies the behavior of enumerations

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report