Wednesday, April 29, 2015

Then, we need to instantiate a MurongXiaoPiFu class. So here every man to introduce newobj commande


Today is the first day of the Yi Wei Year of the Goat, the small every man here to give you a first look at given the rank of a years. Tell me what you do not know whether the ordinary man, like, touch the keyboard amortization calculator hands are already a little rusty out? So, in order not to forget the mission of the program ape, ape not snub the best partner program - the keyboard. Every man decided to write "with CIL write programs," the latest article. But what topic to write it? Before every man also introduced CIL actually object-oriented, so the big New Year's wondering, it is better to write a class, an abstract class is used to it little ordinary man, both introduced small ordinary man, every man can also take this small class Happy New Year to you. So order from top to bottom, is nothing less than how to declare a class, class members how to define that how an instance of a class later, and calling various methods of instances, of course, complete CIL code for this article you can see in the appendix . Murong amortization calculator small ordinary man dressed in a coat so what else? All reference types are derived from System.Object, it creates a new instance of the class is the constructor System.Object actually be achieved. So based on the above two points, to achieve our constructor also very clear: // constructor .ctor implementation .method amortization calculator public void .ctor () {.maxstack 1 ldarg.0 // 1 will cite examples of pressure Stack call instance void [mscorlib] System.Object ::. ctor () // 2. constructor ret call the base class}
Remark poor men. As everyone amortization calculator knows, MurongXiaoPiFu this class inherits amortization calculator from System.Object The default base class. But when we omit inherit statement, CIL compiler will automatically recognize our classes derived from System.Object, in other words, it will be taken as a reference type. So if we do not omit inherit statement, our class how explicitly inheritance it? Yes, with the extend keyword. Then add inherited from System.Object statement (though this is redundant, but in order to demonstrate the use of inheritance here is still very low in this writing), we complete declaration code is as follows: // statement .class MurongXiaoPiFu class MurongXiaoPiFu extends [mscorlib] System.Object // same System.Object. The reference from the mscorlib assembly {// constructor .ctor implementation .method public void .ctor () {.maxstack 1 ldarg.0 // 1 instance of pressure Stack call instance void [mscorlib] System.Object ::. ctor () // 2. constructor ret call the base class}}
So slightly, amortization calculator every man's basic message is this: the // variable declarations .field public amortization calculator string name .field public int32 age .field public string sex .field public string job // assignment ldstr "Chen Jiadong" stfld string for each member variables MurongXiaoPiFu :: name ldc.i4.s 0x19 // 25 years stfld int32 MurongXiaoPiFu :: basic information age ldstr "man" stfld string amortization calculator MurongXiaoPiFu amortization calculator :: sex ldstr "program ape" stfld string MurongXiaoPiFu amortization calculator :: job of a member function of every man there, But every man wants to share with everyone the New Year and introduce ourselves ah. So the light has a member variable is not enough, amortization calculator we also need a few member functions. Speaking of a class of functions, the first concern, what? Right ah, what a class function is a static function it or instance function? So then we're going to implement a function to include both static function, but also have examples amortization calculator of functions. Of course, if you want to be more thorough, we have to take into account the virtual functions. For example, these three functions: hello (SayHi) static function to introduce amortization calculator ourselves (Introduce) instance of the function Year (HappyNewYear) instance function, virtual function then, we realize amortization calculator that two instances of functions and static function uses the opposite static, instance methods use instance amortization calculator key word. But CIL class member functions amortization calculator are examples of the default function, so we can omit the instance keyword. At the same time, because it is an instance function, so before each instance variable certainly get a reference to the first known instance. Therefore, the value of each step, we must first "ldarg. 0" will push a reference instance, after re-use "ldfld" to push values. // Instance of the function to introduce ourselves .method void Introduce () {.maxstack 7 ldstr "My name is {0}, {1} years old this year, is a {2}" ldarg.0 ldfld string amortization calculator MurongXiaoPiFu :: name ldarg.0 ldfld int32 MurongXiaoPiFu :: age box int32 // int type of age have a packing ldarg.0 ldfld string MurongXiaoPiFu :: job call void [mscorlib] System.Console :: WriteLine (string, object, object, object) ret} // Examples of function, virtual function, a good New Year .method public virtual void HappyNewYear () {.maxstack 1 ldstr "Happy New Year" call void [mscorlib] System.Console :: WriteLine (string) ret}
Then, we need to instantiate a MurongXiaoPiFu class. So here every man to introduce newobj commanded, newobj instructions by calling the class constructor to create a new instance. After creating a good example, the reference to an instance of lying in the stack, so in order to have just declared amortization calculator Murong variable assignment, we need to pop the stack instance reference assigned amortization calculator to Murong, and therefore need to use stloc. newobj instance void class MurongXiaoPiFu amortization calculator :: '. ctor' () stloc Murong
And if you are calling instance function, you have to push a reference to the first instance, but also in the back call also need to add instance. amortization calculator So we need to call Introduce and HappyNewYear specify their instances, that is, an instance of a reference to the use of ldloc push, then call when the call is also need to specify an instance method. amortization calculator // Call the instance method ldloc Murong call instance amortization calculator void class MurongXiaoPiFu amortization calculator :: Introduce () ldloc Murong callvirt instance void class MurongXiaoPiFu amortization calculator :: HappyNewYear ()
Tell me if you feel better at writing, then will let small ordinary man beg you to "recommend" Thanks ~ bumbling declaration point of a look: This blog articles are all original, if not otherwise specified, For reprint please retain Original link (http://www.cnblogs.com/murongxiaopifu/p/4296151.html) and author information Murong small ordinary man Appendix .assembly extern mscorlib {.ver 4: 0: 0: 0 .publickeytoken = (B7 7A 5C 56 19 34 E0 89) // .z / V.4 ..} .assembly 'HelloWorld' {} .method static void Fanyou () {.entrypoint .maxstack 4 .locals init (class MurongXiaoPiFu amortization calculator Murong) newobj instance void class MurongXiaoPiFu: : '. ctor' () stloc Murong amortization calculator call void MurongXiaoPiFu :: SayHi () ldloc Murong call instance void class MurongXiaoPiFu :: Introduce () ldloc Murong callvirt instance void class MurongXiaoPiFu :: HappyNewYear () // ldstr "Hello World!" / / call void [mscorlib] System.Console :: WriteLine (string) ret} .method static void AddLife () {.maxstack 4 // local variables amortization calculator .locals init (int32 num1, int32 num2, int32 result) // first Function: Display the prompt input addend and obtain the value of the input // "Please enter the first addend" ldstr "Please enter the first addend" on the screen amortization calculator call void [mscorlib] System.Console amortization calculator :: WriteLine ( string) // get user input values call string [mscorlib] System.Console :: ReadLine () // string input converted to int call int32 [mscorlib] System.Int32 :: Parse (string) // value of the Stack, assigned to a local variable num1 stloc num1 // num2 ldstr "Please enter the second addend" call void [mscorlib] amortization calculator System.Console :: WriteLine (string) call string [mscorlib] System.Console :: ReadLine () call int32 [mscorlib] System.Int32 :: Parse (string) stloc num2 // second function: to kill one love, no, love should be the sum ... // push values into the stack from variable ldloc num1 ldloc num2 the sum will add // // assign the result to result stloc result // last feature, the key fact is packing // display format ldstr "{0} + {1} = {2}" // will num1, num2, result packing, writeLine for later use. ldloc num1 box int32 ldloc num2 box int32 ldloc result box int32 // The formula is displayed call void [mscorlib] System.Console :: WriteLine (string, object, object, object) ret} // declare the class MurongXiaoPiFu extends .class MurongXiaoPiFu [mscorlib] System.Object // same System.Object from the mscorlib assembly {.field amortization calculator public string name .field public int32 age .field public amortization calculator string sex .field public string job // constructor .ctor implementation .method public void. ctor () {.maxstack 10 // define each member variables ldarg.0 ldstr "Chen Jiadong" stfld string MurongXiaoPiFu :: name ldarg.0 ldc.i4.s 0x19 // 25 years stfld int32 MurongXiaoPiFu :: age ldarg.0 ldstr " M "stfld string MurongXiaoPiFu :: sex ldarg.0 ldstr" program ape "stfld string Muro

No comments:

Post a Comment