课程: Java Essential Training: Syntax and Structure

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Records

Records

- [Instructor] A Record is a special type of Java class. That is great for simple objects that will only need fields and methods to access those fields. Here is a record that serves as a model for an account. Notice that instead of the word class, we use the word record. Also, the fields are defined in the class declaration within a set of parentheses followed by a set of curly braces. That's it. This simple declaration creates a record with these fields. You don't need to create any getter or setter methods. All of that is done for you under the covers. However, if you want to add additional methods you can do so within the curly braces. Records can be instantiated just like classes. In this example, account is the name of the record and we use the new keyword, then called the constructor, passing in all of the values. Records are immutable. This means at the fields of a record are final and their value cannot be changed…

内容