π’π₯οΈ Introduction to Object-Oriented Programming in PowerShellπ₯οΈπ’
Table of contents
No headings in the article.
Object-oriented programming (OOP) is a powerful paradigm that allows developers to create modular and reusable code. While PowerShell is primarily known as a scripting language, it also supports object-oriented programming concepts, making it versatile for building complex applications. In this post, we will explore the basics of OOP in PowerShell using an example of two classes: Student
and Teacher
. We will learn how to create objects, define properties and methods, and leverage inheritance.
π« Creating the Student Class π«
Let's begin by creating the Student
class, which will represent a student and hold information such as their name, age, and grade. We define the properties using [datatype]
syntax and add a ShowInfo
method to display the student's information.
class Student {
[string]$Name
[int]$Age
[string]$Grade
[void]ShowInfo() {
Write-Host "Student Information:"
Write-Host "Name: $($this.Name)"
Write-Host "Age: $($this.Age)"
Write-Host "Grade: $($this.Grade)"
Write-Host ""
}
}
In the Student
class, we declare three properties: Name
, Age
, and Grade
, representing the student's name, age, and current grade. The ShowInfo
method is used to display the student's information, utilizing the $this
keyword refers to the current instance of the class.
π¨βπ« Creating the Teacher Class π¨βπ«
Next, let's create the Teacher
class, which will represent a teacher and store details such as their name, age, and subject. Similar to the Student
class, we define the properties and include a ShowInfo
method to display the teacher's information.
class Teacher {
[string]$Name
[int]$Age
[string]$Subject
[void]ShowInfo() {
Write-Host "Teacher Information:"
Write-Host "Name: $($this.Name)"
Write-Host "Age: $($this.Age)"
Write-Host "Subject: $($this.Subject)"
Write-Host ""
}
}
In the Teacher
class, we declare the properties Name
, Age
, and Subject
, representing the teacher's name, age, and subject they teach. The ShowInfo
method is responsible for displaying the teacher's information.
π₯ Creating Objects and Displaying Information π₯
Now that we have defined both the Student
and Teacher
classes, let's create objects and utilize their properties and methods. We'll instantiate objects, assign values to their properties, and then call the ShowInfo
method to display the information.
$student = New-Object Student
$student.Name = "John Doe"
$student.Age = 16
$student.Grade = "10th"
$teacher = New-Object Teacher
$teacher.Name = "Jane Smith"
$teacher.Age = 35
$teacher.Subject = "Mathematics"
$student.ShowInfo()
$teacher.ShowInfo()
In the above code, we create a $student
object of the Student
class and assign values to its properties. We set the name to "John Doe", age to 16, and grade to "10th". Similarly, we create a $teacher
object of the Teacher
class and assign values to its properties, such as the name "Jane Smith", age 35
, and subject "Mathematics". Finally, we call the ShowInfo
method on both objects to display their respective information.
π‘ Understanding the Output π‘
When you run the script, you will see the output displaying the information of the student and teacher objects:
Student Information:
Name: John Doe
Age: 16
Grade: 10th
Teacher Information:
Name: Jane Smith
Age: 35
Subject: Mathematics
The output shows the information for the student and teacher objects, demonstrating how their respective ShowInfo
methods are used to display the data stored in their properties.
π Conclusion π
In this post, we explored object-oriented programming in PowerShell using an example of two classes: Student
and Teacher
. We learned how to create objects, define properties and methods, and display information. PowerShell's support for OOP allows for modular and reusable code, enhancing the structure and organization of your scripts and applications. By leveraging the principles of OOP, you can create more robust and maintainable code in PowerShell.