In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the difference between C# interface, abstract class, abstract method and virtual method? I believe many inexperienced people don't know what to do. therefore, this paper summarizes the causes and solutions of the problem. I hope you can solve this problem through this article.
C# abstract class and interface children have great similarity in supporting abstract class definition, and can even be replaced with each other, so many developers are more casual about the choice of abstract class and interface when defining abstract class. In fact, there is a big difference between the two.
First, let's talk about the similarities and differences between interfaces and abstract classes:
Similarities:
1. None of them can instantiate themselves, that is to say, they are used to be inherited.
two。 Abstract methods in abstract classes, like interface methods, cannot have a method body.
Differences:
1. There can be fields in an abstract class and no fields in an interface.
two。 Ordinary methods in abstract classes can have method bodies, while interface methods do not have method bodies.
3. Methods in interfaces cannot have access modifiers, abstract methods in abstract classes are inherited and need to be overridden by override.
4. Interface is more like a specification, a constraint.
5. A subclass can inherit only one abstract class, but it can inherit multiple interfaces.
Then there are the similarities and differences between abstract methods and virtual methods:
In fact, you can think of an abstract method as a virtual method without a method body.
Similarities:
1. Both abstract and virtual methods can be overridden.
Differences:
1.abstract and virtual have different keywords
two。 Abstract methods must have no method body, while virtual methods can have method bodies.
Here is an example to illustrate:
/ / abstract class of a mobile phone / public abstract class Mobilephone {private string _ logo; public string Logo {get {return _ logo;} set {_ logo = value;}} public abstract void Call (); public virtual void Callagain () {Console.WriteLine ("I'll call again") } / define an entity class that inherits an abstract class and multiple interfaces / public class Nokia: Mobilephone,IClock,Ilistenmusic {public override void Call () {Console.WriteLine ("NOKIA calls are OK");} public void clock () {Console.WriteLine ("NOKIA phone also has alarm clock function");} public void listenmusic () {Console.WriteLine ("NOKIA phone can also listen to music") } public override void Callagain () {Console.WriteLine ("forget it, let's not play");} / two interfaces are defined below, in order to show that multiple interfaces / public interface Ilistenmusic {void listenmusic ();} public interface IClock {void clock ();}
To add a small example.
Here are interesting interview questions about the difference between the new keyword and override:
Class Program {static void Main (string [] args) {B5 b5 = new B5 (); b5.MethodB ();}} public class A5 {public virtual void MethodA () {Console.WriteLine ("A5.MethodA"); Console.Read ();} public virtual void MethodB () {MethodA ();}} public class B5: A5 {public new void MethodA () {Console.WriteLine ("B5.MethodA") Console.ReadLine ();}} class Program {static void Main (string [] args) {B5 b5 = new B5 (); b5.MethodB ();}} public class A5 {public virtual void MethodA () {Console.WriteLine ("A5.MethodA"); Console.Read ();} public virtual void MethodB () {MethodA () }} public class B5: A5 {public new void MethodA () {Console.WriteLine ("B5.MethodA"); Console.ReadLine ();}}
The final answer is: "A5.MethodA"
After reading the above, have you mastered the method of what is the difference between C# interface, abstract class, abstract method and virtual method? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.