C#学习笔记-接口与抽象类
2018-06-23 23:49:51来源:未知 阅读 ()
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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:linq中给字段添加别名
- HTML骨架 2020-06-10
- 转行Web前端工程师要掌握的学习知识汇总 2020-06-10
- 毕业生想学习web前端开发,有什么好的发展方向吗? 2020-06-09
- 转行前端很迷茫,该怎么学习? 2020-06-06
- 前端学习记录 2020-06-05
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash