Polymorphic copy: Difference between revisions
Content added Content deleted
(→{{header|Ada}}: Fix modification in "07:25, 9 September 2014 rosettacode>Ker ") |
(PascalABC.NET) |
||
Line 1,668: | Line 1,668: | ||
For immutable data it usually does not make sense to clone it because it does not have an identity beyond its value. |
For immutable data it usually does not make sense to clone it because it does not have an identity beyond its value. |
||
For mutable data types there are clone functions available (<code>Dictionary.clone</code>, <code>Array.clone</code>, <code>BitArray.clone</code>). |
For mutable data types there are clone functions available (<code>Dictionary.clone</code>, <code>Array.clone</code>, <code>BitArray.clone</code>). |
||
=={{header|PascalABC.NET}}== |
|||
<syntaxhighlight lang="delphi"> |
|||
type |
|||
Base = class |
|||
public |
|||
fb: integer; |
|||
constructor (fb: integer) := Self.fb := fb; |
|||
function ToString: string; override := fb.ToString; |
|||
function Clone: Base; virtual := new Base(fb); |
|||
end; |
|||
Derived = class(Base) |
|||
public |
|||
fd: real; |
|||
constructor (fb: integer; fd: real); |
|||
begin |
|||
inherited Create(fb); |
|||
Self.fd := fd; |
|||
end; |
|||
function ToString: string; override := inherited ToString + ',' + fd.ToString; |
|||
function Clone: Base; override := new derived(fb,fd); |
|||
end; |
|||
begin |
|||
var lst := new List<Base>; |
|||
lst.Add(new Base(3)); |
|||
lst.Add(new Derived(4,5.5)); |
|||
lst.Add(new Base(6)); |
|||
lst.Add(new Derived(7,8.8)); |
|||
lst.Println; |
|||
var lst1 := new List<Base>; |
|||
foreach var obj in lst do |
|||
lst1.Add(obj.Clone); |
|||
lst1.Println; |
|||
end. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
3 4,5.5 6 7,8.8 |
|||
3 4,5.5 6 7,8.8 |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |