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

Why don't enumerations be used in the return values of Java interface

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

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "Java interface return values do not use enumerations". In daily operation, I believe that many people have doubts about why enumerations are not used in Java interface return values. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "why Java interface return values do not use enumerations". Next, please follow the editor to study!

For example, Starbucks provides a 0.0.1 version of the second-party library, which defines a Starbucks class that contains enumerated types of SizeEnum, which are medium cup, large cup and extra large cup.

Public class Starbucks implements Serializable {

Private Long id

Private String name

Private Integer capacity

Private SizeEnum sizeEum

}

Public enum SizeEnum {

TALL (1)

GRANDE (2)

VENTI (3)

}

Define a service class and implement the method of getting Starbucks according to id:

Public class StarbucksImpl implements StarbucksService {

Public Starbucks getStarbucksById (Long id) {

Starbucks starbucks = new Starbucks ()

Starbucks.setId (1L)

Starbucks.setName ("Latte")

Starbucks.setCapacity (360)

Starbucks.setSizeEnum (SizeEnum.TALL)

Return starbucks

}

}

Then, Starbucks stores introduced the 0.0.1 version of the jar package, and sold it well:

Public class StarbucksDemo {

@ Resource

Private StarbucksService starbucksService

Public void getStarbucks () {

Starbucks starbucks = starbucksService.getStarbucksById (1L)

System.out.println (starbucks)

}

}

One day, Lao Luo asked for the medium latte, but the waiter said it was a large cup. After some argument, Mr. Luo was very angry.

So Starbucks upgraded to the 0.0.2 version of the second-party library, and added small cups to the enumeration class SizeEnum. The upgraded enumeration classes are as follows:

Public enum SizeEnum {

TALL (1)

GRANDE (2)

VENTI (3)

SHORT (4)

}

At the same time, the interface method of the service class has also been modified accordingly:

Public class StarbucksImpl implements StarbucksService {

Public Starbucks getStarbucksById (Long id) {

Starbucks starbucks = new Starbucks ()

Starbucks.setId (1L)

Starbucks.setName ("Latte")

Starbucks.setCapacity (240)

Starbucks.setSizeEnum (SizeEnum.SHORT)

Return starbucks

}

}

Due to the large number of Starbucks stores, some of them do not know the newly added requirements, so SHORT appears in the returned results, but there is no small cup in the 0.0.1 version of the second-party library, so there is a problem, that is, serialization failure.

Through this example, I believe you have a certain understanding of enumerated types as a return result. The following quotes the answer given by Gu Jie in Zhihu:

Due to upgrade, the enumerated classes of both sides are different, and an exception occurs when the interface is parsed and the class is deserialized.

Any element that appears in Java has thinking and logic behind it from Gosling's point of view (although not absolutely perfect, the top-level abstraction of Java is already genius), such as interfaces, abstract classes, annotations, and enumerations mentioned in this article. Enumerations are good, type-safe, clear and straightforward, and can be judged using the equal sign or in switch. Its disadvantage is also obvious, that is, do not expand. But why is there a distinction between the return value and the parameter? if they are not compatible, then there is a problem with both, how to allow the parameter to be enumerated. At that time, enumerations were almost useless if the parameters were not available. Parameter output, after all, is determined locally, you have local, sent to the past, forward compatibility will not be a problem. However, if the interface returns, it will be disgusting, because the enumerated value parsed may not be locally available, and a serialization exception will be thrown.

For example: your local enumeration class, there is a weather Enum:SUNNY, RAINY, CLOUDY, if the weather based on the method of calculating mood: guess (WeatcherEnum xx), you can pass in all three values. Return value: Weather guess (parameter), then the other side will return a SNOWY after operation. It is silly that there is no such value in the local enumeration.

At this point, the study on why enumerations are not used in the return values of the Java interface is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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