C # Learning Diary---multiple inheritance

Source: Internet
Author: User
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)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.