日期:2014-05-17  浏览次数:20771 次

方法继承的问题
C# code
 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;
                       
        }
}


继承类的COLNE是不是写的太麻烦了点,如果有好几个继承类的话,每个都得改,这样“ ChildrenTeam c = base.Clone2() as ChildrenTeam ; c._ID=this.ID”貌似可以解决,但面临装箱拆箱的效率问题,大家有没有更好或更优雅的写法?

------解决方案--------------------
ChildrenTeam c = base.Clone2();
c.xxx = xxx
...
return c;
------解决方案--------------------
另外忍不住还是要说你,什么“装箱拆箱”还效率问题,不要听了几个名词就乱用,根本挨不上。

装箱拆箱和类型转换是两码事。
------解决方案--------------------
晕,你这个根本没有重写
public override ChildrenTeam Clone2()
=〉
public override Team Clone2()

------解决方案--------------------
主要是基类中
 Team c = new Team();
一句写死了,导致基类的Clone2只能返回纯的 Team 类型的实例,而不可能转换为各种子类。这里,哪怕是用一个类似
protected virtual Team CreatInstance() { return new Team(); }
的函数,保证子类可以通过重写该函数以正确初始化子类实例也好过把语句写死。
------解决方案--------------------
你这个用NVI模式(Non-Virtual Interface)就可以了:
C# code
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);
        }
    }
}

------解决方案--------------------
探讨
楼上的方法不错,但是有一点,有的继承类,有可能没有CLONE2,只需要运行基类的CLONE2就可以了

------解决方案--------------------
探讨

你这个用NVI模式(Non-Virtual Interface)就可以了:
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Team
{
pub……