Differences between constructors and Methods

Source: Internet
Author: User
1. in C #, some are objects. To use an object, you must use new to instantiate the class (static class exception ). When you use the new command, the system will automatically call the constructor of this class to perform initialization and other work. As for the method, you can only use the "object. Method ()" method manually.

 
2. class can have no constructor, because sometimes, the system will help you generate an empty constructor without parameters, it will not do anything, it only exists due to the syntax. Constructor can be overloaded (or overload, I cannot remember these two concepts ). You can only read this method. Java and C # Both talk a lot about it.

3. Stack and heap are the places where temporary data is stored, that is, cache. The object's Word handles exist in heap, and the basic types (such as int and char) exist in stack. Stack is faster than heap. In thinking
Both in Java and C # technology insider (photocopy) indicate that thinking in Java is very detailed and easy to understand.

Q: What is the difference between constructors and methods? A constructor is a constructor. The constructor name is the same as the class name, and the method is customized.* Constructor is a special method used to initialize member variables. Public class test {static int I; // defines a member variable public test () {I = 10;} // This sentence is the constructor, (test and class name are the same) the function is to initialize the member variables. Do not write them. The system will default to an empty constructor, such as public test () {} void plus (int x, int y) {system. out. println (x + y);} // defines a plus method, which indicates that the input value is int type and the input number is 2, add the two numbers and print them out. Public static void main (string [] ARGs) {test T = new test (); // instantiate an object, T. plus (1, 3); // call the plus (member) method through T. System. Out. println (I); // print the I value }}

 

 

My master wrote the following:

ConstructorIs a special type.
He is different fromLocation of his MethodFang
1. Create a pairSymbol-time ConstructorAutomatic RunningWhile the average sideMethod must be tunedCall with statementCan be executed
2. ConstructorRequired number and Class NameMust be the same (includingCase Sensitive)
Iii. ConstructorData cannot be returnedReturn Value Type
For example
ClassA
{
PubliC A () {}// constructFunction Name and classSame name, noReturn Value classType
PubliC strinG B () {...; REturn"Str";} // GeneralAvailable MethodsReturn Value Type, If no response is returnedThe value must be v.Oid
 
}
A aa = neW ();//Create an objectAAWill run automaticallyClass A ConstructionFunction () 
AA. B (); // GeneralMethod B onlyYesHis statementWill be executed

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.