方法继承的问题
public class Team { public virtual Team Clone2() { Team c = new Team(); c.Time = this.Time; c.RaceName =this.RaceName; c.Name = this.Name; c.RightName = this.RightName; c.Score = this.Score; c.VSName= this.VSName; return c; }} public class ChildrenTeam { public override ChildrenTeam Clone2() { ChildrenTeam c = new ChildrenTeam(); c.Time = this.Time; c.RaceName =this.RaceName; c.Name = this.Name; c.RightName = this.RightName; c.Score = this.Score; c.VSName= this.VSName;//基类特有的变量c.ID=this.ID; return c; }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ class Team { public string TeamField { get; set; } protected virtual void OnClone(Team t) { t.TeamField = TeamField; } public Team Clone2() { Team t = new Team(); OnClone(t); return t; } } class ChildTeam : Team { public string ChildTeamField { get; set; } protected override void OnClone(Team t) { base.OnClone(t); (t as ChildTeam).ChildTeamField = ChildTeamField; } public ChildTeam Clone2() { Team t = new ChildTeam(); OnClone(t); return t as ChildTeam; } } class Program { static void Main(string[] args) { ChildTeam tc = new ChildTeam() { ChildTeamField = "b", TeamField = "a" }; ChildTeam tc2 = tc.Clone2(); Console.WriteLine(tc2 == tc); Console.WriteLine(tc2.TeamField + "," + tc2.ChildTeamField); } }}
[解决办法]