The 6th chapter of the Java Development Handbook learning process objects and classes

Source: Internet
Author: User

The traditional process programming design solves the problem by designing a series of processes-algorithms. Once these processes are identified, the next step is to find the way to store the data, "program = algorithm + data structure". Object-oriented programming (that is, oop,object oriented programming) swaps this order, putting the data first, and then considering the algorithm that manipulates the data.

In OOP, programs are treated as a collection of objects that work together, and each object is an instance of a class. All classes make up a hierarchy that is associated with an inheritance relationship.

Because the process-oriented programming pattern is to design a series of processes (that is, functions) and then solve through the parameter input, so for the software maintenance personnel, whether the change of function or data structure, the whole program will be disturbed, which may lead to the collapse of the software system.

The process is closer to the physical realization of the computer world, while the object-oriented thought is more in accord with people's cognition habit.

A class declaration is an abstract specification of the template used to create an object.

The data in the object is called the instance domain, and the process of manipulating the data is called a method.

Once a class is defined, you can declare two kinds of members in the class body: member variables and member methods.

A member variable (also called an instance variable) is a property of a class. Each object's properties have its own value.

When you create an object from new, it returns its reference value and assigns it to a reference, that is, to point the reference to the created object. If you create an object with new but do not point a reference to it, the object cannot be accessed after it is created. Because in Java, objects can only be accessed by referencing a reference to an object.

Note the question about the return value type on page 111th.

Local variables:

Local Variables can only be valid in a statement block .

In addition to the class body, a pair of curly braces, such as a method body, a loop body, an if or else, is a statement block .

Because the block of statements can be nested, the local variables defined in the "foreign Language block" will compile an error if it is found that there is also a local variable with the same name in the "Inside statement block".

A local variable differs from a member variable, and is not automatically assigned an initial value if it is not initialized after the declaration. If an uninitialized local variable is used, an error is compiled.

 Public class sample6_11{    publicstaticvoid  main (string[] args)    {         int i, x = N, y = +;         if (x > y)        {            = ten;        }         int j = i;    }}

As the above code shows, in the "foreign Language block", the local variable I can be used in the "inner statement", but the "inner statement block" used in the "Trace" in the "inner statement block" will be erased outside the "inside". That is, even if the inside statement block is initialized, but after the "inside statement block", the compiler still thinks I is not initialized.

In Java, the work of compiling and running is different.

Variable number of parameters:

Defines a method Sortandprint, which can sort and output any number of integers.

 Public Static void sortandprint (int... entrys) {    arrays.sort (entrys);     int size = entrys.length;    System.out.println ("unlearning Result:");      for (int i = 0; i < size; i++)    {        + ", ");    }}

In this example, Entrys can still be thought of as an array, but in int ... becomes a variable-length array parameter under the declaration.

Attention:

There can be at most one variable-length parameter in a method;

If the method has more than one parameter, the variable-length argument must be the last.

This object reference:

If there is a person class, it has a member method Showaga (), and if a person object is accessed only once, then you can directly new person (). Showage () without first initializing the object reference of a person class and then calling Showage () Method. Because the return value of new is the object reference that was created, this is the same as invoking it with an explicitly declared reference.

A local variable is called when the local variable in the method has the same name as a member variable. In this case, to make the system access member variables, you need to reference this with a predefined object before the variable.

The 6th chapter of the Java Development Handbook learning process objects and classes

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.