跟读练习: Records In Java - Full Tutorial - The Best New Java Feature You're Not Using - 通过YouTube学习英语口语

C2
records are a relatively new addition to Java
⏸ 已暂停
207
如果句子过短或过长,请点击 Edit 进行调整。
1
records are a relatively new addition to Java
2
that you can use to avoid a ton of boilerplate code in certain types of classes.
3
In this video we'll talk about exactly what records are,
4
how they can simplify your code,
5
and when and how you can implement them into your Java programs.
6
My name's John and I'm a lead Java software engineer.
7
I also have a full Java course available and a link down in the description so go check it out.
8
To understand why records are so useful it helps to know what programmers had to do before they existed,
9
which is probably what you are still doing if you're not using records yet.
10
Let's say you wanted to make a very simple Java class
11
that all it contained was a bunch of fields to carry data about something.
12
These kinds of classes are super common in the Java world.
13
Like for example if you had an employee class that carried the employee's name and their employee number.
14
Maybe that employee info exists in some database
15
and all this class has to do is hold that employee info and return it to the user.
16
So before records existed, here's everything you had to do.
17
Let's give it two private fields,
18
a string employee name, and an int employee number.
19
Let's also go ahead and make these final so they can't be modified after the employee object is created.
20
So because these two fields are final,
21
we want to be able to set them right when we create an employee object.
22
So we have to add them to a constructor.
23
So maybe our constructor is just public employee class and
24
that takes in the string name and the int employee number as parameters.
25
Then all it does is just set this.name equals name and this.employee number equals employee number.
26
Then let's go ahead and generate some getter methods for these two fields so they can be accessed from outside the class.
27
Next you also want a good two-string implementation
28
that returns all the data in the class in a nice
29
readable string format something like this next you're going to want
30
to override the hash code method with a good implementation as well now we're almost done
31
but not yet you're also going to want a good equals
32
method implementation to check whether one instance of this class is equal to another
33
so now finally after all of
34
that we can come back here to our main method
35
and create a new employee class object we'll just call it employee equals new employee class
36
and we pass in a name and employee number here in the constructor.
37
And there you go!
38
But that was a whole lot of code!
39
That's almost 50 lines of code for a class that does something so simple like hold two pieces of data.
40
But that's how you have to do it with a traditional Java class.
41
Now to compare, let's show how to do the exact same thing but using a record.
42
We just say public record.
43
We'll call it employee record.
44
It has a string name and an int employee number.
45
Open and close the curly brackets and that's it.
46
This single line of code gives us the equivalent of everything we just created in this employee class.
47
So now with just that we can go back here to our main method
48
and create an employee record object in the exact same way that we created the employee class object.
49
So we'll call this employee record equals new employee record
50
and pass in the name of Kramer and Kramer is employee number 54321.
51
So now let's break it down a bit.
52
To create a record first we say public record instead of public class.
53
That's because a record is a certain special type of class,
54
kind of like an enum is also a special type of class.
55
Next we just give it a name.
56
Here we're calling it employee record.
57
Next in parentheses we list out all the components
58
that we want to hold inside our record in a way that looks a lot like the parameters to a method.
59
So this is just all the different data elements that you want to hold in your record.
60
So here we have string name and int employee number.
61
So with just that, Java gives us a whole ton of other stuff for free.
62
First, it generates private fields for all the components that we listed here,
63
just like we did over here in our employee class.
64
These will also be generated as final fields by default,
65
just like we did in our class,
66
so that they can't be changed after an employee record object is created.
67
It also generates public getter methods for each of these components,
68
similar to the getters that we created here.
69
The only difference is that the getters that it automatically generates for a record don't have get in their method name.
70
They just have the name of that field.
71
So just to demonstrate that back over here in our main method,
72
if we wanted to print out this employee class object's name we would just call employee dot get name.
73
But to do the exact same thing with our employee record
74
object we would just have to call employee record dot name without the get
75
but if we go ahead and run our program we can see
76
that the functionality is the same next it will also generate
77
for you a certain type of constructor now you might know
78
that when you create a normal java class what it automatically creates for you behind the scenes even
79
if you don't make any constructor at all is a no args constructor like this that doesn't take any parameters.
80
When you create a record however it works a bit differently.
81
Instead what it creates is a constructor like this one
82
that takes in as parameters all of the different components that you listed up here in the record declaration.
83
Then it sets all of
84
that record's fields with the values in those parameters just like we did over here in our employee class constructor
85
that we made.
86
This type of constructor that is automatically generated when you create a record is known as a canonical constructor.
87
Even though we didn't explicitly create any constructor over here in our record,
88
we were still in our main class able to call a new constructor and pass in our two component values.
89
And that's because that constructor was automatically generated behind the scenes.
90
There's also some really cool things that you can do with constructors only in records
91
that you can't do in any other type of Java class that we'll talk about in just a little bit.
92
Finally, it'll also automatically generate implementations of the toString,
93
equals, and hash code methods.
94
Very similar to the ones that we created over here in our class.
95
So the toString method will give you the name of the record class
96
and then the name and value of all of its components.
97
And we can actually test out
98
that automatically generated toString method just by passing in the actual object to the system.out.println method
99
because when you pass in an object
100
that isn't a string to system out print line it automatically
101
calls the toString method to get the string representation of that object.
102
So if we go ahead and run our code we can see
103
that it prints out the contents of these objects in a very readable way.
104
So we can see that the record automatically created a toString
105
that has the name of the record and then the value of each of its components.
106
And it will generate equals and hash code methods
107
that will consider two objects of that record class equal if all of their components are equal to each other.
108
These three methods can be somewhat tedious to write,
109
and so often programmers will just skip implementing them.
110
And even if they do bother to do it,
111
they can be really easy to mess up,
112
especially the equals and hash code methods.
113
So with records, you don't have to worry about them at all.
114
It will automatically create them for you.
115
You are allowed to override those methods with implementations of your own,
116
but most of the time there's just no reason to do it.
117
One thing to note is that it does not generate any setter methods.
118
One of the main features of a record and one of the main reasons to use a record
119
is that they are immutable by default.
120
That just means that a record object cannot be changed once it's created.
121
Once your employee record object is created with a certain name and employee number,
122
you can no longer change this employee record's name or employee number to anything else.
123
So it really wouldn't make sense to have any setters created anyway.
124
Now there are a few other things that you'll have to know if you're going to be using records.
125
First, like any other class,
126
you can create instance methods if you want.
127
So for example, you might have some method public string name in uppercase.
128
And maybe all that does is just return name dot to uppercase.
129
And so you can call this method on an employee record object just like you would any other type of object.
130
So over here we can call employee record dot name in uppercase.
131
And if we run it,
132
we can see that we get the employee name in uppercase.
133
You can also create your own static methods as well.
134
So like public static void print whatever and that can just print whatever.
135
Of course to call a static method you should call it using the name of the class itself
136
and not a certain instance of that class.
137
So over here we would just call employee record with capital E capital R dot print whatever
138
and of course that will just print whatever.
139
You can also create static fields like you could in any other class.
140
For example, you could have a public static final string default employee name equals George.
141
However, you cannot define your own non-static instance fields.
142
So something like private string something,
143
you are not allowed to do this.
144
You'll get an error that says instance field is not allowed in record.
145
Any instance fields like this that you want have to be defined up here in your list of components.
146
Another thing is that records cannot extend any other class.
147
So up here if you try to do something like extends employee class you would get an error
148
that says no extends clause allowed for record.
149
All Java records implicitly extend the record class.
150
Kind of like all enums implicitly extend the enum class.
151
So because Java doesn't allow multiple inheritance your record can't be a child of any other class.
152
Records are also implicitly final classes,
153
which means that they can't be extended by any other class either.
154
You are allowed to put final in your record declaration,
155
but it's redundant to do so because it's already automatically going to be final.
156
However, you are allowed to implement interfaces in records.
157
So to do
158
that right up here after your record declaration like any other class you can just put in implements whatever interface you want.
159
So if you want you can implement the runnable interface.
160
It would probably be kind of weird for a record to be runnable
161
but if you want to do it you can do it.
162
You just have to implement this run method like you would for any other runnable.
163
I mentioned before that we'd talk a little bit more about constructors in records.
164
Remember that for records, Java will automatically create this canonical constructor
165
that takes in all of the components and just sets all of those fields based on those components that are passed in.
166
So the idea is once you use that canonical constructor to create an object of that record class,
167
all of its fields will be set and can never be changed.
168
The record only exists to carry that collection of data through your program.
169
However, you can also define your own constructors.
170
First, even though it gives you that canonical constructor for free,
171
you can override it with your own implementation if you want.
172
So if I want, I can create public employee record,
173
which takes in a string name and an int employee number.
174
So to override that default constructor,
175
it has to take in the exact same parameters in the exact same order.
176
One reason you might want to override the default constructor is
177
just to do some sort of validation on the values being set.
178
Like maybe it doesn't make sense for an employee number to be negative.
179
So you might do something like if the employee number is less than zero,
180
throw a new illegal argument exception.
181
Employee number cannot be negative.
182
After that though, you still have to set your records fields using the values passed in like this.
183
So this.name equals name and this.employeeNumber equals employeeNumber.
184
However, with records there's a really cool little shortcut you can use to override your canonical constructor
185
if you want and that's called a compact constructor.
186
This is something that is unique only to records.
187
All a compact constructor is is this exact same constructor except with the parameters removed and these assignments removed.
188
It will automatically add these same parameters in the same order,
189
and at the end of this constructor it will automatically set all of this record's fields with those parameters.
190
You can also have additional regular constructors that take in different sets of parameters if you want.
191
And for those all the normal constructor rules apply.
192
You can learn a ton more about Java constructors in this video here.
193
I think the ability to create this kind of compact constructor is really cool
194
and it really serves to illustrate exactly why records exist.
195
Records are just a super simple
196
and fast way to create a certain type of class
197
that we have to use all the time in the Java world and that's a class that just holds data.
198
Now remember we didn't even need any of this extra stuff we added
199
so just this is all
200
that we needed to create a full featured class whose purpose is to just hold and carry this data.
201
And
202
if you ask me that's a whole lot better than having
203
to write out all of this to do exactly the same thing.
204
If you enjoyed this video or learned something please let me know by leaving a like.
205
Be sure to subscribe for more java tutorials
206
and for my full java course just check out the link down in the description.
207
Thank you so much for watching I'll see you next time.

下载应用

AI 为你说出的每个句子打分

TRENDING

热门

背景与概况

在本视频中,讲述者John是一位资深的Java软件工程师,他分享了Java中Records这一新特性。Records能够显著减少编写冗长代码的需求,使得处理数据类变得更加简单高效。对于学习者来说,这段讲话不仅涉及了编程语言的技术细节,同时也提供了提升英语口语能力的机会。通过观看此类技术视频,您可以在掌握复杂主题的同时,不知不觉提高英语水平,真是一举两得。

日常交流中的五个常用短语

  • “Let’s say”:用于引入假设情况,增加对话的互动性。
  • “Here’s everything you had to do”:解释某个过程时很实用,帮助听者理解步骤。
  • “We want to be able to”:表达愿景或目标,适合在商务或技术讨论中使用。
  • “That’s almost 50 lines of code”:强调某个内容的复杂度,适用于说明一项工作的繁重程度。
  • “And that’s it”:在讲解完某个过程或步骤后,表示结束,非常口语化。

逐步影子跟读指导

在观看有关Java Records的视频时,您可以按照以下步骤进行英语口语练习,以提升您的英语表达能力:

  1. 观看视频并理解内容:首次观看视频时,关注讲解者如何使用语言表达技术概念。
  2. 停顿与重复:对于每个难懂的句子,您可以暂停并尝试跟读,模仿讲述者的语调和节奏。这时候可以进行英语影子跟读
  3. 分析短语用法:在反复练习中,注意记录视频中出现的短语(如“Let’s say”),并理解其在各种语境下的使用方法。
  4. 进行角色扮演:与学习伙伴一起讨论视频中的内容,采用视频中的语言进行对话,以此提高您的英语口语练习能力。
  5. 定期复习:定期回看此视频,巩固记忆,提升流利度和自信心。

通过以上步骤,您不仅能掌握Java中的Records概念,还能在使您具备实际运用英语的能力上大大受益。无论是准备雅思口语练习,还是提升日常交流能力,这都是一个有效的学习方式。

什么是跟读法?

跟读法 (Shadowing) 是一种有科学依据的语言学习技巧,最初开发用于专业口译员的培训,并由多语言者Alexander Arguelles博士普及。这个方法简单而强大:您在听英语母语原声的同时立即大声重复——就像是一个延迟1-2秒紧跟说话者的影子。与被动听力或语法练习不同,跟读法强迫您的大脑和口腔肌肉同时处理并模仿真实的讲话模式。研究表明它能显着提高发音准确性,语调,节奏,连读,听力理解和口语流利度——使其成为雅思口语备考和真实英语交流最有效的方法之一。

请我们喝杯咖啡