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