Greet!
I recently trying to learn the class from Microsoft
when i learning the chapter
class Book {
# Class properties
[string] $Title
[string] $Author
[string] $Synopsis
[string] $Publisher
[datetime] $PublishDate
[int] $PageCount
[string[]] $Tags
# Default constructor
Book() { $this.Init(@{}) }
# Convenience constructor from hashtable
Book([hashtable]$Properties) { $this.Init($Properties) }
# Common constructor for title and author
Book([string]$Title, [string]$Author) {
$this.Init(@{Title = $Title; Author = $Author })
}
# Shared initializer method
[void] Init([hashtable]$Properties) {
foreach ($Property in $Properties.Keys) {
$this.$Property = $Properties.$Property
}
}
# Method to calculate reading time as 2 minutes per page
[timespan] GetReadingTime() {
if ($this.PageCount -le 0) {
throw 'Unable to determine reading time from page count.'
}
$Minutes = $this.PageCount * 2
return [timespan]::new(0, $Minutes, 0)
}
# Method to calculate how long ago a book was published
[timespan] GetPublishedAge() {
if (
$null -eq $this.PublishDate -or
$this.PublishDate -eq [datetime]::MinValue
) { throw 'PublishDate not defined' }
return (Get-Date) - $this.PublishDate
}
# Method to return a string representation of the book
[string] ToString() {
return "$($this.Title) by $($this.Author) ($($this.PublishDate.Year))"
}
}
i try to write my own code over the example
class map {
[int]$x1
[int]$y1
[int]$z1
[string]$name
# Default constructor
map() { $this.Init(@{}) }
# Convenience constructor from hashtable
map(
[hashtable]$Properties) { $this.Init($Properties) }
# Common constructor for title and author
map([int]$X1, [int]$Z1) {
$this.Init(@{ })
}
# Shared initializer method
[void] Init([hashtable]$Properties) {
foreach ($Property in $Properties.Keys) {
$this.$Property = $Properties.$Property
}
}
}
$map= [System.Collections.Generic.List[map]]$map
class newplace {
[int]$x1
[int]$y1
[int]$z1
[string]$name
newplace (){
$this.x1=123
$this.y1=123
$this.z1=123
$this.name = 'test'}
}
$newplace =[newplace]::new()
$map.add($newplace)
but i get the error of
MethodException: Cannot find an overload for "add" and the argument count: "1".
i know there is sth wrong with the overloads in my code ,but i didn’t really understand why