C # basic series-inheritance 1 (override and overload)

Source: Internet
Author: User

Inheritance is essential to object-oriented systems. There seems to be nothing to say about inheritance. Everyone knows, isn't it just that a son can have his father's attributes and methods ~~~

Yes ~ Inheritance is the attributes and methods for the son to have his father. In C #, only single-inheritance classes and multi-continuation interfaces are supported. I don't want to talk nonsense either. If you are interested, find the interface injection.

During many interviews, we generally encounter the following two problems:

1. How do you choose abstract classes and interfaces?

This benevolent person sees wise and wise. I cannot say that I have to like the preferred interface.

2. What is the difference between override and overload?

This article focuses on this point. Before we begin, we will first list keywords: Virtual, abstract, and override (there is no keyword for overload)

Abstract modifier class is an abstract class and cannot be case-based. When modifying a method, it is an abstract method (the class must be an abstract class) and must be overwritten.

Virtual can only be modified and declared as virtual. It can be implemented if you want to implement it.

Explanation of override in msdn

The override method provides a new implementation of Members inherited from the base class. The method declared by override is called the override base method. The override base method must have the same signature as the override method. For inheritance information, see inheritance (C # programming guide ). You cannot override non-virtual or static methods. The override base method must be virtual, abstract, or override. Override declares that the accessibility of the virtual method cannot be changed. The override and virtual methods must have the same access level modifier. You cannot use the new, static, or virtual modifier to modify the override method.

From msdn, we can clearly draw several conclusions:

1. If the parent class has abstract, the method of the same name of the subclass must have override.

2. If the parent class has a virtual method, the method with the same name of the subclass is not necessarily override, probably overload.

3. Override must have a parent-child relationship. You can use blue to win Blue. Son surpassed father.

 

Overload does not have this keyword in msdn, but it should be said that it is a polymorphism among the three main features of the class. (If there is a keyword, I think it should be new. In many cases, we can save it. This statement is not rigorous.) Operator must be mentioned in object-oriented systems. This is an example of overload. But this is special. It must be a static method. And it cannot be new as I mentioned above. Therefore, this statement is not rigorous.

 

Overload overload is a manifestation of polymorphism in a class.

When a subclass object uses this method, it calls the definition in the subclass. For it, the definition in the parent class is "blocked.

I did not pay attention to these few words when I deleted them. I didn't delete them. Thanks 3 # madfrag's reminder.

If multiple methods with the same name are defined in a class, they may have different numbers of parameters or different parameter types, it is called method overload ). The overload method can change the type of the returned value.

  

I have seen this statement in Java, but it also applies to C #.

There is a problem with the content of the strikethrough. So I opened a new article-inherit 2

 

Expand it as needed. This operator overload makes many friends think that string is a value type, not a reference type. It is actually a reference type. It just reloads the operator and makes it look like a value type.

I have attached a small example that contains the content I mentioned above ~

    public abstract class Ba    {        public virtual void Test() { }        public virtual int Property { get; set; }        public abstract string Nature { get; set; }            }    public class Sun : Ba    {        protected void Test()        {        }        public override int Property        {            get            {                return base.Property;            }            set            {                base.Property = value;            }        }        public override string Nature        {            get            {                throw new NotImplementedException();            }            set            {                throw new NotImplementedException();            }        }        public static Sun operator +(Sun b1, Sun b2)        {            return null;        }    }

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.