14. How to program in C# - INTERFACES - Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

NOVICES: If you don't know what interfaces are, or you're not sure why you'd use them, listen up - interfaces are one of the most important things you can learn if you want to write good code. Interfaces are the backbone of every large, well-written, well-tested and well-maintained codebase out there.

👍︎︎ 8 👤︎︎ u/neoKushan 📅︎︎ Sep 27 2015 🗫︎ replies
Captions
thanks for tuning in at brakus hello and welcome to video number 14 in how to program c-sharp course in today's video we're going to have a look at interfaces so an interface is basically like a contract that you can sign with a class saying what needs to be in that class so if you look at an interface it's very much like a structure of what methods and properties needs need to be in that class so that sounds may be a bit abstract to you but basically you don't have to get very far in programming before interfaces become very useful for making things simpler safer and a whole lot cooler so that's going to be awesome and we are a pretty good way into the series here so I quickly want to mention that if you go to Priya's comm I do have a donate button here where you can support Brad keys so you can simply click on that and it will take you to PayPal and you can donate any amount that you want to all of the stuff that I upload is free all of the assets on the unity asset store is free and assets pack so if you want me to keep doing this for free you can of course help me out by donating so I really wanted to mention that it's of course not something that you need to do and I don't expect you to either but if it's something that you feel like would would feel would be a good idea I'm not going to stop you oh cool so let's dig right into today's video so I've opened up Semoran studio here and if we take a look at implementing a simple interface we can basically describe an interface as kind of saying what should be in a class and then the class itself describes how each method is implemented so an example of an in face we're going to create one here could be lets say an item so we wanted to have an item system and we wanted to define the structure of all items well then we create an instant interface for those items and I'd like to begin all of my interfaces with an I so here's going to be AI item just so we can identify them as interfaces and then we open and close some brackets here and inside of the interface itself the syntax is are actually really simple we don't need to do any kind of availability layers so we can avoid the public and private and all that and basically we just need to define the datatype for the property so we're going to have a string with the name of the item and you can't just close this off this has to be marked as a property meaning that we have to do get and set that's just a formality but it's something you're going to have to do and then we can also maybe make an integer with the gold value of this item and that's also going to be a property so we do the get set here and then we can maybe make a couple of methods so first off we're going to have Boyd and this should of course we pull off load depending on what you want to return so we'll just say save void here and we'll make this equip so we can equip our item and another one for is selling it and none of these are going to take in any parameters either that's also something you'll have to think about here so that's a very simple interface and if we wanted to implement this we could go in here and define a class and we could call this a sword and of course open and close some curly brackets and in here we could create a public string name and again just simply that's use the cost default get set and a public integer gold value and again use get set cool and then we could go ahead and create a constructor so we'll make this a public sword and we're going to take in as an argument on the name so string name so we can give it a name once we create a sword and we're going to set name equal to underscore name the name passed in and we're going to set gold value equal to 100 so we'll just say that all swords in our game has a goal value of 100 imaginary dollars and coal so you can see here that we've now implemented the two properties and that we haven't yet implemented the methods and the program is going to notify us of this so if we save this and hit play you can whoa this actually runs oh I know why it's because we need to implement the interface stupid me so this is very central right now we just have a class definition and an interface definition but if we go ahead and actually inherit from this interface just like we've inherited from other classes before so we can inherit from a item here then you can see that when we hit play it's going to say that it doesn't implement the interface member equip and if we click on this you can see that it doesn't either implement the interface net member sell and if we didn't have these values or properties here it will notify us to implement those two code so and you might be thinking right now well why don't we just use a base class and the reason for this is that we can use multiple interfaces that we can derive from multiple interfaces and also this allow is to define how we how the actual methods and properties are going to work in on the derived class itself so now we can simply go ahead and make is some fairly simple methods here and equip method and of course we don't have any logic for a rhythm equiping items here so we'll just write out that the name of the item and then has been equipped and actually we'll just say crypt shorter and then we'll have another method called sale and this one is just going to say that console.writeline the name of the item plus sold for plus the gold value plus imaginary dollars cool and now when we hit play here nothing is going to happen but we don't get any errors so now down here we can go ahead and create a a new sword you can create a sword and we'll just call this sword as a reference and we'll they equally to a new sword oops not an S fight a new sword and we'll give it the name sword of Destiny and then we can call methods on so we can say that we want to equip this sword and then we want to sell this sword so now when we hit play you can see that sword of Destiny was equipped and sort of destiny was sold for a hundred imaginary dollars cool so that's the simplest use of an interface that I can come up with and this works just fine already but let's take this a step further so I've drawn this thing in paint I know it's the most beautiful thing I've ever seen in my life to you but bear with me so if we look at interfaces as like tags that we can apply to our objects because right now we've tap tagged this class as an item well we could also tag it as a quality so we could basically say that this item has some kind of quality level and therefore should be able to be damaged so we can add that kind of dimension on to the class we would also tag it as part of the quest so we could create another interface for that and then we can basically have different items so we're going to have an sword and an axe and both the sword and the axe maybe has its tagged as an item of course and we both want them to have some kind of quality level and be able to be destroyed when used and but maybe only the sword is really important for one of the quests the access to something we picked picked up and therefore something we can just sell no matter what so this is you see some a piece of logic that is available in pretty much any RPG that I've played at least because it allows us to very dynamically use our code throughout our game and then if we even go further with this we can have an inventory with all of these items so we can have an array of items and then we can loop through that array and check what items are marked as part of a quest and those that are that are we can call a method on those items that only those items have that we want to turn in the quest turn in the quest items so this might sound complicated but I think we'll go ahead and implement this so first off let's create the quality layer and that's going or the quality tag and it's going to act pretty much the same as our item tag so we're going to have an interface here I quality and I know the name here is not perfect but we'll just go with our quality and this one is going to have some kind of integer and we'll call this quality level no we call this durability actually instead of quality let's call this a damageable I like that a lot better I should have thought of that so long ago okay so interface I damageable and it's going to have an int durability and it's going to of course have a getter and a setter and then we can create a method that will us here that will say take damage and it's going to take in an integer amount and it's simply going to say that durability minus equals and then we'll say 20 oh no of course amount amount cool so and the name of this argument basically doesn't matter that's completely irrelevant and of course this is on the implementation side we just want to finish this off I don't know I got confused there sorry but basically we we do this structure where we want to have a void that takes damage and we want to put in the amount as an integer and it doesn't matter what you name this here because we can just rename it or name it something different down here and it's not going to throw any kind of errors or anything so now we have this damageable tag which forces our objects or our class to include a durability variable and 88 damnit damage method so let's now take this with high damage ball and now we can create a public void take damage and it's going to take in the amount or it could call this damage just to show you that that would work I'm going to do that and then we do the logic here and of course now we want to name this a damage and we also want to implement the durability so we can say public integer and and this is going to be called to rapidly T and simply increment the cast and normally it gets it and we could do something here again with a private variable underscored durability that will make sure that durability doesn't go below zero and all that which you've seen in previous video so again we have complete control over what this durability variable actually does what its value is and how we change it cool and now down here we'll be able to we'll say that we want to the sword and then we want to damage the sword by 20 and whenever it takes damage let's also print out that the name of the item and then took damn or let's just say damaged by and then we'll put in the damage amount and then say it now has a durability of and then put in the durability cool yeah us so let's save that and hit play whoops expected a : am i missing I'm missing a plus sign there hit play cool and it says sort of testing equipped sort of destiny damaged by 20 it now has a durability of negative 20 sort of distance so for a hundred imaginary dollars cool so I for some reason okay so durability of course defaults to zero so up here in our constructor will simply say that durability should be equal to 30 by default and now you can see that it says that it has a durability of ten awesome so that's kind of the idea that we can create these tags and mark our items and if you just take this whole sword class here and copy it down here we can very very easily create an X so it's again that's going to derive from item and damageable and this is also going to be X it's going to take in the name it's going to start with a gold value of 70 and a durability of 50 and all of these implementations are going to be the same and then down here we can create an axe call it X set it equal to a new axe and set in its name to fury axe and then we can maybe equip the X and we can of course damage the damage the axe and we're going to damage this by 10 and finally we can sell the axe so we can do all of the same stuff and let's just make an empty line between these simply write out console.writeline and you can see that it does all the stuff for the sword create an axe and does all of this stuff for that too so that's super cool but that's just adding on to the amount of like city that we have in our game next up is the actual very very cool part of this and that is marking these items as Chryst's and then using that in our actual game logic so if we should go ahead and click create an interface here that the idea is very simple we go ahead and create an interface called a part of quest and again we do all of the brackets we then create and let's just create a void we could have of course some more information like the name of the quest and all that but here we'll simply force it to include a void called turn M that will turn in the item and that's basically all of this that this interface is going to implement so or all the classes deriving from this interface is going to be forced to implement I should say and now we can make our sword derive from this but not our axe so let's do a comma here and implement I pod of quest and it's super cool this idea that we can see all of the attributes of our sword right here it also makes it very easy to read the code once it gets more complicated and very easy to add functionality to a class and then we simply want to include the logic here so we'll make a public void and we'll call this turn in and it's not going to take in any arguments we're simply going to say that console the right line and then name of the item turned in that's it cool so now down here our sword will be able to call the turn in method but our X won't so we retry here X dot turn in you can see it doesn't autocomplete and if we would get an error if tried that so now let's see what we can actually do with this so down here we can create an inventory and we simply need to mark this we need to make the type of a item so this is an array of items meaning an array of classes that derive from the eye item interface and this is simply going to be called inventory or items and we're going to set that equal to a new item array which is going to take us to be able to store two elements and in the first element so index zero we're going to set it equal to this sword that we created and the second index or the second spot in our array meaning index one it's going to be equal to X so both both of these or these are different classes but they derive from the same interface and therefore we can add them into our array and then we can loop through and turn in all quest items so we can say for int I equals 0 is less than inventory length I plus plus so this is a symbol for loop I hope you remember those if not go back and watch the video because for loops are kind of essential and then we simply want to say that then we want to create an eye part of quest and this is going to store the quest item and we are going to set it equal to inventory and then the index and it's okay so let me just explain what we're doing here so we're creating an inventory and the that's going to act is an array of items and it's going to have a total capacity of two the first inventory item we set eager to a sword because that derives from a item and the second spot we said equal to ax and that also derives from a item then we loop through all of the elements and the element that we are currently looking at is called inventory to I so when we go through the first time this is going to be the first element which is the sword it's going to say inventory zero here and the second time it's going to be inventory one which is the X and then we try and put this into a variable of type I part of quest and we call this the quest item so if we just do this it's going to give us an error because right now the inventory item is of type a item and the quest item is of type I part of quest but we can cast this object into a I part of Chryst by simply adding an as I part of quests so now this is read as create a variable of type I part of quest and fill it with the inventory item of index I as as an I part of quest object so what this does is basically if the inventory is marked with the I part of quest tag if it derives from the interface well then it's simply going to be transformed into the quest item available but if it doesn't which are ax doesn't well then this is not going to wear again a quest item is going to be equal to 0 because the ax is not a quest item and therefore and christum will be null so now we can write if quest item is not equal to null meaning if the object that we are looking at indeed is marked as a quest item well then we can go ahead and make sure then well then we assured that it has a turnin method and therefore we can call it so now we can say that we want to call the turnin method on quest item so that is completely right and this should work for any number of items that we decide to create so let's try and run this and see if it's actually working so let's hit play and that website just want to quickly make some more room here and now we play again and you can see that we create a sword and we equip it and it says that it was equipped then we take some damage and it was damaged then we sell it and it says it was sold let me create an axe we equip it damage it and sell it and then we create an inventory array which is basically an array of eye items then we fill in the two items that we have then we loop through it and we check and we try and force these items into an a variable called quest item of type I part of quest and then we check if that succeeded and if it did meaning it wasn't equal to null then we turn it in and you can see here that it says sort of destiny turned in because that is indeed marked as a quest item if we were to go up here and mark the acts also so I quest part of quest item and we were to create a method called turn in again we could just copy and paste the method here or we could make this completely different we can say that name here turned in and then we can say it was it was an axe you can see we can change this how we would like so now when we hit play here it's going to go through and turn both of the items in so when we play it says sort of destiny turned in and fury acts turned in parenthesis it was an axe cool so that is basically all I wanted to show you with interfaces both the really simple way of looking at it as basically a way of making your code look cleaner and easier to follow and the other way which is using the inventory to kind of define how object objects should interact cool so thanks for watching I hope you enjoyed this video I hope you could you were able to follow it even though some of the stuff showed here is actually fairly complex I'm very proud if you've gotten this far into this series so again thanks for watching and I'll see you in the next video
Info
Channel: Brackeys
Views: 227,865
Rating: undefined out of 5
Keywords: unity, unity3d, tutorial, beginner, easy, function, and, settings, how, to, howto, learn, learning, course, series, tips, tricks, tutorials, workflow, fix, tip, technology, game, development, develop, games, programming, coding, basic, basics, C#, Software, abstract, contract, interfaces, interface, inheritance, polymorphism, pillar, third, object, oriented, oop, c# (programming language), c# tutorial, programming tutorial, xamarin
Id: IQpss9YAc4g
Channel Id: undefined
Length: 27min 16sec (1636 seconds)
Published: Sat Sep 26 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.