What is about such concepts as Class, Interface, Mixin in PowerShell? Does it support OOP? If so, where can I read about this?
相关问题
- how to define constructor for Python's new Nam
- Keeping track of variable instances
- How to Debug/Register a Permanent WMI Event Which
- Object.create() bug?
- How can I do variable substitution in a here-strin
相关文章
- 在vscode如何用code runner打开独立的控制台窗口,以及设置好调试模式时窗口的编码?
- C#调用PowerShell的问题
- 接口B继承接口A,但是又不添加新的方法。这样有什么意义吗?
- EscapeDataString having differing behaviour betwee
- NameError: name 'self' is not defined, eve
- PowerShell Change owner of files and folders
- Implementation Strategies for Object Orientation
- Check if the Type of an Object is inherited from a
Version 5 of Powershell seems to support some of mainstream OOP.
All credit goes to this guy: https://xainey.github.io/2016/powershell-classes-and-concepts/
Example of a class:
Example of an abstract class:
The PowerShell pipeline deals with objects, not just a text stream a a Unix pipeline does. All variables are instances of objects as well. These are all .NET objects, BTW.
Here's part of the output of an "ls" command piped to the get-member cmdlet:
get-member displays the members of the object you pipe to it. You can see that these are the actual members of the System.IO.DirectoryInfo class.
You can define new types in PowerShell v2.0 using the
Add-Type
cmdlet:help Add-Type
for more information.Also, see:
How do I create a custom type in PowerShell for my scripts to use?
http://thepowershellguy.com/blogs/posh/archive/2008/06/02/powershell-v2-ctp2-making-custom-enums-using-add-type.aspx
PowerShell is more of an OOP consumer language. It can utilize most of the .NET Framework but it doesn't natively support creating interfaces, classes and certainly not mixins. .NET, which PowerShell's type system is based upon, doesn't support mixins. PowerShell does support dynamic addition of properties and methods to an existing object via the Add-Member cmdlet.
Add-Type is useful but if you have to escape to C# or VB to define a class or a class that implements a particular interface, I wouldn't consider that first class support the creation of classes/interfaces.
If you looking for some free learning material, check out Effective Windows PowerShell.