current position:Home>In addition to class in Java, do you know the keyword that defines the class?
In addition to class in Java, do you know the keyword that defines the class?
2022-05-15 05:18:10【Program ape DD_】
Hello everyone , I am a DD, It's the first time I'm closed at home 52 days !
In addition to depression , Keep sharing dry goods ! Originality is not easy. , Help me praise at the end of the article 、 Look again 、 Forwarding support oh ~
In the past, we used to define classes with class
key word , But from Java 16 Start , We will add one more keyword record
, It can also be used to define classes .
This record
Introduction of keywords , Mainly to provide a more concise 、 Compact final
How classes are defined . Let's learn more about record Class details .
Statement record class
Statement record The basic syntax of the class :
record range(int start, int end){}
We know class The life class can be in a separate file , You can also declare... In other classes . that record Class is the same , It has these kinds of declarations :
A separate document states :
public record range(int start, int end){}
Declare... Inside a class :
public class DidispaceTest {
public record range(int start, int end){}
}
Intra function declaration :
public class DidispaceTest {
public void test() {
public record range(int start, int end){}
}
}
record Class explanation
After knowing how to declare , You'll want to use record
There are just a few elements declared , What capabilities will it have ?
because record
Keyword declaration class is mainly used to simplify the declaration of some classes , So its essence is a special kind of class, Or a template class.
record
Declared class , With these characteristics :
It's a
final
classautomatically
equals
、hashCode
、toString
functionThe member variables are
public
attribute
therefore , For what I wrote before range class , It is equivalent to such a class :
public final class range{
final int start;
final int end;
public range(int start, int end) {
this.start = start;
this.end = end;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
range range = (range) o;
return start == range.start && end == range.end;
}
@Override
public int hashCode() {
return Objects.hash(start, end);
}
@Override
public String toString() {
return "range{" +
"start=" + x +
", end=" + y +
'}';
}
public int start(){
return start;
}
public int end(){
return end;
}
}
Because some functions are hidden , stay range When defining , We can't see , So we can try to write such a test code to verify :
@Test
public void test() {
range r = new range(100, 200);
log.info(r.toString());
range r2 = new range(100, 200);
log.info(String.valueOf(r.equals(r2)));
}
Define member functions
because record
The essence of declaration is also class , Then it is certainly possible to define member functions .
such as , We can do this in record
Class to define member functions :
record range(int start, int end){
int distance(){
return end - start;
}
}
then , You can call it like this :
range r = new range(100, 200);
r.distance();
If you want to know more about Java The content of new features , I'm serializing a free Java New features column :https://www.didispace.com/java-features/, See... In the form of documents Java New characteristics , Reading and learning experience is better !
We have created a high-quality technical exchange group , With good people , I will be excellent myself , hurriedly Click Add group , Enjoy growing up together . in addition , If you want to change jobs recently , Years ago, I spent 2 A wave of large factory classics were collected in a week , Those who are ready to change jobs after the festival can Click here to get !
Recommended reading
··································
Hello , I'm a procedural ape DD,10 Old driver developed in 、 Alibaba cloud MVP、 Tencent cloud TVP、 I have published books and started a business 、 State-owned enterprises 4 In the Internet 6 year . From ordinary developers to architects 、 Then to the partner . Come all the way , My deepest feeling is that I must keep learning and pay attention to the frontier . As long as you can hold on , Think more 、 Don't complain 、 Do it frequently , It's easy to overtake on a curve ! therefore , Don't ask me what I'm doing now, whether it's in time . If you are optimistic about one thing , It must be persistence to see hope , Instead of sticking to it when you see hope . believe me , Just stick to it , You must be better than now ! If you don't have any direction , You can pay attention to me first , Some cutting-edge information is often shared here , Help you accumulate the capital to overtake on the curve .
copyright notice
author[Program ape DD_],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/131/202205111244327876.html
The sidebar is recommended
- Online FAQ positioning FAQ what is the cause of the high load problem?
- What is the function of getstatic, a common tool for online FAQs?
- Android 11 new soft keyboard occlusion / animation solution
- Common tools for online FAQs include?
- How does SAP commerce cloud configure new applications for storefront
- In the CMS GC process, what is the reason why the business thread puts objects into the old generation (the characteristics of concurrent collection)?
- How good and accurate is the recommendation?
- Online FAQ positioning FAQs what are the causes of continuous GC problems?
- Does the data reflect the real viewing experience?
- What are the reasons for fullgc (throw oom if FGC recovery is invalid)?
guess what you like
Algorithm improvement - basic algorithm (turtle speed multiplication)
[C + +] sword finger offer 10 - I. Fibonacci sequence
Online FAQ positioning FAQ nosuchmethodexception what is the cause of the problem?
IOS enables native im development
What is the common function of SM?
"Automated testing" a new generation of Web front-end automated testing framework - playwright, get started quickly!
Online FAQ positioning FAQ what is the cause of the high load problem?
What is the function of watch, a common tool for online FAQs?
Timeliness in recommender systems, Zhang Fuguo et al. ESWA 2017
Alibaba's open source Java diagnostic tool uses what methods to diagnose.
Random recommended
- What is the function of dashboard, a common tool for online FAQs?
- What is the role of JAD, a common tool for online FAQs?
- Online FAQ positioning FAQ what are the causes of high CPU utilization?
- 07 - explore the underlying principles of IOS | several OC objects [instance object, class object, metaclass], ISA pointer of object, superclass, method call of object and the underlying essence of class
- Extreme fox gitlab settled in Alibaba cloud computing nest to jointly improve the development experience on the cloud
- How does artificial intelligence help natural science
- Elementui upload file
- Modern CSS solution: CSS mathematical functions
- Create a general efficiency improvement solution for front desk + middle desk based on vue3 (network disk link)
- Brush 100 front-end high-quality interview real questions in 2 weeks, and the source code is complete
- Vue has reduced its workload by half since using components
- I built a front-end mock tool
- About uboot -- Ping virtual machine Ubuntu operation
- Video transcoder editready for Mac
- [taro] taro gets the detailed attributes of the element (solved)
- Picture and text difference comparison tool: kaleidoscope for Mac
- Background of spatiotemporal artificial intelligence
- The top 10 of oceanbase database competition was born, and the integration of industry and education accelerated the training of database talents
- China brand Day | Youxuan software: strengthen its own brand and fight for China's database industry
- New feature release of gaussdb (for redis): enhanced prefix scanning and multi rent isolation
- CICC purchases the original maintenance service of gbase database in 2022
- Java implementation sequence table
- Simple implementation of linked list in Java
- C + + parameterless constructor (difference between stack and heap)
- Vue NPM startup error - solution
- With the introduction of Alibaba cloud database into Shandong medical insurance information platform, the settlement response speed increased by nearly 10 times
- Yixinhuachen was selected into the atlas of relevant manufacturers in the primary market of China's big data industry
- 2021-06-05 Java Foundation (day four): two ways of locking
- Android bangs screen and water drop screen are the best overall adaptation scheme
- Don't take detours in Android learning
- Android realizes Gaode map track playback
- 2021 preparing for the 1000 Java interview questions that gold, silver and four must brush
- The database function queries the MySQL database at the correct time
- Linux changes the SSH connection mode, changes the public key to the user name and password, and logs in
- Websocket + springboot message active push
- Java common classes and methods
- Go connect to the database (simple query learning)
- HTTP - understand HTTP protocol (I)
- Spring boot quickly switches the configuration of development environment and production environment (application. YML)
- Java gets the date of the previous day