C # Inheritance

Source: Internet
Author: User
Inheritance is one of the most important concepts in object-oriented programming. Inheritance allows us to define a class by defining another class based on one class, which makes it easier to create and maintain applications. It also facilitates the reuse of code and saves development time.

When creating a class, the programmer does not need to completely rewrite the new data member and member functions, just design a new class, inheriting the members of the existing class. This existing class is called the base class, and this new class is called a derived 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 classes and derived classes

A class can derive from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces.

The syntax for creating derived classes in C # is as follows:

<acess-specifier> class <base_class>{...} class <derived_class>: <base_class>{...}

Suppose that there is a base class Shape, and its derived class is Rectangle:

Using System;namespace inheritanceapplication{class Shape {public void SetWidth (int. w) {width = w;} public void Sethei ght (int h) {height = h;} protected int width; protected int height; }//derived class class Rectangle:shape {Publicint Getarea () {return (width * height);}} class Rectangletester {static VOIDMA In (string[] args) {Rectangle Rect = new Rectangle (); Rect.setwidth (5); Rect.setheight (7); The area of the printed object Console.WriteLine ("total area: {0}", Rect.getarea ()); Console.readkey (); } }}

When the above code is compiled and executed, it produces the following results:

Total area: 35

Initialization of the base class

Derived classes inherit the member variables and member methods of the base class. Therefore, the parent class object should be created before the child class object is created. You can initialize the parent class in the member initialization list.

The following program demonstrates this:

Using System;namespace rectangleapplication{class Rectangle {///member variable protected double length; protected double width; pu Blic Rectangle (Double L, double w) {length = l; width = w;} public double Getarea () {return length * width;} public vo ID Display () {Console.WriteLine ("Length: {0}", length); Console.WriteLine ("Width: {0}", width); Console.WriteLine ("Area: {0}", Getarea ()); }}//end class Rectangle class Tabletop:rectangle {private double cost; Publictabletop (double L, double W): Base (L, W) {} public double getcost () {double cost, cost = Getarea () *, return cost, public void Display () {base. Display (); Console.WriteLine ("Cost: {0}", Getcost ()); }} classexecuterectangle {static void Main (string[] args) {Tabletop T = new tabletop (4.5,7.5); T.display (); Console.ReadLine (); } }}

When the above code is compiled and executed, it produces the following results:

Length: 4.5 Width: 7.5 Area: 33.75 Cost: 2362.5

C # Multiple Inheritance

Multiple inheritance refers to the ability of a category to inherit behavior and features from more than one parent class at a time. As opposed to single inheritance, single inheritance refers to a category that can inherit from only one parent class.

C # does not support multiple inheritance. However, you can use interfaces to implement multiple inheritance. The following program demonstrates this:

Using System;namespace inheritanceapplication{class Shape {public void SetWidth (int. w) {width = w;} public void Sethei ght (int h) {height = h;} protected int width; protected int height; }//base class Paintcost public interface paintcost {int getcost (int),}//derived class class Rectangle:shape, Paintcost {public int Getarea () {return (width * height),} public int Getcost (int.) {return area * 70;}} Class Rectangletester {static void Main (string[] args) {Rectangle Rect = new Rectangle (); int area; Rect.setwidth (5); Rect.setheight (7); Area = Rect.getarea (); The area of the printed object Console.WriteLine ("total area: {0}", Rect.getarea ()); Console.WriteLine ("Total Paint cost: ${0}", Rect.getcost (area)); Console.readkey (); } }}

When the above code is compiled and executed, it produces the following results:

Total area: 35 Total paint Cost: $2450

Read more about C # Inheritance related articles please follow topic.alibabacloud.com!
  • 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.