Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
14
Task/Polymorphic-copy/Groovy/polymorphic-copy-1.groovy
Normal file
14
Task/Polymorphic-copy/Groovy/polymorphic-copy-1.groovy
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
class T implements Cloneable {
|
||||
String property
|
||||
String name() { 'T' }
|
||||
T copy() {
|
||||
try { super.clone() }
|
||||
catch(CloneNotSupportedException e) { null }
|
||||
}
|
||||
@Override
|
||||
boolean equals(that) { this.name() == that?.name() && this.property == that?.property }
|
||||
}
|
||||
|
||||
class S extends T {
|
||||
@Override String name() { 'S' }
|
||||
}
|
||||
14
Task/Polymorphic-copy/Groovy/polymorphic-copy-2.groovy
Normal file
14
Task/Polymorphic-copy/Groovy/polymorphic-copy-2.groovy
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
T obj1 = new T(property: 'whatever')
|
||||
S obj2 = new S(property: 'meh')
|
||||
|
||||
def objA = obj1.copy()
|
||||
def objB = obj2.copy()
|
||||
|
||||
assert objA.class == T
|
||||
assert objA == obj1 && ! objA.is(obj1) // same values, not same instance
|
||||
|
||||
assert objB.class == S
|
||||
assert objB == obj2 && ! objB.is(obj2) // same values, not same instance
|
||||
|
||||
println "objA:: name: ${objA.name()}, property: ${objA.property}"
|
||||
println "objB:: name: ${objB.name()}, property: ${objB.property}"
|
||||
Loading…
Add table
Add a link
Reference in a new issue