Inheritance is one of the most important concepts in object-oriented programming. Inheritance allows us to define another class based on one class to define a class, and when a class derives from another class, the derived class inherits the attribute from the base class.
The thought of inheritance realizes the relationship of (IS-A). For example, mammals belong to (is-a) animals, dogs belong to (is-a) mammals, so that dogs belong to (is-a) animals.
base class and derived class:
Derived classes in C # inherit members from his direct base class, methods, properties, fields, events, index indicators but except constructors and destructors.
Write an example below.
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace Test { class Anlimal //defines a base class { protected int foot = 4; Protected double weight = 22.4; protected void Say (string type, string call) { Console.WriteLine ("Category: {0}, cry: {1}", Type,call); } //dog Inherits Anlimal class Dog:anlimal { static void Main (string[] args) { Dog dog = new Dog ( ); int foot = Dog.foot; Double weight = dog.weight; Console.WriteLine ("Dog foot: {0}\ndog weight:{1}", foot,weight); Dog.say ("Dog", "barking");}}}
Results
Multiple inheritance:
C # does not support multiple inheritance. However, you can use the interface to implement multiple inheritance, the above example we add a Smallanlimal interface for him
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace Test {class Anlimal//defines a base class {protected int foot = 4; Protected double weight = 22.4; protected void Say (string type, String call) {Console.WriteLine ("Category: {0}, cry: {1}", Type,call); }} public interface Smallanlimal//Add an interface interface only declares that the method is implemented in a subclass {protected void hight (double high T); }//dog inherits Anlimal class Dog:anlimal,smallanlimal {public void hight (double hight)//Implement Interface {Console.WriteLine ("Hight: {0}", Hight); } static void Main (string[] args) {Dog dog = new Dog (); int foot = Dog.foot; Double weight = dog.weight; Dog.hight (23.23); Console.WriteLine ("Dog foot: {0}\ndog weight:{1}", foot,weight); Dog.say ("Dog", "barking"); } } }
The above is the C # learning Diary---Multiple inheritance content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!