Locked lesson.
About this lesson
In this lesson, we learn how to create Object Methods that operate inside your own classes.
Exercise files
Download this lesson’s related exercise files.
46 - Intro To Classes Object Methods.docx57.7 KB 46 - Intro To Classes Object Methods SOLUTION.docx
59.3 KB
Quick reference
Intro To Classes: Object Methods
Object methods allow us to create methods in our classes.
When to use
Use them whenever you want to create methods for your Classes.
Instructions
{
// Create Book Object
Book book1 = new Book("C# Programming", "John Elder", 309);
Console.WriteLine(book1.BigSmall());
}
class Book
{
public string title;
public string author;
public int pages;
public Book(string myTitle, string myAuthor, int myPages)
{
title = myTitle;
author = myAuthor;
pages = myPages;
}
public string BigSmall()
{
if (pages >= 250)
{
return "Big Book"
}
return "Small Book"
}
}
Hints & tips
- Object methods allow us to create methods in our Classes
- 00:00 Okay, in this video I want to talk about object methods.
- 00:06 Back at the beginning of this section,
- 00:08 I explained that objects can have attributes, these guys.
- 00:12 They can also have methods.
- 00:13 And I talked about a car having a transmission method, an engine method,
- 00:17 an air conditioning method.
- 00:19 And methods do what methods do, they take in input, they do something,
- 00:23 they spit something back out, right?
- 00:25 So our classes can do the same thing.
- 00:28 And remember, several times back in the course we've started to type things and
- 00:33 we wanted to convert something like this.
- 00:35 And then we wanted to convert it, so x.to and then these things start to pop up,
- 00:40 right?
- 00:41 These are sort of methods that you can call that do something.
- 00:45 Well, we can do this same thing in our classes.
- 00:48 We can define a.to something or a dot anything we want and
- 00:51 that will then do whatever that is we've designed in our class.
- 00:55 So to create an object method, you just come down here underneath
- 00:59 our constructor method and just create another method.
- 01:03 So we could go public, and this is going to be a string.
- 01:06 Let's create something called BigSmall.
- 01:09 So I want to know if our book is a big book or a small book.
- 01:12 Does it have a lot of pages or just a few pages, right?
- 01:15 So we can create a little method to sort of figure that out, right?
- 01:19 So we know how many pages are in each book, right?
- 01:22 So let's do some logic.
- 01:23 So let's go if pages and we know how to do logic, right?
- 01:28 We can use our comparison operators are greater or equal to, what's a big book?
- 01:35 Say 250 pages.
- 01:36 Well then, we want to return the word big book, all right?
- 01:40 We can just ask our program, hey, is this a big book or small book?
- 01:45 And if it's a big book, it'll return big book, right?
- 01:48 Else, we don't even need an else statement,
- 01:52 we could just say return Small Book, right?
- 01:56 Because if the if statement runs, the whole thing terminates and
- 01:59 it won't go back down.
- 02:00 If this doesn't run, then this will run because obviously it's not greater than or
- 02:04 equal to 250 pages.
- 02:06 So therefore, it's not a big book.
- 02:08 So let's come back to our main program and instead of asking for
- 02:12 book1.title, we can say book1.
- 02:14 And I can start to type b and you can see BigSmall pops up.
- 02:18 So if we save this and run it, We can
- 02:23 see big book because remember book1 has 305 pages,
- 02:29 which is bigger than 250 pages.
- 02:32 If we want to say book2.BigSmall, if we save this and run it.
- 02:39 We see it's a small book.
- 02:41 Now, this is a little confusing, maybe what is big book and small book.
- 02:44 Maybe we want to be a little more descriptive over here.
- 02:47 We could do something like title
- 02:53 is a Big Book, otherwise title
- 02:59 is a Small Book, whatever.
- 03:04 But the cool thing about this is, we've created an object, so
- 03:08 our class here knows what book we're talking about.
- 03:11 When we say title, it knows which title to look up.
- 03:15 Why?
- 03:15 Because we're saying right here, book2,
- 03:19 it knows to look in book2 for the pages and the title.
- 03:22 So here we can save this and run it again.
- 03:26 Should we save this page.
- 03:29 And here it says Python programming for fun is a small book.
- 03:34 And it still having these two things here.
- 03:36 Maybe we can get rid of those if we want to and comment this out, right?
- 03:42 Whatever you like.
- 03:43 Here let's go book1 again.
- 03:45 Save this, run it again.
- 03:48 And now it's the C-Sharp programming for everyone is a big book.
- 03:52 Very cool, right?
- 03:53 We're creating our own object methods that will do anything we want.
- 03:57 And this is, understandably, a very basic thing, right?
- 04:01 We're just counting the number of pages.
- 04:03 But anything you could think of to create a method for
- 04:06 you, you could create it in just this way.
- 04:08 Public string, name it, and then just do your stuff between these curly brackets
- 04:13 and return anything you want.
- 04:14 Because your program is going to call this,
- 04:16 it needs to return something back to the program.
- 04:19 In this case, it's just returning this thing onto the screen and just that easy.
- 04:24 So this has been admittedly a very quick crash course in classes and objects.
- 04:29 There's a lot more stuff to learn, we just barely scratched the surface with this.
- 04:34 But you've learned, hopefully, what a class is, what an object is.
- 04:38 How to create a basic class and a basic object.
- 04:40 How to create a constructor method that gets called every
- 04:43 time the object gets created, and does something automatically.
- 04:46 And then also how to create your own object methods that let's you
- 04:51 do anything at all you want to do and
- 04:54 then pass those things back into your program and do cool stuff with them.
- 04:59 So that's all for this video.
Lesson notes are only available for subscribers.