簡體   English   中英

C# Object 定向繼承論

[英]C# Object Orientated Inheritence Theory

全部,

我有以下設計邏輯:

父(或基?)class:人
乘客從 Person 繼承
員工從人繼承

我計划在Passenger 和Employee 類中使用以下結構:

        public struct LocationList
        {
            Location LocationName;
            DateTime RequiredArrivalTime;
            DateTime ActualArrivalTime;
        };

我認為結構的邏輯位置將在 Person class 但 c# 不允許 inheritance 結構。

第二次編輯:遵循 Ekke 的問題:

我的代碼如下:

人 class:

using System;
using System.Collections.Generic;
using System.Text;

namespace Airport_Server
{
    public class Person
    {
        public struct LocationList
        {
            Location LocationName;
            DateTime RequiredArrivalTime;
            DateTime ActualArrivalTime;
        };
    }
}

乘客 class:

using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Drawing.Imaging;
using System.Text;

namespace Airport_Server
{
    public class Passenger : Person
    {
        private float Cash;
        public DateTime FinishAtCurrentLocation;
        public LocationAction CurrentLocationAction;
        public int PassengerID;
        private static int NextPassengerNo;
        public List<LocationList> AreaTimePlan;
        protected Location CurrentArea;
        private bool bolNextArea;
        private DateTime NextAreaTime;

        public DateTime GetNextAreaTime()
        {
            foreach (LocationList InvLocation in AreaTimePlan)
            { 
                if (InvLocation.LocationName.Title==CurrentArea.Title) << Error 'Person.LocationList.LocationName is inaccessible due to it's protection level
                {
                    bolNextArea = true;

                }
                if (InvLocation.LocationName.Title != CurrentArea.Title && bolNextArea=true)
                {
                    NextAreaTime = InvLocation.RequiredArrivalTime;
                    bolNextArea = false;
                }
            }
            return NextAreaTime;

        }
    }
}

第一次編輯:段落添加了以下評論:

我打算使用結構而不是單個變量的原因是因為我將列出它們。 除非有我不知道的選項; 如果我使用單個變量,那么我需要幾個列表,這似乎是一個更復雜的選項?

我可以通過將 LocationList 結構變成 class 來解決它,但我不確定這是否是最好的方法? 似乎對它自己的 class 的要求很小,所以我想知道我是否錯過了一個選項或一個 object 導向的設計概念?

我可以將結構放在乘客和員工中,但我試圖避免重復。

我使用 Java 學習了面向 object 的設計,那是很久以前的事了。 我正在自學 c# 並盡量不養成壞習慣。

謝謝

解決方法是將public訪問修飾符添加到LocationList屬性中,以便可以在該類/結構之外訪問它們。 沒有這個,默認是private的,這意味着它們只能在該類型中訪問。

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers

結構成員,包括嵌套類和結構,可以聲明為公共的、內部的或私有的。 Class 成員(包括嵌套類和結構)可以是公共的、受保護的內部、受保護的、內部的、私有的受保護的或私有的。 Class 和結構成員,包括嵌套類和結構,默認情況下具有私有訪問權限。 不能從包含類型外部訪問私有嵌套類型。

public class Person
{
    public struct LocationList
    {
        public Location LocationName;
        public DateTime RequiredArrivalTime;
        public DateTime ActualArrivalTime;
    };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM