Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,420 --> 00:00:03,960
The copy constructor copies every value from one object to another.
2
00:00:06,120 --> 00:00:11,730
In the previous lesson, you decided to sell a second Nissan, so you made a new object and passed in
3
00:00:11,730 --> 00:00:12,780
the same values.
4
00:00:14,240 --> 00:00:17,750
Now, I have to admit, passing every valley twice is a bit annoying.
5
00:00:24,000 --> 00:00:26,610
In this lesson, you're going to get a copy constructor and call it.
6
00:00:30,120 --> 00:00:33,600
Constructor overload means having more than one constructor.
7
00:00:38,570 --> 00:00:44,060
So far, you created one constructor when you pass in for argument's, Java knows to call the constructor
8
00:00:44,060 --> 00:00:45,230
with four parameters.
9
00:00:46,410 --> 00:00:52,260
Now, a class can have many constructors, as many as you want, each constructor shares the same name
10
00:00:52,260 --> 00:00:55,400
as the class that it's in, but it takes different parameters.
11
00:00:55,920 --> 00:01:02,370
You can have a constructor with four parameters, three parameters, two parameters, one parameter
12
00:01:02,640 --> 00:01:03,600
or even zero.
13
00:01:04,810 --> 00:01:09,580
And the fact that each instructor has the same name, how does Javin know which one I intend to call?
14
00:01:10,730 --> 00:01:15,620
Java looks at the arguments you pass, then it's going to know which constructor you want it to call.
15
00:01:16,430 --> 00:01:20,720
For example, you can create a car object and pass in two arguments to the constructor.
16
00:01:21,380 --> 00:01:24,440
Java's going to know to run the constructor with two parameters.
17
00:01:33,430 --> 00:01:34,420
Why does this matter?
18
00:01:34,660 --> 00:01:36,520
Well, we're about to add a new constructor.
19
00:01:41,240 --> 00:01:47,000
And that constructor is the copy constructor, the copy constructor copies every value from one object
20
00:01:47,000 --> 00:01:47,630
to another.
21
00:01:50,970 --> 00:01:56,220
The copy constructor takes one parameter, the object you want to copy values from the source.
22
00:02:02,710 --> 00:02:09,310
As you're creating a new car object, passing the object, you want to copy values from Jarvis's the
23
00:02:09,310 --> 00:02:15,970
argument and it knows to run the copy constructor, the parameter source stores, a reference that points
24
00:02:15,970 --> 00:02:17,170
to the Nissan object.
25
00:02:22,020 --> 00:02:26,310
Then it copies every value from the source object into the current object.
26
00:02:34,820 --> 00:02:37,160
So inside the car class, I'm going out of constructor.
27
00:02:39,730 --> 00:02:40,780
Public car.
28
00:02:43,920 --> 00:02:50,520
And this constructor, for it to run, it expects to receive another car object, car source.
29
00:02:54,370 --> 00:02:59,800
Because of this object is going to be the source from which we update the fields of our new object.
30
00:03:00,640 --> 00:03:05,950
All right, so we're going to set every field in the current object equal to a value from the source
31
00:03:05,950 --> 00:03:06,500
object.
32
00:03:07,030 --> 00:03:11,560
We're going to set the make of the object we just created equal to the make from the source object that
33
00:03:11,560 --> 00:03:12,340
gets passed in.
34
00:03:13,180 --> 00:03:14,860
We'll do the same thing for price.
35
00:03:25,800 --> 00:03:27,360
Year and color.
36
00:03:43,620 --> 00:03:48,810
And now we can call the copy constructor, certainly Nissan, to equal to a new object in UKAR.
37
00:03:49,920 --> 00:03:54,600
And passing the original Nissan is the source object that we want to copy values from.
38
00:03:56,660 --> 00:03:57,470
Reinier code.
39
00:04:15,080 --> 00:04:20,240
And it works every value from the source object gets copied over into the new object.
40
00:04:23,290 --> 00:04:29,620
You set Nisan to equal to a new object and passed the Nissan as an argument, Javert sees the argument
41
00:04:29,620 --> 00:04:31,630
and knows to run the copy constructor.
42
00:04:35,850 --> 00:04:42,840
The parameters source stores a reference that points to the Nissan object, this refers to the new object
43
00:04:42,840 --> 00:04:48,240
we just created, the current object, the one that's calling the constructor and the copy constructor,
44
00:04:48,240 --> 00:04:52,020
copies every field from the source object into the current object.
45
00:04:53,320 --> 00:04:57,430
And in the end, Nissan, too, is equal to a copy of the Nissan object.
46
00:05:04,630 --> 00:05:10,480
This is a way better solution to the reference trap in the old solution, we were using the first constructor
47
00:05:10,480 --> 00:05:11,620
to create copies.
48
00:05:12,220 --> 00:05:14,620
We were passing in four identical arguments.
49
00:05:14,620 --> 00:05:17,440
So Jabarin in the constructor with four parameters.
50
00:05:21,390 --> 00:05:23,590
This method is a bit annoying, in my opinion.
51
00:05:23,940 --> 00:05:27,690
I like the copy constructor because it copies every value from a source.
52
00:05:32,160 --> 00:05:36,930
This is actually the accepted way of copying objects, so we're going to update our table.
53
00:05:46,170 --> 00:05:51,510
And this lesson, you added a copy constructor and used it, the copy constructor takes one parameter,
54
00:05:51,540 --> 00:05:53,190
the object that you want to copy.
55
00:05:54,060 --> 00:06:00,810
As you're creating a new car object, passing the object that you want to copy values from Java, the
56
00:06:00,810 --> 00:06:03,180
argument, and it knows to run the copy constructor.
57
00:06:06,950 --> 00:06:14,030
The parameter source is a reference that points to the Nissan object, and this refers to the new object
58
00:06:14,030 --> 00:06:19,640
you just created, the current object, the one that's calling the constructor and the copy constructor,
59
00:06:19,640 --> 00:06:23,690
copies every field value from the source object into the current object.
60
00:06:29,390 --> 00:06:33,260
This is the best way and the accepted way to copy objects.
6342
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.