CSharp OOP - Inheritance (Class inheritance, Interface implementation)
2013-05-29 21:00
337 查看
/*
Author: Jiangong SUN
*/
Inheritance, Polymorphism and Encapsulationare the three most important concepts in OOP.
Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.
A derived class can have only one direct base class. However, inheritance is transitive.
A class can implement multiple instances.
I will show two examples of inheritance which are :
1) Class inheritance
2) Interface implementation
1) Class inheritance
A parent class with 2 constructors and 1 method:
A child class heritate the parent class and communicate with parent class.
Test them, the results are shown on the right of code.
2) Interface implementation
Here I've declared two interfaces with some methods signatures.
PS: An interface contains only the signatures of methods, delegates or events, and of course properties.
Then a class implement these two interfaces with respective different implementation of every methods and properties declared. You should notice that, to distinguish the different implementations, the interface name should be ahead of the every single implementation
of method if they have same names.
If they don't have same names, you just need to use "Public" ahead of the property or method, and you don't need to use interface as prefix.
Then the first 3 lines are to test the common implementations of methods.
Then the second and third parts are to test the method implementations for each interface.
I hope you can find this article helpful. Enjoy coding!
Reference:
http://csharp-station.com/Tutorial/CSharp/Lesson08 http://msdn.microsoft.com/en-us/library/64syzecx(v=vs.80).aspx
Author: Jiangong SUN
*/
Inheritance, Polymorphism and Encapsulationare the three most important concepts in OOP.
Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.
A derived class can have only one direct base class. However, inheritance is transitive.
A class can implement multiple instances.
I will show two examples of inheritance which are :
1) Class inheritance
2) Interface implementation
1) Class inheritance
A parent class with 2 constructors and 1 method:
A child class heritate the parent class and communicate with parent class.
Test them, the results are shown on the right of code.
2) Interface implementation
Here I've declared two interfaces with some methods signatures.
PS: An interface contains only the signatures of methods, delegates or events, and of course properties.
Then a class implement these two interfaces with respective different implementation of every methods and properties declared. You should notice that, to distinguish the different implementations, the interface name should be ahead of the every single implementation
of method if they have same names.
If they don't have same names, you just need to use "Public" ahead of the property or method, and you don't need to use interface as prefix.
Then the first 3 lines are to test the common implementations of methods.
Then the second and third parts are to test the method implementations for each interface.
I hope you can find this article helpful. Enjoy coding!
Reference:
http://csharp-station.com/Tutorial/CSharp/Lesson08 http://msdn.microsoft.com/en-us/library/64syzecx(v=vs.80).aspx
相关文章推荐
- inheritance & class variable DEMO
- Kotlin-08.类和继承(class/Inheritance)
- 从Csharp走到VB.Net(一):MyClass保留字
- (11)映射继承关系二之每个类对应一张表(@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
- Csharp: ChineseLunisolarCalendar class .
- CSharp OOP - Delegate ( MultiCast, Named , Anonymous, Lambda, EventHandler )
- 15.6. Class Scope under Inheritance
- Part 34 to 35 Talking about multiple class inheritance in C#
- What does it mean to “delegate to a sister class” via virtual inheritance?
- Invalid single-table inheritance type: 1 is not a subclass of
- Class Inheritance in C++
- oop-Inheritance & Polymorphism
- Class撑起了OOP世界的天。Class类是OO的基本单元,OO的世界都是通过一个一个的类协作完成的,提高软件的重用性、灵活性和扩展性(转)
- 如何把csharp里面的class/struct转换成byte array
- case class inheritance
- [C++] OOP - Access Control and Class Scope
- JavaScript面向对象OOP思想Class系统
- 1641 OOP in C (Date class)
- 面向对象 oop Class和Object
- 条款二十四:了解virtual functions、multiple inheritance、virtual base class、runtime type identification的成本