In this video we are going to create a car object and a motorbike object from a struct that was created in Mojo. So let's see how that can be done. First of all, once again, we have created a second file here. And this import statement says that from the vehicle file we want to import something that is called a vehicle. And a vehicle is basically something that has wheels and that can have a color or seats.
And that is common for most vehicles. Therefore, it makes sense to create something that we can use for cars, motorbikes and maybe also for a truck. So let's do that in this case. And I would say my truck is a truck with a trailer. The color is green.
The brand is it's a Mercedes Artego. It has four wheels and two seats. And now I can call a function to see the details of my truck object. And when I run this code, I get an error because I have missed something here and that is to tell Mojo that this is going to be a vehicle.
So let's repeat that. And now I get an output. It says that my first vehicle is a passenger car, which is blue. And a Dacia Lodgy is the brand. The number of tires is four and the number of seats is seven.
The motorbike is black, it's a Kawasaki KMX. It has two tires and two seats. And the next vehicle is a truck with a trailer. It's green. It's a Mercedes Atego with four tires and two seats.
While this is a simple example, it already shows that we have a structure in the data. And that is because in this file here, we have created something that is called a struct. In other language types, it would be called a class. And basically you can think of it as a template. Actually, this car and vehicle example helped me to understand the whole thing.
Because when you think of a vehicle, you can have some common attributes. For example, all vehicles have wheels. And there also might be things that are different, like the number of seats. And when you have structured data like this one, you could, for example, store 100 different vehicles, let's say for a car rental business. And when a customer comes in and says, well, I need a car with seven seats, you could run a query in your database and find all the cars that have this property in the data set.
So what this thing does is: we say it's a struct and we call it a vehicle. And now we can give it some properties. The vehicle_type is a string. That's also the case for the color and the brand and the number of tires and the number of seats are integer variables. So basically, when you create a new instance, these are the properties that are required.
So if I leave out the number of seats and try to run my file, I get an error here where it says I expect six arguments, but I only got five. Or in other words, you have missed to give me the last one. And that would be a way to make sure that everyone who creates a new vehicle object in our database would actually create it the same way as the other objects have been created. But let's get back to the vehicle file. Here we have a function that is called Init.
It starts with in out self and we pass the vehicle type from here. The color, the brand, the number of tires and the number of seats here. And now we connect all the arguments here to the self object. Which means whenever we pass this kind of data, a new object will be created that has exactly these properties. And here below we have created a simple function that is able to print out all the properties for the object so that we can see the details.
And the good thing is that we could now change this struct here. So let's say in this new function we want to give a return value if it is available. That return type is a Bool, which means it's a Boolean variable. So it can be either be true or false. And let's say we want to return true because it's on the parking lot and it can be rented.
And now I could use something like this to print out that the truck is available for rental. So basically what we have done now is we have changed the template here, or in other words, we have created a function for an existing struct and that will be available for all the objects that are created with the Struct. Okay, that's it for this video. I hope it is a little bit helpful for you. In my case, this vehicle example actually helped me to understand classes.
That was for Java some time ago. But this struct here in Mojo is basically the same. Okay, that's it for this video. I say thank you for watching and I will see you in the next one.
In questa pagina del sito puoi guardare il video online LEARN MOJO TUTORIAL - 21 HOW TO CREATE OBJECTS FROM A SELF CREATED STRUCT della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Learn Mojo Tutorial - Simple AI Tutorial 01 gennaio 1970, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 76 volte e gli è piaciuto 2 spettatori. Buona visione!