New feature of Java 16: record class

Help me to the Internet cafe 2022-05-14 14:40:09 阅读数:314

newfeaturejavarecordclass

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 .record Introduction of keywords , Mainly to provide a more concise 、 Compact final How classes are defined .

Let's learn more about record Class details . Supporting video tutorial :Java 16 New characteristics : Use record Declaration class

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 :

  1. A separate document states :
public record range(int start, int end){}
  1. Declare... Inside a class :
public class DidispaceTest {
public record range(int start, int end){}
}
  1. 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 :

  1. It's a final class
  2. automatically equalshashCodetoString function
  3. The 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();
版权声明:本文为[Help me to the Internet cafe]所创,转载请带上原文链接,感谢。 https://javamana.com/2022/134/202205141436333199.html