C# inheritance

Feb 08, 2017 pm 01:35 PM
c# inherit

Inheritance is one of the most important concepts in object-oriented programming. Inheritance allows us to define a class based on another class, which makes it easier to create and maintain applications. It also helps reuse code and save development time.

When creating a class, programmers do not need to completely rewrite new data members and member functions. They only need to design a new class and inherit the members of the existing class. This existing class is called the base class, and the new class is called a derived class.

The idea of ​​inheritance realizes the Belongs to (IS-A) relationship. For example, mammals are (IS-A) animals, dogs are (IS-A) mammals, so dogs are (IS-A) animals.

Base classes and derived classes

A class can be derived from multiple classes or interfaces, which means it can inherit data and functions from multiple base classes or interfaces.

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

class { ... } class : { .. . }

Suppose 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 SETHEIGHT (int H) {Height = H;} Protected Int Width; Protected Int Height;} // Disposter class Rectangle: Shape {PublicInt Getarea ( ) {RETURN (Width*Height);}} Class Rectangleteter {Static voidMain(string[] args) { Rectangle Rect = new Rectangle(); Rect.setWidth(5); Rect.setHeight(7); //Print the area of ​​the object Console.WriteLine("Total area: {0 }", Rect .getArea()); Console.ReadKey(); } }}

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

Total area: 35

Initialization of base class

The derived class inherits 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   {      // 成员变量      protected double length;      protected double width;      public Rectangle(double l, double w)      {         length = l;         width = w;      }      public double GetArea()      {         return length * width;      }      public void Display()      {         Console.WriteLine("长度: {0}", length);         Console.WriteLine("宽度: {0}", width);         Console.WriteLine("面积: {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() * 70;         return cost;      }      public void Display()      {         base.Display();         Console.WriteLine("成本: {0}", GetCost());      }   }   classExecuteRectangle   {      static void Main(string[] args)      {         Tabletop t = new Tabletop(4.5,7.5);         t.Display();         Console.ReadLine();      }   }}

当上面的代码被编译和执行时,它会产生下列结果:

长度: 4.5宽度: 7.5面积: 33.75成本: 2362.5

C# 多重继承

多重继承指的是一个类别可以同时从多于一个父类继承行为与特征的功能。与单一继承相对,单一继承指一个类别只可以继承自一个父类。

C# 不支持多重继承。但是,您可以使用接口来实现多重继承。下面的程序演示了这点:

using System;namespace InheritanceApplication{   class Shape   {      public void setWidth(int w)      {         width = w;      }      public void setHeight(int h)      {         height = h;      }      protected int width;      protected int height;   }   // 基类 PaintCost   public interface PaintCost   {      int getCost(int area);   }   // 派生类   class Rectangle : Shape, PaintCost   {      public int getArea()      {         return (width * height);      }      public int getCost(int area)      {         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();         // 打印对象的面积         Console.WriteLine("总面积: {0}",  Rect.getArea());         Console.WriteLine("油漆总成本: ${0}" , Rect.getCost(area));         Console.ReadKey();      }   }}

当上面的代码被编译和执行时,它会产生下列结果:

总面积: 35油漆总成本: $2450

更多C# 继承相关文章请关注PHP中文网!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Access Modifiers in C# Access Modifiers in C# Sep 03, 2024 pm 03:24 PM

Guide to the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

C# StringReader C# StringReader Sep 03, 2024 pm 03:23 PM

Guide to C# StringReader. Here we discuss a brief overview on C# StringReader and its working along with different Examples and Code.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

BinaryWriter in C# BinaryWriter in C# Sep 03, 2024 pm 03:22 PM

Guide to BinaryWriter in C#. Here we discuss syntax and explanation, how it works with examples to implement with proper codes.

See all articles