Python for .NET Developers Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: C# Car App

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Before we get to the Python version of our object oriented programming let's look at the equivalent over in C#.
0:08 What we're going to do is we're going to create a hierarchy of cars. Regular cars, sports cars, electric cars. So here we have the car class
0:16 and this is meant to be only a base class. So it's abstract. And it has some auto properties here: model name, engine type, cylinder, and base price.
0:26 Those are all passed in to the constructor here like that. It also has cool little property. It'll tell you if it's electric, true or false.
0:33 And that is to just obviously check whether the engine type is electric. There might be better ways with enumerations and other stuff to do that
0:41 but we're just keeping it simply here. Just use strings on this one. Then we're going to ask the car to drive. And the default car will just print out
0:48 the minivan goes vroom or something like that. And then all these, I put the base class here
0:55 or the class, the type name that's this implementation is coming from. You can put dispatch together. We'll do it in Python. You can compare.
1:02 They're similar but maybe not exactly the same. That's just drive and it's virtual so it can be overridden.
1:08 Right? For example, the sports car over here might want to change what drive means. Then, there's also the ability to refuel.
1:17 And because we have both electric and gas cars the base class, it just has I have no idea. I got to throw up my hands on refuel.
1:23 It's so different what that means. The class that derives from me is going to have to figure that one out. Right? So when we derive from this
1:30 for example, the sports car it must implement refuel if we want the sports car class to be able to be created, instantiated.
1:38 Let's just look at one of these or we can look at the electric car real quick. This one simplifies the constructor.
1:43 Takes the model name and the base price but it doesn't pass the number of cylinders or whether its engine is electric. It uses the base
1:50 a delegate to the base constructor for that. Okay? So it just passes through the model name and the base price, but the other two
1:57 these are set by virtual the electric car. It overrides refueling. With the electric car, like I said it says the type name at the beginning
2:06 just for this example, so the whatever is charging up and then we're driving, it overrides this as well so we can create an electric car
2:14 and says the whatever it is zooms silently along. We also have a parking lot over here. And we can create a parking lot using a factory
2:22 a static factory method. Give it a number of spots, like I want Three levels and five spots, so we're going to create
2:28 15 parking spots on each level. Return those. I have some iterator stuff going on here. That's pretty cool. We can get back how many spots are taken.
2:38 And we can park the car into one of these spots. So we can loop over our spots and then figure out, no one's parked there
2:45 and we're going to park our car in that spot. Simple data structure, just dictionaries and so on but that's what we're going to do
2:50 in this particular thing here. And last but not least, we can run it. I'm going to create some cars.
2:57 We're going to loop around and ask them to drive and fuel and then we're going to park them. So I'll just run that real quick. Boom, hello C# cars.
3:04 The sports car, the Corvette, tears along the highway. And then it only wants the best gas. And then we create another one.
3:10 A Windstar minivan, it goes vroom. Cause this one does not override the base class. It's a basic car.
3:17 But it must implement fuel, so it will take any old fuel. Electric car, zooms along silently, is charging up.
3:23 Volt zooms along silently, is charging up. And then here's a bunch of free spots. We park a few, and we have
3:28 The Corvette and the Windstar and the Tesla and the Volt they're all parked in these very spaces. All right, this is our world in C#.
3:36 We're not going to create it, it's already created for you. Our goal is going to be to explore these ideas
3:41 and create something similar to, or as similar as we can in Python.


Talk Python's Mastodon Michael Kennedy's Mastodon