抽象类和继承类中的接口

问题描述 投票:0回答:1

例如,我有一个具有IEquatable接口的抽象类。基于此类的属性,我想自己实现Equals方法(因为继承的类将通过这些基本属性检查是否相等):

abstract class Site : IEquatable<Site>
    {
        public string Name { get; set; }
        public string Adress { get; set; }
        public int Year { get; set; }
        public Site() { }
        public Site(string name, string adress, int year)
        {
            Name = name;
            Adress = adress;
            Year = year;
        }

        public abstract bool CheckIfNew();

        public override bool Equals(object other)
        {
            if(ReferenceEquals(null, other))
            {
                return false;
            }
            if(ReferenceEquals(this, other))
            {
                return true;
            }
            return GetType() == other.GetType() && Equals((Site)other);
        }

        public bool Equals(Site other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }
            if (ReferenceEquals(this, other))
            {
                return true;
            }
            return other.Name == Name && other.Adress == Adress;
        }

        public override int GetHashCode()
        {
            return Name.GetHashCode() ^ Adress.GetHashCode();
        }
    }

假设我有两个继承的类。它们都具有IComparable接口,用于将它们排序在列表中。这是其中之一:

class Museum : Site, IComparable<Museum>
    {
        public string Type { get; set; }
        private int[] WorkDays { get; set; }
        public bool HasGuide { get; set; }
        public decimal TicketPrice { get; set; }

        public Museum(string name, string adress, int year, string type, int[] workDays, bool hasGuide, decimal ticketPrice)
            : base(name, adress, year)
        {
            Type = type;
            WorkDays = workDays;
            HasGuide = hasGuide;
            TicketPrice = ticketPrice;
        }

        public bool CheckAvailability(int weekDay)
        {
            return WorkDays[weekDay - 1] == 1;
        }
        public override bool CheckIfNew()
        {
            DateTime curent = DateTime.Now;
            int difference = curent.Year * 12 + curent.Month - Year * 12;
            return difference < 24;
        }
        public bool Equals(Museum other)
        {
            return base.Equals(other);
        }

        public override string ToString()
        {
            return base.ToString() + string.Format($" {Type, -20} | {(HasGuide ? "Taip" : "Ne"), -5} | {TicketPrice, 6}");
        }


        public int CompareTo(Museum other)
        {
            return TicketPrice > other.TicketPrice ? 1 : (TicketPrice < other.TicketPrice ? -1 : 0);
        }
    }

此继承的类现在同时具有IEquatableIComparable接口吗?如果仅通过基类属性检查相等性,是否应该在继承的类中以不同的方式实现Equals方法?我应该在基类中添加IComparable接口并使CompareTo方法抽象吗?例如,如果我要将这些继承的类型存储在仅接受具有这两个接口的类型的自定义列表中,那么实现这些接口的最佳方法是什么?我可以选择一种或另一种方法,但我认为它仍然可以工作,但是这样做的正确和安全方法是什么?

c# inheritance methods interface abstract-class
1个回答
1
投票

[抽象类和接口之间有一些细微的差别,但有一个很大的差别:

Abstract classes在继承链中。这使其成为主要的[[exclusive目的。窗口或窗体也不能是DBConnection

接口不在继承链中。

关于无法正式声明字段的限制已通过属性无效。如果没有将其完全删除,则将其删除。

不提供功能实现的限制为removed with C# 8.0

一个“聚集”接口的抽象类很常见。

如果仅通过基类属性检查相等性,是否应该在继承的类中以不同的方式实现Equals方法?

您仍然应该覆盖它。如果它不添加任何更改等式,则实现很简单:

public override bool Equals(Museum other) { return base.Equals(other); }

如果有,您可以添加该实现。

1

© www.soinside.com 2019 - 2024. All rights reserved.