C # learning notes-interfaces and abstract classes,

Source: Internet
Author: User

C # learning notes-interfaces and abstract classes,

  1 namespace ClassLesson  2 {  3     class Program  4     {  5         static void Main(string[] args)  6         {  7             var person = new Person(5);  8             Console.WriteLine(person.GetAge());  9             Console.WriteLine(Person.getFive());        //static 10             Console.WriteLine(person.Age); 11             person.Age = 10; 12             Console.WriteLine(person.Age); 13             Console.WriteLine(person.Age2); 14             person.Age2 = 10; 15             Console.WriteLine(person.GetAge()); 16             Console.WriteLine(person.GetName()); 17  18             Console.ReadLine(); 19         } 20     } 21  22  23     //the default modifier is internal,can be accessed in the namespace 24     //the Class can only inherit one Class,but can inherit a great deal of Interface 25     class Person : Main, ISuper 26     { 27         int age; 28  29         public int Age      //default value is 0 30         { 31             get; 32             set; 33         } 34  35         public int Age2 36         { 37             get 38             { 39                 return age + 10; 40             } 41             set 42             { 43                 age = value - 10; 44             } 45         } 46  47         public Person(int myAge)        //constructed function 48         { 49             this.age = myAge; 50         } 51  52         public int GetAge()     //the default modifier is private (in the Class) 53         { 54             return age; 55         } 56  57         public static int getFive()     //static method be stored in the Class 58         { 59             return 5; 60         } 61  62         public int GetSuper() 63         { 64             return age = 100; 65         } 66  67         public override int GetAbstract() 68         { 69             return 50; 70         } 71     } 72  73     interface ISuper     //only include method、property、index and event 74     { 75         int GetSuper();     //only need statement,the details in the Class 76     } 77  78     //abstract class=>can't be instantiated 79     //function:be inherited by other Class 80     abstract class Main 81     { 82         public string Name; 83         public string GetName() 84         { 85             return Name = "string"; 86         } 87  88         public abstract int GetAbstract();   //abstract method's details must be in the Class which inherited this 89     } 90  91     /** 92      * the difference between Abstract Class and Interface: 93      * 1. 94      * Internal:all details be in the Class which inherited it 95      * Abstract Class:only 'abstract' details be in the Class which inherited it 96      * 2. 97      * Internal:can't have member variables and properties 98      * Abstract Class:all 99      * 3.100      * Abstract Class:can't be instantiated101      * 102      * the Class can only inherit one Class,but can inherit a great deal of Interface103      */104 }

 

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.