C#学习笔记-接口与抽象类

2018-06-22 04:39:52来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

  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 instantiated
101      * 
102      * the Class can only inherit one Class,but can inherit a great deal of Interface
103      */
104 }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:.Net 面试题 汇总(二)

下一篇:MyBatis从入门到放弃二:传参