In this lesson and when I show you a few examples of how to write a class in Strief So that's going
to start with I'm going to go to X court and in a year I'm going to start a new command line tool and
it's going to be a swift language.
I'm going to call the.
Even the strength.
Perfect.
So here's the first example is actually going to be class and I left school autocomplete for me so I
know what they should do.
I have to give it a name I can or can you know not give it a superclass.
And I have to give it some code.
So let's go ahead and write that I'm going to say my first class is a user across.
And what does my User class have.
It has a variable called the name of the Type A string.
Keep in mind that I'm making my name and implicitly forced on route which means name must be there then
I'm gonna save that.
Let's say it have something called for your name.
And that's an option or maybe my user doesn't have a photo.
I have a variable caller ID you ID that my user uses unique ID essentially that's going to be a string
that it must be there.
I cannot not have it and then I'm going to have another thing.
Let's say there are right and this one is going to be an integer and that one should be there as well.
And that's about that.
We now have a class user that has some attributes.
Let's go ahead and build an instance of it.
So this is how to make the class up here.
And this the body of it.
And now we've got to make one instance an instance.
I'm going to say that I'm going to call it my user and new user is of the type the user is going to
be a brand new user.
And then I can say emule user name is going to be staring at me or user dot height.
It has to be an integer value to say 200.
I could not give it a for Tony.
Maybe I don't want to have a photo on him.
I'm going to say I mean use a dollar to you ID is going to be a set of different characters and that's
about that.
Now we have a user that that user is of the type class user.
I could go ahead and say print what I want to print is the user name it is and then I would have one
of these interpretations and I would say you use that name and I have to unwrap it because it's an option.
So let's go ahead and now what I see that's username is exactly the way we expected it to be up.
I should show you something that's completely irrelevant.
But you know it doesn't hurt for producing unique values.
Ian Swift you could use this you ID dot you you I.D. a string and now it will produce a unique value.
So I could say are you ID is and I would get the new ID and you give me a unique ID for this use the
that's a unique number that unique combination that swift produces sometimes when you are you know writing
things internally you would depend on this.
So that was our first example of making a class in my next example I'm actually going to change this
bit.
The one that we do on initialization of our class and you it a custom and it's somebody going here say
in it.
And that's like a default you know class of every default method of every class.
And if you are dead it means you're overriding in it.
So I'm going to say initialize this let's call it input to name a string and string and then input forth
or a name.
That's a string option or maybe there or maybe it is near and then input new ID.
That could be a string.
I'm going to change this in a second.
So if you're typing just hold on for a second and input height and input height it's going to be an
integer.
Now why I say I have to change the input ID because it's most likely that for something like a new ID
you wouldn't know it when you are creating a new user.
If you're not making a new user you probably know their height you know their name if they have a photo
unit you know them and you might want the compiler to produce your ID for you.
So I'm not going to actually input a new idea.
I'm going to say at the end it's time to do this say self that's a name is input to name.
So when you are initializing and you one with this method set its name to whatever name I give you.
If Let's let's call it him put forth a name that is input to a name.
It means if there is an input for the name then also make our Fortran name to be input photo our name
and or self what our name that's really irrelevant you could use it or not.
I'm kind of I kind of prefer using the stuff and that's sort of it is input height.
So when we call this method we introduce three values in it and name four to a name and a height and
then our object gets produced based on those for what happens with the Y.D. we are producing it.
But we don't have a new idea.
So what they have to do is they have to manually add that to my in it as well which is I'm going to
say I'm self-taught you ID will become you you ID Dar's you idea string perfect.
So now when we initialize everything works the way we want it.
Now how are we going to use this initialize method.
I'm going to go here to I mean to use it but I'm making a stance.
I'm going to save the user.
And Knight says Oh use can only be initialized with this method because that in it has been overridden.
And I said Oh.
So the name is I mean your input photon on AM I'm going to say nil because it is an optional that can
be nil.
When I say I don't have a to a name I'm going to say the height is hundred eighty and that's about that.
So I don't need any of these guys anymore because at the initialization everything has happened.
So really quickly once again I'm going to change my format so you can see it a little bit easier.
Maybe I bring this here which is a more common practice like that.
And now if I bring that one down up there.
OK.
So now we have everything we have a user that has all these three.
And then we have an in it for it that initializes it.
And when I use the in it I'm going to provide it with a name no photo name and height.
So far we wrote a class that has some attributes and has an initialized method.
Now let's go ahead and write another class that this class and actually translate something for us.
Of course we can write an entire translator but what I'm going to do is I'm gonna write a class that
can translate to one senior word from English to either French or German languages.
Let's see how we're going to do that.
I'm going here I'm going to get myself into the space.
Say I have a class called translate.
All right.
My translator class has a variable called language which is a string.
So I have this one and it also has this it has an in its beat him language which is going to be a string.
And whenever we do it obviously self-taught language becomes implied goit.
So so far we have actually managed to initialize our translator and then I'm going to say this I'm going
to say it has a function to translate.
And what does translate do is the receivers of war.
So let's say impute to war is going to be a string.
It doesn't give us anything back.
What it does do it has to translate that whole depending on what's the language.
Some will say if language is let's say German then we assume that it that input word is always going
to be hello.
But you could you know write it differently on the say if language is German then prints hello.
In your mind is it.
And then hello.
All right.
Yes indeed.
If that wasn't the case then when say hello in French is I was born or something like that.
So let's say How are we going to use that.
I'm going to say.
Give me an object.
So now my French is going to be translate initialize with a language called the French French and if
now from my French I call a method called translate and I translate hello.
Then if we run this it is going to show us hopefully hello in French is perfect and all the other things
are printed because my other programs are still in here.
My other class.
So once again really really quickly.
Classes read declare them class and then we have a set of attributes and we how we can have fun in it
if we want.
And we also can have functions if we need them and then depending on those we can do a variety of things
something really quick about naming conventions.
We often not often almost all of these we capitalize our class names so we don't color class translate
we call it capital-T translate and that's a very very common practice.
I would suggest that you know you do that as well.
So in case you still need some help with this I'm going to keep this on the screen for a few seconds.
And then after this I'm going to scroll to the other class there are first class and keep it here for
a few seconds as well.
So now that's these other two classes are the only thing that really matters is this.
Class one attribute in it.
And then this one a function.
Beyond that everything is things that you have done in the past and it would be doing more and more
of these in the coming Assen.
So until then.