Java Experience--object-oriented preliminary __java

Source: Internet
Author: User
1. Object: Is the most basic unit that forms the system; Attributes: Describes the static characteristics of objects; Behavior: Describes the dynamic characteristics of an object. In the Java official parlance, variables are referred to as field, so there are places where variables are translated into fields.
A heap is a run-time data area in which objects of the class allocate space, and they do not require the release of program code to display, but are handled by garbage collection. The advantage of the heap is that it can dynamically allocate space, the lifetime does not have to tell the compiler in advance, because they are dynamically allocating memory at runtime; The disadvantage is that the solid access speed is slow due to the dynamic allocation at runtime. The advantage of the stack is that the access rate is faster than the heap, after the register, the data can be shared; The disadvantage is that the data size and lifetime in the stack must be fixed and inflexible. Therefore, the reference variable in the stack memory does not really store the variable data in the object, the object's variable data is actually stored in the heap memory, and the reference variable is only pointing to the object in the heap memory.
2. Class: A group of objects with the same attributes and behavior of the abstract. A class is a template, an object is an instance; A class is abstract and the object is concrete. The class is a building drawing, and the object is a building.
Modifiers: can be omitted. can also be: public, protected, private, static, final, abstract, the first three of which can only appear one, the latter two are also, but the latter two can be combined with static to modify the method.
3. Method return value type: Can be any data type in the Java language, and if the return value type is declared, the method body must have a valid return statement that returns the value of a variable or expression that must match the type of the method return value; If a method has no return value , you must declare it by using void.
4. Construction method: is a special method in the class
? The name of the constructor method is the same as the class name;
? The constructor does not return a value and is declared without void; (if the constructor does not have a return value using the Void keyword, the compile-time error will not occur, but Java will treat this so-called construction method as a normal method)
? When the class is instantiated, the construction method is invoked automatically. Therefore, the constructor functions-initializes the members in the class object.
If the programmer does not write a constructor for a class, the system will provide a default construction method that the system will no longer provide once the programmer has provided it.
5. Class name variable [=new class constructor (argument)]
For example: Car obj=new car ("Mercedes Benz 600", "silver Grey");
Object name. Member variable name or object name. Member Method name (argument list)
For example: Obj.name= "BMW"; or obj. Run (100);
? If the mission of an object (instance) is complete, it should be removed from memory.
? When an object is not referenced by any reference variable, the object is cleared
? Eliminates unwanted objects through the memory garbage automatic collection mechanism (finalizer).
6. Default construct: public class name () {}
Overloading of constructed methods: When a class has multiple overloaded construction methods, the statement that creates the object of the class automatically invokes the corresponding construction method based on the number of actual arguments given, the type of the argument, and the order of the parameters.
With no arguments, the method body is empty. Function: Automatically initializes a member variable to its default value.
The role of 7.this:
1. In the same class, a class's member variable cannot have the same name, but a local variable in a method or statement block can duplicate the class's member variable, and this must be used to qualify and distinguish between a class variable.
2. The This keyword can also be used for mutual application between methods. Most of the time, when a method accesses other methods of this class, this can be omitted, but for static, the class name must be invoked directly, and if the This keyword is used, the keyword cannot point to the appropriate object, so this reference cannot be used in a static-decorated method , and therefore static cannot access ordinary variables that do not use the static modifier, which is exactly the same as static variables that cannot directly access non-static variables.
public void Highrun (int speed) {
int highspeed=2*speed;
This.run (highspeed);
}
3. This reference can also be used as the default reference in the constructor method, because the constructor is invoked directly using the New keyword, so calling another constructor in the constructor cannot write the constructor name directly, using the this reference to invoke the other constructor for initializing the object. Note: This call statement must be the first executable statement in the constructor.
4. Reference the class itself in its own method. When this is the default reference for an object, the program can access the This reference like a normal variable, or even use this as the return value of the normal method. (Return this)
8. Methods cannot exist independently, and methods must belong to a class or object. Therefore, if you need to define a method, you can only define it in the class body.
1. A method that is decorated with static is part of this class, so the static-decorated method of a class can exist even if no specific object is created for the class. Static methods (class methods) belong to a class, or to all objects that belong to the class. Therefore, if you create a specific object for a class, you can also invoke the class method through the object name.
Attention:
? The This reference cannot be used in a static method.
? Static methods can only handle static properties and invoke static methods.
? Static-decorated methods and variables can be invoked either by a class or by an object, or by an object, without a normal variable decorated with static.
Static is a special keyword that can be used to modify members such as methods, variables, and so on. A member of the static modifier indicates that he is common to this class and does not belong to a single instance of the class, so the variables and methods that are modified by static are usually used as class variables and class methods, and the normal method without static modification is a single instance of the class, not the class. So these become instance variables and instance methods. And because static literal translation comes from static meaning, the former is also called static variable and static method, the latter is non-static variable and non-static method. Note: Static members cannot access non-static members.
The class name (the class is the caller of the static method). Method name (class method or Static method
) ([actual parameter list]);
2. Methods that do not use static modification belong to the object of the class and are not part of the class; Therefore, the method must use the object as the caller:
Need to note:
? Methods cannot be defined independently and can only be defined in the class body.
? In a logical sense, a method is either a class or an object. Therefore, the execution method must use a class or object as the caller.
? When a method in the same class invokes one another, if the called method is a normal method, this is used by default as the caller, and the class is used as the caller by default if the method being invoked is a static method.
The class name (the class is the caller of the method). Method name (instance method or Non-static method) ([argument list]);
9. Parameter passing of the method:
Note: The method's parameter pass in Java is passed by value---the copy (replica) of the actual parameter value into the method, but the actual parameter value itself is not affected by any effect.
1). Basic types of variables as method parameters

