Calculate inheritance in C #
One of the most important concepts in object-oriented programming is Inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse code features and faster execution time.
When creating a class, instead of writing all data members and new member functions, programmers may inherit the members of an existing class. This existing class is called Base Class - the base class, and the new class is treated as Derived Class - the inheritance class.
The idea of inheritance calculates the IS-A relationship (Being One). For example, mammal IS A animal, IS-A mammal dog, so the IS-A animal is animal, and .
Base Class and Inheritance (Derived Class) in C #
A class can be inherited from more than one other class, that is, it can inherit data and functions from multiple base classes or interfaces.
The syntax for creating inheritance classes in C # is:
class { . } class : { . }
Considering a base class Shape and inheritance Rectangle later: create 3 classes named Shape, HinhChuat and TestCsharp in turn:
The Shape class is the base class
dùng System;
QTMCsharp namespace
{
class Shape
{
protected int chieu_rong;
protected int chieu_cao;
public void setChieuRong (int w)
{
chieu_rong = w;
}
public void setChieuCao (int h)
{
chieu_cao = h;
}
}
}
The HinhChuNhat class is the inheritance class
dùng System;
QTMCsharp namespace
{
class HinhChuNhat: Shape
{
public int TinhTich ()
{
return (chieu_cao * chieu_rong);
}
}
}
The TestCsharp class contains the main () method to manipulate the HinhChat object
dùng System;
QTMCsharp namespace
{
public class TestCsharp
{
public static void Main (string [] args)
{
Console.WriteLine ("Crash in C #");
Console.WriteLine ("------------------------ n");
// create doi tuong HinhChuNhat
HinhChuNhat hcn = new HinhChuNhat ();
hcn.setChieuRong (5);
hcn.setChieuCao (7);
// Print your phone book.
Console.WriteLine ("Standard configuration: {0}", hcn.tinhDienTich ());
Console.ReadKey ();
}
}
}
If you do not use the Console.ReadKey () command; then the program will run and finish (so fast that you can not see the results). This command allows us to see the results more clearly.
Compiling and running the above C # program will produce the following results:
Initialize Base Class in C #
The derived class (Derived Class) in C # inherits member variables and member methods from the base class. Therefore, the parent class object should be created before the subclass is created. You can provide directives to initialize the subclass in the member initialization list.
The following example program illustrates how to initialize Base Class in C #: create 3 classes named HinhChuNhat, ChiPhiXayDung, TestCsharp as follows:
The HinhChuNhat class is the base class
dùng System;
QTMCsharp namespace
{
class HinhChuNhat
{
// members of the staff
protected double chieu_dai;
protected double chieu_rong;
// constructor
public HinhChuNhat (double l, double w)
{
chieu_dai = l;
chieu_rong = w;
}
// phuong thuc
public double planet ()
{
return chieu_dai * chieu_rong;
}
public void Display ()
{
Console.WriteLine ("Chatter: {0}", chieu_dai);
Console.WriteLine ("Junk: {0}", chieu_rong);
Console.WriteLine ("Connection: {0}", TinhDienTich ());
}
}
}
Class ChiPhiXay Inherits class HinhChuNhat
dùng System;
QTMCsharp namespace
{
class ChiPhiXayDung: HinhChuNhat
{
private double cost;
public ChiPhiXayDung (double l, double w): base (l, w)
{}
public double planetChiPhi ()
{
double chi_phi;
chi_phi = tinhDienTich () * 70;
return chi_phi;
}
public void hienThiThongTin ()
{
base.Display ();
Console.WriteLine ("Cost: {0}", crystalChiPhi ());
}
}
}
The TestCsharp class contains the main () method to manipulate the ChiPhiXayDung object
dùng System;
QTMCsharp namespace
{
public class TestCsharp
{
public static void Main (string [] args)
{
Console.WriteLine ("Crash in C #");
Console.WriteLine ("Download and install");
Console.WriteLine ("------------------------ n");
// tao doi tuong ChiPhiXayDung
ChiPhiXayung t = new ChiPhiXayDung (4.5, 7.5);
t.hienThiThongTin ();
Console.ReadLine ();
Console.ReadKey ();
}
}
}
Compiling and running the above C # program will produce the following results:
Multiple inheritance in C #
C # does not support multiple inheritance. However, you can use Interface to implement multiple inheritance. The following example illustrates how to use Interface to deploy multiple inheritance in C #: we create 2 classes named Shape, HinhChatat, TestCsharp and an interface named ChiPhiSon as follows:
The Shape class is the base class
dùng System;
QTMCsharp namespace
{
class Shape
{
protected int chieu_rong;
protected int chieu_cao;
public void setChieuRong (int w)
{
chieu_rong = w;
}
public void setChieuCao (int h)
{
chieu_cao = h;
}
}
}
interface ChiPhiSon
dùng System;
QTMCsharp namespace
{
public interface ChiPhiSon
{
int tinhChiPhi (int dien_tich);
}
}
The class HinhChuNhat is the class that inherits the Shape class and the ChiPhiSon interface
dùng System;
QTMCsharp namespace
{
class HinhChuat: Shape, ChiPhiSon
{
public int TinhTich ()
{
return (chieu_rong * chieu_cao);
}
public int tinhChiPhi (int dien_tich)
{
return dien_tich * 70;
}
}
}
The TestCsharp class contains the main () method to manipulate the HinhChat object
dùng System;
QTMCsharp namespace
{
public class TestCsharp
{
public static void Main (string [] args)
{
Console.WriteLine ("Crash in C #");
Console.WriteLine ("Da ke loser");
Console.WriteLine ("------------------------------");
// create doi tuong HinhChuNhat
HinhChuNhat hcn = new HinhChuNhat ();
int dien_tich;
hcn.setChieuRong (5);
hcn.setChieuCao (7);
dien_tich = hcn.tinhDienTich ();
// print and print.
Console.WriteLine ("Tongue: {0}", hcn.tinhDienTich ());
Console.WriteLine ("Tong read: 0", hcn.tinhChiPhi (dien_tich));
Console.ReadLine ();
Console.ReadKey ();
}
}
}
Compiling and running the above C # program will produce the following results:
Follow tutorialspoint
Previous lesson: Class (Class) in C #
Next article: Polymorphism in C #
You should read it
May be interested
- Excel functions calculate the debt age and the average balancedebt accountants often have to calculate the age of accounts receivable of customers to know which account should be collected first, in addition to calculate the average balance of these receivables to 'improve the quality of financial management. .
- Calculate the total value of the filtered list in Excelfilter is a very useful and easy to use feature in microsoft excel. with filters, you can quickly limit data to only show the necessary information. however, how to calculate the total value of the filtered list? the following article will help you answer the above question.
- How to calculate percentage, format percentage in Excelwant to calculate percentages in excel? the following article from tipsmake will help you do it quickly. you just need to remember the formula and follow a few simple steps.
- What is GPA? What does GPA mean?gpa is a familiar term for international students who apply for scholarships. so what does gpa mean?
- The way to teach children fast mental arithmetic of Japanese peopledare to say that you will never find such interesting mental math or math rules in textbooks!
- 3 ways to calculate totals in Excelto calculate totals on excel, users can use them in three different ways, based on spreadsheets, autosum functions or sum functions with large data areas.
- How to calculate NPV in Excelhow to calculate npv in excel. npv is the net present value (npv), that is, the net present value of an investment. based on the npv, the investor calculates the difference between the current value of cash inflows and the current value of cash outflows.
- How to calculate the distance to view TV reasonably and safely for healthchoosing the right distance when watching television not only helps you fully enjoy the appeal of the entertainment program, but more importantly ensures health safety, especially eyes.
- Instructions for calculating inches for TVshow to calculate the standard size to choose for yourself the tv that best suits your needs.
- How to calculate the number of days in Excelhow many days from january 9, 2015 to june 12, 2017? if you can find results within 5 seconds, you are truly a genius. however, this is no longer a problem when you know how to use excel.