Feel the cross-language characteristics of .NET
During this time, I also found a lot of information on the Internet, I found that most tutorials were discussing the technology trend of .NET and changes to software production. It is often simply talking about. Icon distribution calculation, components Chemical, etc., there are very few awareness of a little feeling. Perhaps such an introduction for masters is already enough. Can understand that distribution calculation for a large number of beginners like me. What the component is representative, the only thing you can know is that it is very high technology.
So I have an idea that I have a powerful force. I wrote my own things. I hope that I can have a bit peri-sensible understanding of the rookie. Net is a bit peri-feeling, but also let everyone come to batch, help himself progress.
.NET support language independent, and language integration, you can inherit a class written by the .NET Public Type System (CTS), capturing his exception, using multiple multiple-transpared multiple times. That is to say in the .NET environment Lowering can be mixed. You can derive a class from the base class written in a completely different language, or you can capture an exception written in another language.
In this example, we will generate an abstract base class human using the controlled C ; then generate a class Woman derived from Human, generate a class MAN derived from Human. The structure is as follows: C Abstract base class: Human he has three polymorphous methods: Eatfood (), Sport (), errorsomething () Human derived class written with VB.NET: Woman he will overwrite three methods of Human Eatfood (), Sport ), Errorsomething () and will throw an exception in ErrorSomething () Human derived class written by C #: Man will cover three methods of Human Eatfood (), Sport (), Errorsomething ()
We use the controlled C to write the base class Human, which implements an IFACE interface to support eating and exercise, and there is a virtual function called ERRORSMETHING, and any Human's specific inheritance class must implement this method.
The code is as follows --------- Code start ------------ // File Name: human.cpp # Using
Public__gc class human: public ifce {public: Virtual void Eatfood () {console :: WriteLine ("It's all to eat!");} Virtual void sport () {console :: WriteLine ("Life is moving!" Virtual void errorsomething () = 0;
-------------- CODE END ---------------
Save this definition to the human.dll file to compile and connect to CL / CLR / C Human.cpplink -dll /out:human.dll -Noentry Human.obj
We will use VB.NET from Human's base class, which is called Woman. Here we will override EatFood (), Sport () and Erroorsomething three methods, here ErrorSomething is a bit special, we will be in this method The artificial throws an exception to test this exception after inheriting this class written by VB.NET using other languages.
The code is as follows -------------------- Code Start ------------------- 'File Name: Woman .vbimports systempublic class womaninherits humanoverrides public sub eatfood () Console.Writeline ("Female Eat") End Sub
Overrides public subun () Console.Writeline ("Female Movement") End Suboverrides Public Sub Errorsomething () Console.Writeline ("Women's Class Errors") Throw new Exception ("Excending") End Subend Class ---- -----------------------------------------------------------------------------------
Generate DLLVBC /R :Human.dll / T: library /out:Woman.dll Woman.vb using VB.NET compiler
Now we use C # to generate the second class MAN inherited from Human, and similar to the Woman class generated by VB.NET, the three virtual functions will be covered here, but do not throw an exception in the errorsomething () method.
--------- Code Start ----------------- // File Name: man.csusing system; public class man: human {Override public void eatfood () {Console.writeline ("Men's Eat");} Override Public Void Sport () {Console.writeLine ("Men");} Override Public Void ErrorSomething () {Console.writeline ("Men's error"); }} -------------- Code end -------------- Use the following command to generate dllcsc / r :huMan.dll / T: library / out: Man.dll man.cs
Finally, let us generate a test class to test cross-language applications polymorphism and exception handling the following code uses C # contains a main () method with an Human application and an exception processor. ---------- ----- Code Start ----------------------- // File Name: Test.csisting System; Class test {public static void main ) {Human hh; try {man mm = new man (); hh = mm; hh.eatfood (); hh.errood (); Woman ww = new woman (); hh = ww; hh.eatfood (); hh (); Catch (Exception E) {Console.Writeline (E.TOString ());}}} ------------ Code end -------- ---------------- There is a main () method in the Test class. First, an object HH from the Human class is defined. In the TRY sentence, a MAN class is instantiated first. And use HH reference to it. Here we didn't let MAN to perform EatFood (), or ErrorSomething (), but let a Human object hh to do, instantiate a Woman class, point to him with HH, nor will Woman class To perform EatFood () or errorsomething (), but let the Human object to do. On the surface, it is human to perform Eatfood () and ErrorSomething (), but in fact the former is MAN is an instance, the latter is a Woman instance. Also The state, but he is a cross-language
At the same time, in the second ErrorSomething because it is a Woman class, it throws an exception, we will capture this exception in the TEST class, because the exception throws that the program is written with VB.NET, and our capture exception is In C #, this also illustrates the processing of abnormalities.
Create an Exe file CSC /R :Human.dll;Woman.dll; man.dll / t: Exe /out: Test.exe test.cs The compiled and final operation results are shown below: