C # started practicing 7 (constructor statement)

Object class is created using the "Object class name name = new class name ()" method to achieve.

In fact, in the form of "class name ()" call is the class constructor, that constructor is the name of the class with the same name.

Constructor defined syntax is as follows.

Access modifiers class name (parameter list)
{
    block of statements;
}

Here constructor access modifiers are usually publicthe type, this can create objects of that class in other classes.

If the access modifier is set to privatetype, you can not create objects of that class. Constructor parameters and other methods, are zero or more parameters.

In addition, the constructor is called when creating an object class. Some operations will usually members of the class into the constructor initialization to complete.

the using the System; 

namespace KingTest03 
{ 
    class Program 
    { 
        int A; 
        String B; 
        int C; 

         Program ( int Id, the Name String, int Price) // Assignment also be encapsulated together in the form of parameters 
        {
             the this II.A = Id;
             the this .B = the Name ;
             the this .c = Price; 
        } 
        public  void Print () 
        { 
            Console.WriteLine ( " book ID is: " + the thisII.A); 
            Console.WriteLine ( " book name: " + the this .B); 
            Console.WriteLine ( " book price is: " + the this .c); 
        } 
        static  void the Main ( String [] args) 
        { 
            Program Program = new new Program ( 123 , " learning # C " , 432 ); 
            program.print (); 
        } 
    } 

}

 

Guess you like

Origin www.cnblogs.com/BruceKing/p/11551016.html