When the method Changab () is invoked, the exchange of formal parameter A and B values does not affect the value of arguments A and B in the main method.
2). Variable of reference type as method parameter
When the value of a and B of the Datawrap object referenced by the DW parameter is exchanged in the Changeab method, we see that the value of a and B of the Datawrap object referenced by the DW variable in the main method is also exchanged. That is, the address reference is passed.
Assign the DW directly to NULL so that it no longer points to any valid addresses. Dw=null;
The DW object in the main method is unaffected by any
10. Method of variable parameter length
After JDK1.5, Java allows you to define parameters with variable parameter lengths, allowing you to specify an indeterminate number of formal parameters for the method.
? method definition of variable parameter length: If the method is defined, add three points after the type of the last formal parameter ..., it means that the formal parameter can accept more than one parameter value and multiple parameter values are passed in as an array.
Example: The method of variable parameter length is outclassinfo.
public static void Outclassinfo (String cname,string...sname) {
To treat parameter sname as an array in the method body
}
Attention:
? Defining methods with variable number parameters
public static void Outclassinfo (string cname, string ... sname);
? Using array parameters to define methods
public static void Outclassinfo (String cname, string[] snames)
The effect of these two methods signatures is the same, but there is a difference:
? The method of calling deformable parameters is simpler;
? A method that invokes an array parameter must be passed to an array of the parameters, Outclassinof ("09 Software", "New string[]{", "Harry", "Dick"}); the actual parameter value of a method is not necessarily an array, but if the method is declared with an array parameter, When called, you must pass an array of the parameters.
? A formal parameter in the form of an array can be anywhere in the parameter list, but a variable number of parameters can only be at the end of the formal parameter list. That is, a method can have at most a variable length of a parameter.
11. Recursive method
If a method definition calls itself directly or indirectly, it is the recursive invocation of the method. A recursive invocation of a method contains an implicit loop that repeats a piece of code, but this repetition does not require circular control.
Example: Write a program that calculates the factorial of 10.
public static long Calc (int n) {
if (n==1) {
return 1;
}else{
Return N*calc (n-1);
}
}

The recursive method generally consists of two parts:
(1) Recursive body: Recursive method.
(2). Recursive exit: Condition of recursion termination.
When a method keeps calling itself, the return value of the method must at some point be determined, that is, it is no longer invoked itself; otherwise, this recursion becomes infinite recursion, similar to a dead loop, so there is an important rule in defining a recursive method; recursion must be recursive to a known direction.
When you want to go through a certain depth of unknown files, the common recursive implementation. (Procedure: A system that defines a method that accepts a file path as an argument) recursively iterates through all the files and file paths under the current path, and then calls itself again to process all the files and file paths under the new path until all the files and their file paths are traversed.
12. Overloading of methods
The Java language allows you to define several methods with the same name in a class, but requires that these methods have different parameter characteristics, which is called the overload of the method (overloaded). The different parameter characteristics include:
? Number of parameters: int getsort (int x);
int getsort (int x,int y);
? Parameter type: int getsort (int x);
int Getsort (double x);
? Parameter order: int getsort (int x,double y);
int Getsort (double x,int y);
Note: The return value type is not the basis for overloading.
*: Invocation of overloaded methods: when there are multiple overloaded methods in a class that invoke one of the overloaded methods of the class, Java can differentiate between the actual invocation of which method based on the different argument tables.
Method overload requirements: Two identical different method names in the same class and different argument lists. Does not have any relationship with other parts of the method (such as: Method return value type, modifier, etc.)
The overload of the example 4.12 method.
Overload ol=new overload ();
Ol.test (); Invoke test with no parameters ()
Ol.test ("Hello"); To invoke Test () with a string parameter
Example 4.13 The overloaded method has a formal parameter with variable length.
public void Test (String msg);
public void Test (String ... books);
Then, Olv.test (); Call Test (String ... books);
Olv.test ("AA", "BB"); Call Test (String ... books);
Olv.test ("AA"); Call Test (String msg);
If you pass in only one parameter, and you need to call a method with variable length, you can invoke the following form: Olv.test (new string[]{"AA"});
Overloading is not recommended as a method with variable parameter lengths because it does not make much sense and can easily cause readability of the program.

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.