Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Overriding Inheritance in PowerShell 5 Classes

$
0
0

Summary: Ed Wilson, Microsoft Scripting Guy, talks about overriding inheritance in Windows PowerShell 5.0 classes.

Microsoft Scripting Guy, Ed Wilson, is here. One of the things that is pretty cool about Windows PowerShell 5.0 classes, is that I can override the things I inherit from a base class. This offers a lot of flexibility because it means that when I inherit from a class, I can customize my new class based on my needs instead of being stuck with a lot of stuff that does not meet the requirements.

One thing I need to do is to override a couple of the properties that I inherit from my Vehicle base class. The reason is that I want to limit the Make and Color to specific values that I define via a couple of enumerations.

Note  For more information, refer to Adding Enums to PowerShell 5 Classes.

My base Vehicle class defines the make and the color as a string. This can be any string that I want to type—there are no limitations. However, cars are only made by specific companies, and they only come in specific colors. The string was appropriate for the base Vehicle class because I do not want to have a huge enumeration. For example, if I also inherit the Vehicle class and create a Bicycle class, I will have different makers than I have for cars. I will also probably have different colors.

But when it comes time to creating my Car class, I want to limit it to specific car makers and car colors. Here is the Vehicle base class:

Class Vehicle

{

    [datetime]$year

    [String]$model

    [string]$make

    [String]$color

}

Here is my Car class that inherits from the Vehicle class:

Class Car : Vehicle

{

 [string]$vin

 [MakeOfCar]$make

 [ColorOfCar]$color

}

And here are the two enumerations I want to use—the MakeOfCar enumeration and the ColorOfCar enumeration:

Enum MakeOfCar

{    

    Chevy = 1

    Ford = 2

    Olds = 3

    Toyota = 4

    BMW = 5

}

Enum ColorOfCar

{

    Red = 1

    Blue = 2

    Green = 3

}

The complete script is shown here:

Class Vehicle

{

    [datetime]$year

    [String]$model

    [string]$make

    [String]$color

}

Class Car : Vehicle

{

 [string]$vin

 [MakeOfCar]$make

 [ColorOfCar]$color

}

Enum MakeOfCar

{    

    Chevy = 1

    Ford = 2

    Olds = 3

    Toyota = 4

    BMW = 5

}

Enum ColorOfCar

{

    Red = 1

    Blue = 2

    Green = 3

}

When I create a new instance of the class, I see the output—note that now the make and the color are initialized to zero.

Image of command output

After I create a new instance of the Car class, I store it in a variable and populate the properties. This iw shown here:

PS C:\> $a = [car]::new()

PS C:\> $a.color = 1 ; $a.make = 2 ; $a.model = "mustang" ; $a.vin = 12345 ; $a.year = "1/1/2015"

PS C:\> $a

vin   : 12345

make  : Ford

color : Red

year  : 1/1/2015 12:00:00 AM

model : mustang

Notice that the Make is now a type of the MakeOfCar enumeration, so when I say it is equal to 2, I get Ford. The color is also now a type of the ColorOfCar enumeration, so 1 equals Red. The Model is still a string, so I type Mustang. The Year is also an instance of the DateTime object, so when I type 1/1/2015, it is converted (cast) to a DateTime object. The VIN is also a string.

The year, model, make, and color are all defined in the base Vehicle class. The VIN is defined in the Car class that inherits from the Vehicle class. The make and color are overridden in the Car class, from being strings to being the appropriate enumeration.

I can verify all of this by looking at my object via Get-Member:

PS C:\> $a | Get-Member -MemberType Property

   TypeName: Car

Name  MemberType Definition                

----        ----------        ----------                

color Property   ColorOfCar color {get;set;}

make  Property   MakeOfCar make {get;set;} 

model Property   string model {get;set;}   

vin   Property   string vin {get;set;}     

year  Property   datetime year {get;set;}  

Now you understand how to override inheritance in Windows PowerShell 5.0 classes. Windows PowerShell 5.0 Class Week 2 will continue tomorrow when I will present a video recap about overloaded constructors.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 


Viewing all articles
Browse latest Browse all 3333

Trending Articles