简体   繁体   中英

C# Object Orientated Inheritence Theory

All,

I have the following design logic:

Parent (or base?) class: Person
Passenger inherits from Person
Employee inherits from Person

I am planning to use the following structure with both the Passenger and Employee classes:

        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.

Second edit: Following Ekke's question:

My code is as follows:

Person 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:

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? 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?

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. I'm teaching myself c# and trying not to get into bad habits.

Thanks

The fix is to add the public access modifier to the LocationList properties so they can be accessed outside that class/struct. Without this, the default is private which means they are only accessible within that type.

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 and struct members, including nested classes and structs, have private access by default. 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;
    };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM