iNNOKENTIY21
Silver Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору
Код: class Edition { [string] $Name [int] $PageCount [void] Print() { Write-Host ("{0} ({1} стр.)" -f $this.Name, $this.PageCount) } } class Magazine : Edition { [string] $Periodic [void] Print() { Write-Host ("[{0}] {1} ({2} стр.)" -f $this.Periodic, $this.Name, $this.PageCount) } } class Book : Edition { [string] $Author [void] Print() { Write-Host ("[{0}] {1} ({2} стр.)" -f $this.Author, $this.Name, $this.PageCount) } } class Publishing { [string] $Name [Edition[]] $Editions = @() [void] Add([Edition] $Edition) { $this.Editions += $Edition } [void] SaveToJson([string] $Path) { [PSCustomObject[]] $Result = @() foreach ($Item in $this.Editions) { $Result += @{ Type = [string] $Item.GetType() Edition = $Item } } $Result | ConvertTo-Json | Out-File -LiteralPath $Path } [void] LoadFromJson([string] $Path) { [Edition[]] $Result = @() $Content = Get-Content -LiteralPath $Path $Json = $Content | ConvertFrom-Json foreach ($Item in $Json) { $Result += $Item.Edition -as ($item.Type) } $this.Editions = $Result } } "Создаётся издательство" $Publishing = [Publishing]::new() $Publishing.Name = "Рога и копыта" $Publishing | Format-List "" "Задаётся путь к файлу данных" $Path = "$PSScriptRoot\Editions.json" $Path "" "Создаётся журнал" $SportMagazine = [Magazine]@{ Periodic = "Weekly" Name = "Sport Magazine" PageCount = 100 } $SportMagazine | Format-Table "" "Создаётся книга" $ВойнаИМир = [book]@{ Name = "Война и мир" PageCount = 100500 Author = "Лев Николаевич Толстой" } $ВойнаИМир | Format-Table "" "Журнал и книга добаляются к изданиям издательства" $Publishing.Add($SportMagazine) $Publishing.Add($ВойнаИМир) $Publishing | Format-List "" "Сохраняется в файл: $Path" $Publishing.SaveToJson($Path) "" "Обнуляются издания у издательства" $Publishing.Editions = @() $Publishing | Format-List "" "Пауза две секунды" Start-Sleep -Seconds 2 "" "Загружаются данные из файла: $Path" $Publishing.LoadFromJson($Path) $Publishing | Format-List "" "Вывод данных (метод Print()):" $Publishing.Editions.Print() |
|