grails - inheritance of variable in groovy -
maybe late hours :) can 1 tell why parent class pull variables child
foo { public string mystring = "my test string of foo" public printoutstring () { println this.mystring } } bar extends foo { public string mystring = "my test string of bar" } foo.printoutstring() //prints out "my test string of foo" expected bar.printoutstring() //prints out "my test string of foo" not expected thought take string bar instead
there no field inheritance in groovy nor in java. can override value of field, linked question's answer suggest:
class foo { public string mystring = "my test string of foo" public printoutstring () { mystring } } class bar extends foo { { mystring = "my bar" } } assert new foo().printoutstring() == "my test string of foo" assert new bar().printoutstring() == "my bar"
Comments
Post a Comment