简体   繁体   English

抽象类和继承类中的接口

[英]Interfaces in abstract and inherited classes

For example I have an abstract class which has the IEquatable interface.例如,我有一个抽象的 class ,它具有IEquatable接口。 Based on the properties of this class I want to implement the Equals method in itself (because inherited classes will check equality by these base properties):基于此 class 的属性,我想自己实现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();
        }
    }

Suppose I have two inherited classes.假设我有两个继承的类。 They will both have IComparable interfaces for sorting them in a list.它们都有IComparable接口,用于在列表中对它们进行排序。 This is one of them:这是其中之一:

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);
        }
    }

Does this inherited class now have both IEquatable and IComparable interfaces?这个继承的 class 现在是否同时具有IEquatableIComparable接口? Should I implement Equals method differently in inherited class if I'm checking for equality only by base class properties?如果我仅通过基本 class 属性检查相等性,我是否应该在继承的 class 中以不同的方式实现Equals方法? Should I add IComparable interface to the base class and make the CompareTo method abstract?我应该将IComparable接口添加到基础 class 并使CompareTo方法抽象吗? If, for example, I would like to store these inherited types in a custom list that only accepts types with these two interfaces, what would be the best way to implement these interfaces?例如,如果我想将这些继承的类型存储在一个只接受具有这两个接口的类型的自定义列表中,那么实现这些接口的最佳方法是什么? I could go one way or another and it would still, in my opinion, work, but what would be the right and safe way to do this?我可以以一种或另一种方式 go 并且在我看来它仍然可以工作,但是正确和安全的方法是什么?

There is a number of minor differences between Abstract Classes and Interfaces, but one big difference:抽象类和接口之间有许多细微的区别,但有一个很大的区别:

Abstract classes are in the inheritance chain.抽象类在 inheritance 链中。 This makes it a primary, exclusive purpose.这使它成为一个主要的、专有的目的。 A Window or Form can not also be a DBConnection A Window 或 Form 不能也是 DBConnection

Interfaces are not in the inheritance chain.接口不在 inheritance 链中。

The limit about not beign able to declare fields, was invalidated with Properties.关于不能声明字段的限制,已通过属性失效。 If it was no completely removed otherwise.如果没有完全删除,否则。

The limit of not providing Function Implementations, was removed with C# 8.0不提供 Function 实现的限制,在C# 8.0 中被移除

A abstract class that "gathers" interfaces is a very common sight. “收集”接口的抽象 class 很常见。

Should I implement Equals method differently in inherited class if I'm checking for equality only by base class properties?如果我仅通过基本 class 属性检查相等性,我是否应该在继承的 class 中以不同的方式实现 Equals 方法?

You should override it anyway.无论如何,您都应该覆盖它。 If it adds nothing to change equals, the implemenation is simple:如果它没有添加任何内容来更改 equals,则实现很简单:

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

If it does, you you can add that implementaiton.如果是这样,您可以添加该实现。

1 : 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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