繁体   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