简体   繁体   English

C# Object 定向继承论

[英]C# Object Orientated Inheritence Theory

All,全部,

I have the following design logic:我有以下设计逻辑:

Parent (or base?) class: Person父(或基?)class:人
Passenger inherits from Person乘客从 Person 继承
Employee inherits from Person员工从人继承

I am planning to use the following structure with both the Passenger and Employee classes:我计划在Passenger 和Employee 类中使用以下结构:

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

I thought the logical place for the structure would be in the Person class but c# doesn't allow inheritance of structures.我认为结构的逻辑位置将在 Person class 但 c# 不允许 inheritance 结构。

Second edit: Following Ekke's question:第二次编辑:遵循 Ekke 的问题:

My code is as follows:我的代码如下:

Person class:人 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;
        };
    }
}

Passenger class:乘客 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;

        }
    }
}

First edit: Paragraph added following comment:第一次编辑:段落添加了以下评论:

My reason for intending to use a structure, instead of individual variables, is because I will have a list of them.我打算使用结构而不是单个变量的原因是因为我将列出它们。 Unless there is an option I'm unaware of;除非有我不知道的选项; if I used individual variables then I would need several lists which seems a more complicated option?如果我使用单个变量,那么我需要几个列表,这似乎是一个更复杂的选项?

I could solve it by making the LocationList structure into a class but I am not sure if that is the best way?我可以通过将 LocationList 结构变成 class 来解决它,但我不确定这是否是最好的方法? It seems like a small requirement for it's own class so I'm wondering if I've missed an option or an object orientated design concept?似乎对它自己的 class 的要求很小,所以我想知道我是否错过了一个选项或一个 object 导向的设计概念?

I could put the structure in Passenger and Employee but I'm trying to avoid duplication.我可以将结构放在乘客和员工中,但我试图避免重复。

I learnt object orientated design using Java and it was quite a while ago.我使用 Java 学习了面向 object 的设计,那是很久以前的事了。 I'm teaching myself c# and trying not to get into bad habits.我正在自学 c# 并尽量不养成坏习惯。

Thanks谢谢

The fix is to add the public access modifier to the LocationList properties so they can be accessed outside that class/struct.解决方法是将public访问修饰符添加到LocationList属性中,以便可以在该类/结构之外访问它们。 Without this, the default is private which means they are only accessible within that type.没有这个,默认是private的,这意味着它们只能在该类型中访问。

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

Struct members, including nested classes and structs, can be declared public, internal, or private.结构成员,包括嵌套类和结构,可以声明为公共的、内部的或私有的。 Class members, including nested classes and structs, can be public, protected internal, protected, internal, private protected, or private. Class 成员(包括嵌套类和结构)可以是公共的、受保护的内部、受保护的、内部的、私有的受保护的或私有的。 Class and struct members, including nested classes and structs, have private access by default. Class 和结构成员,包括嵌套类和结构,默认情况下具有私有访问权限。 Private nested types aren't accessible from outside the containing type.不能从包含类型外部访问私有嵌套类型。

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