日常小問題

小問題之未提供與。。。。。。的必需形參“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();
        }
    }
}

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