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 }
  • 相关阅读:
    继承—泛型
    单例模式
    继承 4—Monkey
    继承 3—A B E
    继承 2—people
    继承 1—Mucic
    面向对象—汽车
    面向对象—封装—汽车
    Linux下查看CPU型号,内存大小,硬盘空间的命令
    redis源码——数据结构与对象
  • 原文地址:https://www.cnblogs.com/Aries-rong/p/6525543.html
Copyright © 2011-2022 走看看