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:10Program 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 :

  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();

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 .

Click to collect 2022 newest 10000T Learning materials

copyright notice
author[Program ape DD_],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/131/202205111244327876.html

Random recommended