Created
May 22, 2024 04:09
-
-
Save artemkoloskov/195e27c5aab6009709d243135d0a0a60 to your computer and use it in GitHub Desktop.
Interfaces and abstracts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Newtonsoft.Json; | |
| public interface IInterf | |
| { | |
| string PropFromInterfaceAbstract { get; set; } | |
| string MethFromInterfVirtual(); | |
| T FromJson<T>(string json); | |
| string ToJson(); | |
| } | |
| public abstract class Abstr : IInterf | |
| { | |
| public abstract string PropFromInterfaceAbstract { get; set; } | |
| public virtual string MethFromInterfVirtual() | |
| { | |
| return $"calling {nameof(MethFromInterfVirtual)} from {nameof(Abstr)}"; | |
| } | |
| public abstract string MethFromAbstrAbstr(); | |
| public virtual string MethFromAbstrVirt() | |
| { | |
| return $"calling {nameof(MethFromAbstrVirt)} from {nameof(Abstr)}"; | |
| } | |
| public virtual T FromJson<T>(string json) | |
| { | |
| Console.WriteLine($"calling {nameof(FromJson)} from {nameof(Abstr)}"); | |
| return JsonConvert.DeserializeObject<T>(json); | |
| } | |
| public virtual string ToJson() | |
| { | |
| Console.WriteLine($"calling {nameof(ToJson)} from {nameof(Abstr)}"); | |
| return JsonConvert.SerializeObject(this, Formatting.Indented); | |
| } | |
| } | |
| public class First : Abstr | |
| { | |
| public override string PropFromInterfaceAbstract { get { return $"calling {nameof(PropFromInterfaceAbstract)} from {nameof(First)}"; } set { _ = value; } } | |
| public override string MethFromAbstrAbstr() | |
| { | |
| return $"calling {nameof(MethFromAbstrAbstr)} from {nameof(First)}"; | |
| } | |
| public override string MethFromAbstrVirt() | |
| { | |
| return $"calling {nameof(MethFromAbstrVirt)} from {nameof(First)}"; | |
| } | |
| public override T FromJson<T>(string json) | |
| { | |
| Console.WriteLine($"calling {nameof(FromJson)} from {nameof(First)}"); | |
| return JsonConvert.DeserializeObject<T>(json); | |
| } | |
| } | |
| public class Second : Abstr | |
| { | |
| public override string PropFromInterfaceAbstract { get { return $"calling {nameof(PropFromInterfaceAbstract)} from {nameof(Second)}"; } set { _ = value; } } | |
| public override string MethFromInterfVirtual() | |
| { | |
| return $"calling {nameof(MethFromInterfVirtual)} from {nameof(Second)}"; | |
| } | |
| public override string MethFromAbstrAbstr() | |
| { | |
| return $"calling {nameof(MethFromAbstrAbstr)} from {nameof(Second)}"; | |
| } | |
| public override string ToJson() | |
| { | |
| Console.WriteLine($"calling {nameof(ToJson)} from {nameof(Second)}"); | |
| return JsonConvert.SerializeObject(this, Formatting.Indented); | |
| } | |
| } | |
| IInterf obj1 = new First(); | |
| IInterf obj2 = new Second(); | |
| Console.WriteLine(obj1.PropFromInterfaceAbstract); | |
| Console.WriteLine(obj1.MethFromInterfVirtual()); | |
| Console.WriteLine(((Abstr)obj1).PropFromInterfaceAbstract); | |
| Console.WriteLine(((Abstr)obj1).MethFromInterfVirtual()); | |
| Console.WriteLine(((Abstr)obj1).MethFromAbstrAbstr()); | |
| Console.WriteLine(((Abstr)obj1).MethFromAbstrVirt()); | |
| Console.WriteLine(((First)obj1).PropFromInterfaceAbstract); | |
| Console.WriteLine(((First)obj1).MethFromInterfVirtual()); | |
| Console.WriteLine(((First)obj1).MethFromAbstrAbstr()); | |
| Console.WriteLine(((First)obj1).MethFromAbstrVirt()); | |
| Console.WriteLine(); | |
| Console.WriteLine(obj2.PropFromInterfaceAbstract); | |
| Console.WriteLine(obj2.MethFromInterfVirtual()); | |
| Console.WriteLine(((Abstr)obj2).PropFromInterfaceAbstract); | |
| Console.WriteLine(((Abstr)obj2).MethFromInterfVirtual()); | |
| Console.WriteLine(((Abstr)obj2).MethFromAbstrAbstr()); | |
| Console.WriteLine(((Abstr)obj2).MethFromAbstrVirt()); | |
| Console.WriteLine(((Second)obj2).PropFromInterfaceAbstract); | |
| Console.WriteLine(((Second)obj2).MethFromInterfVirtual()); | |
| Console.WriteLine(((Second)obj2).MethFromAbstrAbstr()); | |
| Console.WriteLine(((Second)obj2).MethFromAbstrVirt()); | |
| var json1 = obj1.ToJson(); | |
| Console.WriteLine(json1); | |
| var json2 = obj2.ToJson(); | |
| Console.WriteLine(json2); | |
| IInterf obj3 = obj1.FromJson<First>(json1); | |
| IInterf obj4 = obj2.FromJson<Second>(json2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment