[.NET]簡易抽象類別繼承範例

  • 190
  • 0

[.NET]簡易抽象類別繼承範例

Base Class:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using WorldCupCrawler.Model;
using log4net.Config;
using System.Data.Entity.Validation;

namespace WorldCupCrawler.Crawler
{
    public abstract class BaseCrawler
    {
		//要讓繼承的直接存取的話就宣告為protected
        protected readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        protected IWebDriver driver;

        protected Common.CrawlerType CrawlerType;

        protected string Url = "";

        protected int Year = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings["Year"]);

        protected BaseCrawler(string url, Common.CrawlerType crawlerType)
        {
            XmlConfigurator.Configure(new System.IO.FileInfo("./log4net.config"));
            this.Url = url.Replace("amp;","").Replace("{Year}",Year.ToString());//自動取代年度
            this.CrawlerType = crawlerType;


            //初始化chrome selenium
            //隱藏chromedriver.exe視窗
            ChromeOptions chromeBrowserOptions = new ChromeOptions();
            var driverService = ChromeDriverService.CreateDefaultService(@"ChromeDriver");
            driverService.HideCommandPromptWindow = true;
            //chromeBrowserOptions.AddArguments(new List<string>() { "no-sandbox", "headless", "disable-gpu", "window-size=1904x1500" });
            driver = new ChromeDriver(driverService, chromeBrowserOptions);
            Common.WriteLog("成功初始化Selenium," + crawlerType.ToString() + 
                "即將開始爬網!", crawlerType);
        }
		//要讓繼承的複寫的,就宣告為public abstract
        public abstract void Start();
        
        

    }
}


繼承的Class:
 

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using WorldCupCrawler.Model;
using log4net.Config;
using System.Data.Entity.Validation;

namespace WorldCupCrawler.Crawler
{
    public class GroupStageBackupCrawler : BaseCrawler
    {
        //帶參數到BaseClass的寫法是  : base()
        public GroupStageBackupCrawler()
            : base(System.Configuration.ConfigurationManager.AppSettings["GroupStageBackupURL"]
            , Common.CrawlerType.GroupStageBackupCrawler)
        {
			
        }
		
		//abstract class要複寫的關鍵字是override
        public override void Start()
        {
            // do anything you want
        }

    }
}

參考資料:
abstract (C# 參考)
https://docs.microsoft.com/zh-tw/dotnet/csharp/language-reference/keywords/abstract
Abstract constructor in C# [duplicate]
https://stackoverflow.com/questions/2299037/abstract-constructor-in-c-sharp?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa