Instantiation means that in object-oriented programming, the process of creating objects with classes is called instantiation. It is the process of concretizing an abstract concept class to this kind of object. The instantiation process is generally composed of class name object name = new class name (parameter 1, parameter 2...parameter n).
In object-oriented programming, the process of creating objects with classes is usually called instantiation, and its format is as follows:
For example, Date date=new Date(); is to create a date object with the date class, which is called the instantiation of the object.
In most languages, instantiating an object is to open up memory space for the object, or to use a new constructor name directly without creating a temporary object.
Example: Java
1 2 3 4 5 6 7 8 9 10 11 | public class A{ public static void main(String[] args){ B b = new B(); //实例化 b.print(); } } class B{ public void print(){ System.out.println( "Hello,World!" ); } } |
Example: c#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | namespace test { class A { public int Sub( int a) { return a++; } } class B { public void Main(String[] args) { int p = ( new A()).Sub(1); System.Console.WriteLine(p); System.Console.ReadKey(); } } } |
Take PHP as an example.
A class is different from a function and cannot be used directly after it is defined, because a class is just an abstract concept and needs to be instantiated by the keyword new before it can be used. The syntax of class instantiation is as follows: variable name = new class name ([constructor]). Among them, the variable name can be the name of any PHP variable, the construction parameter depends on the class constructor (will be described in detail in Section 8.2.5), if there is no constructor, the parentheses are empty.
You can use the class after instantiating it.
E.g:
1 2 3 4 5 6 7 8 9 | <?php class People{ public $name ; public function introduceMySelf(){ echo '内容' , $this ->name.‘内容2’; } } $p = new People(); ?> |
If you want to use the properties and methods of the class outside the class (provided that the property or method is accessible), you need to use the operator ->, the syntax format is as follows:
Instantiated class variable name -> attribute name;
Instantiated class variable name -> method name ([method parameter]);
The method of using the class is the same as calling the function, and the function parameters are given in parentheses (of course there is no need to write when there are no parameters). It should be noted that when using operator->reference attribute, the attribute name is not added with $.
Function template instantiation
Taking C++ as an example, the C++ standard supports explicit instantiation in addition to implicitly instantiating template functions. The syntax is as follows: [2]
template return type function name <type actual parameter list> (real parameter type list);
For example, if you have the following function template:
1 2 3 4 5 6 | template < typename T> void swap(T a ,T b){ T tempt=a; a=b; b=tempt; }; |
Then, the following statement is an explicit instantiation of the function template:
1 | template void swap< int >( int , int ); |
Note: The template keyword is no longer followed by "<>", otherwise it becomes a specialization of the function template.
Instantiation of class templates
Similar to function templates, class templates are just a virtual "drawing". Only during compilation, the compiler generates a real template class based on the given template type arguments, this is the real class code. The template function is generated after the function template is instantiated, and the template class is generated after the class template is instantiated.
When instantiating a template class for different template argument types, the compiler will generate a copy of the template code for each data type, so the result will be increased code size. Therefore, in the actual calling process, the compiler will instantiate member functions in the selected class template. Only those functions that are called, its corresponding class template member functions will be instantiated and generated by the compiler. This is the "selective instantiation" of class templates.
Support