日常小问题

小问题之未提供与。。。。。。的必需形参“id”对应的实参

抽象原型类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrototypeModel
{
    abstract class Prototype
    {
        private string id;
        
        public Prototype(string id)
        {
            this.id = id;
        }
        public string Id
        {
            get { return id; }
        }
        public abstract Prototype Clone();
    }
}

具体原型类

在这里插入图片描述

改正之后

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrototypeModel
{
    abstract class Prototype
    {
        private string id;
        public Prototype()
        {

        }
        public Prototype(string id)
        {
            this.id = id;
        }
        public string Id
        {
            get { return id; }
        }
        public abstract Prototype Clone();
    }
}

可以看到原型类增加了一个无参构造函数 具体原因:当具体原型类继承原型类的时候 会先调用父类的构造器 但是父类的默认构造器已经被一个有参数的覆盖了 因此会调用父类有参的构造器 这个时候应该给父类构造器的传入实参 或者像上面一样增加一个父类的无参构造器。

给父类传入实参如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrototypeModel
{
    class ConcretePrototype1 : Prototype
    {

        public ConcretePrototype1(string id):base(id)
        {

        }
        public override Prototype Clone()
        {
            throw new NotImplementedException();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章