Placeholder Image

字幕表 動画を再生する

  • Hey, guys, this is part two off my introduction to classes and objects.

  • And in this video, I'm gonna talk about how multiple classes and objects can interact with each other.

  • So let's get started now.

  • Just a quick recap of the last video we created this class called Robert with the three attributes name, color and wait.

  • Name was a string color was a string us well on weight was an intruder and it had only one function called into yourself.

  • And when this function is run, you'll just print out.

  • My name is name whatever the name is.

  • And then out of this class, we created two objects.

  • The 1st 1 was this one a robot object with the name Tom Color Red and wait 30 to show that it's £30.

  • And the second object was a Robert object with the name Jerry Color Blue and wait 40 and we put the first object in this variable called our one.

  • And then we put the second object, and this viable called are too.

  • Now, let's say, on top of that, we have another class here.

  • This class is gonna be called person to represent any person you want to represent, and it's gonna have three attributes name, which is going to be a string personality, which is also going to be a string on is sitting, which is going to be a bullion, which means this is going to be either true or false on.

  • If this is true, that will mean that this particular person is sitting on.

  • If this value is false, that will mean that this person is not sitting.

  • Using this class will be able to create a couple of objects here as well.

  • On the first object here might look like this.

  • This is going to be a person object, of course, with the name Alice.

  • Personality Aggressive on is sitting false.

  • So this person is not sitting.

  • She's probably standing on.

  • The second object here might look like this.

  • This is a person object with the name Vicky Personally.

  • Personality talked if and is sitting truth.

  • Let's put the first object here.

  • The person named Alice to this variable called P one on.

  • Then let's put the second object here to pee, too, to show that this is personal one on this.

  • This person, too.

  • Let's say this person class has a few methods or a few functions as well.

  • Sit down and stand up.

  • All they're gonna do is gonna be pretty simple.

  • When you run, Sit down this function.

  • Let's say this particular object, p one by saying P one dot sit down.

  • You'll just turn the is sitting value to truth because that person will be sitting.

  • And then if the ISS sitting value is already true, When Iran sit down on that object, you'll just stay at truth on the same thing with stand up, it's just gonna do the opposite.

  • So if we run, stand up on P two by writing Peter that stand up, it's gonna turn the ISS sitting value to false.

  • And then if it's already false, it's just not gonna do anything now having these two classes and these two sets of objects separately, it's fine.

  • But is there any way to somehow make them interact with each other and somehow show the relationships between these two sets of objects?

  • One situation when you might want to do this is, for example, when you want to say that a person a particular person owns a particular Robert.

  • So, for example, this person named Becky might own this rubble.

  • It's named Tom and this person, How is my tone, this other Robert Jerry?

  • So how can we express that?

  • One way to show that is having an extra attribute to the person class.

  • And that attribute might be called something like Robert Owed.

  • This is going to be the particular Roberts that's owned by this person, and this is going to be a Robert object.

  • So, for example, if you want to show that Alleys owns Jerry, which is in this variable called are too you can just set the Robert's own attributes off this person Object two are, too.

  • And this is gonna show that P one owns are, too.

  • And then you can do the same thing for P to Becky.

  • If you want to show that this person owns this Robert, which is in the are one variable, you can just set the rubble own attributes off this person to our one.

  • And this is one very common way off expressing the relationships between multiple classes and objects.

  • Okay, let's now see how we can actually implement that using code.

  • I'm gonna use Java as an example here, but other languages like Python are gonna be usually pretty similar.

  • Let's just quickly recap what we did in the last video.

  • We define a class called Roberts by writing class.

  • Robert Curley brackets on this class had three attributes or three instance variables.

  • String name, string, color on inter your weight.

  • We also define a constructor for creating a new object out of this class.

  • On that one took three arguments.

  • String and shrink See into W.

  • Andi created new object on set that objects name this up name to end it and that objects color that new objects color to see on that weight that objects wait to W.

  • And this class had a one with a two, of course into yourself and that one.

  • Print it.

  • My name is whatever this objects name ISS.

  • And then we used the constructor that we define ourselves to create a couple of objects.

  • The first object had the name Tom Color red on Wait 30 on zone, and then we were able to, of course, use dysfunction.

  • Introduced self by writing are one that injured yourself on our two that inches yourself and just a quick note.

  • If you want to implement this yourself, you'll be able to put both of these blocks of code in a single file and then for me.

  • I'm gonna call it test Robert on person Java.

  • You be able to find a link to this file in the description below as well.

  • OK, moving forward to implement what I explained earlier.

  • Each person owning a particular Robert Let's create a person class by writing off course class person curly brackets.

  • And then this person class will have these attributes string, name, string, personality and bullion is sitting on.

  • Of course, bullion just means that is sitting will be either true or false.

  • And then to show that each person owns a particular Robert will need to add another attribute.

  • Of course called Robert Owned on this Robert owned the value of this attributes will be a Robert object.

  • So that type of this attributes will need to be Robert And then, just like we did with the rubber class, we can create a constructor here as well.

  • This constructor is gonna be called a person because we need to call a constructor the same name as the class.

  • So this is person which takes three arguments string and drink tea and bullion I and then once we create this object by using this constructor, this that name that new objects name will be and the given string.

  • And then this new objects personality will BP, which is another given argument and then that objects it's sitting attributes is sitting.

  • Incest variable will be the given I and let's just quickly define the other methods we had in this class as well.

  • One of them was called Sit down.

  • This is just going to simply set this objects is sitting value too true and then the opposite of sit down, which is stand up that one sets this objects is sitting instance viable or a tribute to be false.

  • And note here that if this that is sitting is already false, when you run stunned up, this is not gonna do anything because it's basically resetting this value to be false again.

  • Okay, at this point, you can create a person object by saying new person Ali's aggressive on false.

  • So this is going to create a new person object with the name Alice.

  • Personality aggressive on is sitting false, and we can assign that to a viable, called P one with the type person and lets the same thing for a second person object.

  • This person's name will be Becky.

  • Personality will be talked if on she will be sitting, so is sitting will be true.

  • And then we're gonna put that in p two.

  • After that, if you want to show that the first person here Alleys owns the second but here named Jerry, it's gonna be pretty simple.

  • You just need to write p one dot robert own ecos are, too, and this sets the attribute.

  • The Robert own attributes off P 12 are, too.

  • And then we can do the same thing for P two as well.

  • If you want to show that the second person Viki owns the first rub it, Tom, you can just write p to that Robert Own Eco's are one, and that way P two's road own attributes will be our one.

  • And once you have that in place, you'll be able to do stuff like this as well.

  • Here I just wrote P one dot robert owned dot introduced self.

  • So basically P one that Robert own will give us are, too, because that's the rub but P one owns.

  • And so this line is the same as saying our to the injured yourself.

  • So when you execute this line, this is gonna print out.

  • My name is Jerry because that's the name off or two.

  • And then we can do the same thing with P to hear the second person.

  • So once we write P to the bubble owned dot inches yourself, Peter, that Robert Own will give us our one.

  • So this is the same as saying are one the introduced self.

  • And of course, this line, once executed, will print out.

  • My name is Tom, because that's the name off our one.

  • So that's how you can express the relationships between Roberts on person objects.

  • But can we do something similar with in the same class?

  • So, for example, what if you had these five roberts on each of them can be expressed as a rabbit object on what if you wanted to show that, for example, these two robots are friends with each other and these two robots are friends with each other and sewn, or it could be something like this.

  • Robert is looking at this other Robert and this robot is looking at this room, but on its own.

  • How can we show something like this?

  • Well, you'll actually be pretty similar to what we saw earlier.

  • So let's take a look at the Robert class here.

  • So far, it's the same.

  • It's exactly the same as what we had earlier.

  • We have name, color and wait as attributes on the Robert Constructor and then the introduced cell function.

  • All you need to do on top of this is you just need to add on extra attributes called Let's say looking at.

  • So this is going to be the Robert this particular robot is currently looking at on.

  • This is going to be a Robert object on, So the type here is rob it.

  • So if you want to say, for example, that the first Robert Tom is looking at the second Robert Jerry, you just need to set the looking at attributes off our 12 are, too.

  • You can do that just like that by saying our one dot looking at Ecos are, too.

  • And then if you want to show also that are too, is also looking at our one, you can just write this are too don't looking at eCos are one, and that's it's the looking at attribute off our 22 or one so that our two is now looking at our one as well.

  • So this was a little bit suey example.

  • You know, a bunch of rubble.

  • It's just looking at each other, but you'll be able to use exactly the same idea to implement more realistic things, too.

  • So, for example, if you're trying to implement a Twitter like system or an instagram like system, you want to be able to say this.

  • Robert is following this other Roberts, or this person is following this other person, and then this person is falling this person.

  • And so to show something like that, you can just change the name of this, looking at a tribute from looking at to falling and then pretty much do exactly the same thing.

  • Asked what we did earlier, Okay, so that wraps up my introduction to classes and objects.

  • If there's any related topic that you want me to cover in the future, though, just let me know in the comment below and special thanks to brilliant karaoke for a sponsor in this video.

  • So in my next video in the Siri's.

  • I'm planning to cover the topic off link lists, but actually, once you understood the concepts you learn in this video, you'll be able to start learning.

  • You know, more advanced data structures like grafts on trees.

  • So if you want to jump ahead and start learning these concepts on your own, you might want to take out brilliant in their computer science albums course, they have a section about graphs, and I think this would be perfect for someone who's just starting to learn data structures and albums like a setting.

  • My previous videos Solving Poems is a really good way to solidify the computer science knowledge you've gained passively by watching videos or reading something.

  • I think brilliant can be a good compliment to this course because it gives you a good way to practice what you've learned through solving poems.

  • So if you want to compliment your passive learning with more active learning like solving problems, just go to brilliant O'Rourke slash csto.

  • This will let them know that you came from here on.

  • You get 20% off their annual subscription sue that linked to okay, thanks as always, for watching my videos and thanks so much for being part of Does your gang and I'll see you guys in the next video.

Hey, guys, this is part two off my introduction to classes and objects.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

A2 初級

クラスとオブジェクト入門 - パート2 (データ構造とアルゴリズム #4) (Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4))

  • 11 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語