This article introduces the basic knowledge of PowerShell object-oriented programming, we know that all the variables and outputs in PowerShell are objects, so it is necessary to understand the basic knowledge of object-oriented programming in PowerShell programming.
The introduction of object-oriented programming is mainly to solve the problems of software complication, as early as the 1960s, Object-oriented has appeared. Although the C language does not support object-oriented features, developers using the C language have already applied object-oriented core ideas to them, so we should be more aware of object-oriented thinking without having to dwell on the language itself.
PowerShell is an object-based scripting language, so the following introduction focuses on introducing basic concepts and does not involve object-oriented programming. By learning from the following knowledge, you will understand the basic object knowledge that PowerShell needs to master, thus profoundly understand the advantages of PowerShell using object-based management.
Classes (Class)
An abstract attribute defined for an object (or object) that includes the characteristics of an object (its attributes, fields, or properties) and the behavior of an object (it can do things, methods, or operations). Some people would say that a class is like a design diagram or factory, used to describe the natural characteristics of something. For example, a dog's class may contain commonalities in all dogs, such as the breed and fur color (which are characteristic of the dog), Bark and sit down (all of which are dogs ' behavior).
Objects (object)
A specific instance of a class (Instance). Is the explanation very abstract? The object can be seen as your dog, or your neighbor's dog. In any case, they are examples of dog classes. A dog defines some of the characteristics of all dogs, such as: three (apparently a dog owner likes to play Mahjong) is a real dog, the information in the dog can be used to describe three different from other dogs, three of the fur is brown. We can know that three are categorized as canine, and are an example of a dog class.
Methods (method)
The ability of the object. Three is a dog, it can be called, so called is the three way. Three may have other methods, such as: in-situ circle, Zuoyi, sit down and so on.
Inheritance (inheritance)
A subclass is a special version of a class that inherits the properties and behavior of the parent class and introduces its own unique properties and behavior.
Let's go back to the previous example where dogs are divided into a variety of species, such as the Golden Shepherd, the Collie and the Chihuahua. Three are examples of the Collie, such as the dog, which already has a method called and attribute fur color. So subclasses of each dog class can inherit this information directly, without needing to redefine the redundant information.
Subclasses can change inherited attributes. For example: The Collie may specify that the default fur color is brown and white. And the Chihuahua subclass may default to specify that the method "called" is to produce a high-pitched cry. Subclasses also allow the introduction of new members, such as a Chihuahua subclass may introduce a method of "trembling" (the Chihuahua is so timid that she trembles at risk). The Chihuahua object also has a trembling method, but three is not, because three is a collie, not a Chihuahua.
For classes and objects, we are more concerned about its members, the properties and methods of the class, when PowerShell development. The classes in PowerShell, like C #, have both static and instance members. Here's a demonstration:
We can list static members of a type by using the Get-member static argument:
Copy Code code as follows:
PS c:\user\splaybow> [int] | get-member-static | Out-string-width 80
TypeName:System.Int32
Name MemberType Definition
---- ---------- ----------
Equals method static System.Boolean equals (object Obja, Object o ...)
Parse method Static System.Int32 Parse (String s), static System ...
ReferenceEquals method Static System.Boolean referenceequals (Object obja,...
TryParse method Static System.Boolean TryParse (String s, int32& re ...)
MaxValue Property Static System.Int32 MaxValue {get;}
MinValue Property Static System.Int32 MinValue {get;}
We can see that there are MaxValue and MinValue two static properties on the System.Int32 that indicate the domain of the System.Int32 type:
Copy Code code as follows:
PS c:\users\splaybow> [Int]::maxvalue
2147483647
PS c:\users\splaybow> [Int]::minvalue
-2147483648
If you do not specify a static argument, Get-member displays an instance method of the object:
Copy Code code as follows:
PS c:\users\splaybow> [Int]::minvalue | Get-member | Out-string-width 80
TypeName:System.Int32
Name MemberType Definition
---- ---------- ----------
CompareTo method System.Int32 CompareTo (Int32 value), System.Int32 Comp ...
Equals method System.Boolean equals (Object obj), System.Boolean Equa ...
GetHashCode method System.Int32 GetHashCode ()
GetType method System.Type GetType ()
GetTypeCode method System.typecode GetTypeCode ()
ToString Method System.String ToString (), System.String ToString (Iform ...
About PowerShell object-oriented programming basics, this article on the introduction of so many, I hope to help you, thank you!