zoukankan      html  css  js  c++  java
  • C#学习笔记-接口与抽象类

      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 }
  • 相关阅读:
    通过asp.net 生成xml文件
    listbox 多选处理
    girdview 找到其焦点的笨办法
    关于.net 中调用script的alert后 css失效的办法
    从数据库中读数据中寻找若隐若现的OOP
    Gitlab的安装部署和介绍
    守住你的网站:防御DDoS攻击指南
    分析SQL语句使用资源情况
    Linux下Sniffer程序的实现
    NDIS resources
  • 原文地址:https://www.cnblogs.com/Aries-rong/p/6525543.html
Copyright © 2011-2022 走看看