课程: C# Practice: Interfaces and Abstract Classes

Solution: Implement an abstract class - C#教程

课程: C# Practice: Interfaces and Abstract Classes

Solution: Implement an abstract class

- [Instructor] The code challenge was to create an abstract class with an abstract method. Then override the method in the derived class. Before I show my solution, let's spend a minute talking about abstract class concepts. All classes need a base class. By default, it's system.object. So if you look at this class here on line 25, since I didn't specify a base class it derives on the default. However, it can be beneficial to create a common base class that multiple derived classes can share. For example, in the .Net framework, there is the stream base class which represents a stream. Then there are the FileStream and the MemoryStream classes which are more specific types of stream. So if we decided that we want to implement this as a base class, and you can see I'm using it up here on line 15, I have a TradingCard that by using this colon derives from Card. Now I've marked this class as an abstract class with this abstract keyword. Now any member declared in the abstract class like a method, or a property, or event is available on the derived type. An abstract class cannot be instantiated which forces developers to create a derived type to gain access to the card members. We'll talk about methods in this video but the principles apply to the other member types too. Now, within this class, I'm going to declare some methods. We have two choices in an abstract class when you create methods. The first is the one that we asked for in the code challenge. You can make an abstract method which contains no code. That forces the developer to override the method in the derived class. That is accomplished here by creating this method and then using the abstract keyword. So essentially what I'm doing here is I'm defining the signature of the method. I'm saying it's public. It's going to return to double. It's called GetSalePrice, and it has no parameters. Then up here in my TradingCard class, I'm creating a concrete implementation of that. Has exactly the same signature, it's a double, it's called GetSalePrice, it's public, and it has no parameters. And then I specify that I'm overriding it in the base class with the override keyword. Now let's take a look at what happens if I do not derive from Card. So I'll delete this. I'll get a red squiggle that's telling me that the compiler can't find a suitable method to override. So it's looking in all the base classes for TradingCard and it's not finding any method called GetSalePrice. So I'll put that back in. Now let's say that I changed the name of this method. I'm getting a red squiggle again, and it's the same message. Now it can't find a method with this name. What happens if I remove the override keyword? Now I'm getting a yellow squiggle and that tells me that TradingCard.GetSalePrice hides inherited member Card.GetSalePrice. Then it tells me the solution is to mark the current member override with the override keyword. Otherwise, use the new keyword. In most situations, you want to use the override keyword, not new. So let's put that back. You notice I did create a concrete implementation of this method. Not only did I create the correct signature but I also wrote some code in here that does some work. Now, that's my solution, which solves the code challenge. Let's go one step further. Let's go back to my abstract class and we'll look at line 29 where I've declared a method called, UpdateInventory. Remember I said there were two choices for creating methods in an abstract class. The other choice is to create a concrete method and that's what we're doing here. In other words, you write code in the base class. Then the derived class gets that code without any additional work. So here you can see, I've got a concrete implementation and that means that if I can go to the TradingCard class, I can use the UpdateInventory method without having to write any additional code here. It's also possible to override this UpdateInventory method as with the same keyword we did here. Let's talk about that. So lets uncomment this. I get a yellow squiggle, and it's telling me that it's hiding an inherited member. What's happening here is that in the base class, I haven't given permission for the derived class to override my method. So you can see down here it says, "It can be overridden if it's marked as overridable." And C# use the virtual keyword for that. So let's go up here, I'll mark this as virtual, and then I can go up here and override. (keyboard taps) And now I've got no squiggles, I can test my code. And everything is working correctly. So to review, to solve the code challenge, I created an abstract class. I created an abstract method and specifies the signature. Then in TradingCard, I derived from the Card base class and then I overrode the abstract method.

内容