Created
January 31, 2022 15:15
-
-
Save ynwd/f0182ad2e564d410bd344e1b78555996 to your computer and use it in GitHub Desktop.
SOLID: prinsip liskov
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type A struct{} | |
func (a A) Test() { | |
fmt.Println("Printing A") | |
} | |
type B struct { | |
A | |
} | |
func ImpossibleLiskovSubstitution(a A) { | |
a.Test() | |
} | |
func main() { | |
a := B{} | |
// PANIC : cannot use a (type B) as | |
// type A in argument to | |
// ImpossibleLiskovSubstitution | |
ImpossibleLiskovSubstitution(a) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type A struct{} | |
func (a A) Test() { | |
fmt.Println("Printing A") | |
} | |
type B struct { | |
A | |
} | |
type testing interface { | |
Test() | |
} | |
func PossibleLiskovSubstitution(a testing) { | |
a.Test() | |
} | |
func main() { | |
a := B{} | |
PossibleLiskovSubstitution(a) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment