Java Advanced Summary 1__java

Source: Internet
Author: User
Tags access properties modifier getv

the first class is about the object

1. All things are objects

2. The relationship between the two:

3. its definition rules

attribute + method

Case:

Public class people{

//Properties

Public String name;//name

Public char sex;//sex

public int age;//age

//Method

Public void Tell () {

System.out.println ("Hello world!");

}

Public Void Say () {

System.out.println ("My Name: +name+", My Sex: "+sex+", My Age: "+age); "

}

}

4. Use of objects (objects can be accessed by. To access their own properties and methods)

eg:p.name = "xiaoming";//Access Properties

p.sex = ' male '//Access property

p.age = 15;//Access Property

   

P.tell ();//access Method

P.say ();//access Method

 

Volume Case:

Public class rec{

//Properties

public int length;

public int width;

public int height;

//Method

Public void Getv () {

System.out.println ("volume =" + (length*width*height));

}

Public static void Main (string[] args) {

rec r1 = new rec ();

r1.length=10;

r1.width=5;

r1.height=15;

R1.getv ();

 

rec r2 = new rec ();

r2.length=15;

r2.width=10;

r2.height=20;

R2.getv ();

}

}

 

5. Memory Analysis

I found a classic case, this is a more comprehensive

The second method of speaking

method: Similar to a function in mathematics, is a collection of code that completes a certain function.

function: Simplifies the code, improves the readability of the code, and facilitates the sharing between the code.

Here are some of the methods I have summarized, which can be used for reference:

Summary: Define the method, need not need modifiers, need not return the value, need not need parameter list, all is according to the situation, we can control, how simple how to proceed.

One, method call

when calling a method, you must use the class or object as the caller, and the class. method or Object. method.

static methods and common methods:

▲ If the method is preceded by a modifier "static", the method is a static method and the static method belongs to the class. You can invoke a static method by using a class, or you can use an object to invoke it, or the current class call if the call is not previously indicated.

Case:

▲ If the method does not precede the modifier "static", the method is normal and the normal method belongs to the object. Calling a normal method in a static method can only be invoked by using an object invocation, which can be invoked using an object invocation in a normal method, or directly (the default default this,this refers to the current object).

 

return Value:

▲ If the method has a return value, you can define the corresponding type of variable receive, or direct output, when calling the method.

▲ If the method does not return a value, it can be called directly.

 

 

Parameters:

▲ If the method has parameters, the method can be invoked by passing in the corresponding type of argument

▲ If the method has no parameters, it can be called directly.

Summary:

three-look when calling methods:

First look there is no static.

yes: is a static method class. Method Object. Method method (default default class name)

No: Normal method

1. Static method to tune the object.

2. Normal method of the Tune object. Method this. Method method (default default this)

 

The second look has no return value.

No: normal call

have: Define the corresponding type variable receive

Direct Output

 

The third look has no parameters.

No: normal call

there are: passing in the corresponding type variable

Second, the parameter of the method transfer

formal parameter: A parameter in the parameter list of a method when a method is defined.

Argument (actual argument): When a method is invoked, the value passed to the formal parameter is the argument.

Î
there is only one way to pass parameters in Java (value delivery):

Ø Pass the actual value

Ø Transfer Address value

 

Case One: Pass the actual value

value passing (actual value): In effect, the copy of the argument is passed to the formal parameter, regardless of the shape parameter changes, the argument itself is unaffected.

 

case TWO: delivery address value

value passing (address value): It is actually passing a copy of the argument (a replica) to the formal parameter, but the value here is the address value, so the change of the formal parameter will affect the actual parameter changes.

 

Third, method overload

in Java the same class allows you to define multiple methods with the same name, as long as they are different (type, number), which is called the method overload.

... Note: The following is not an overload

Iv. Recursive invocation of the method (understanding expansion)

thinking:

There are five children, the teacher asked the first child how old, he said than the second big 2 years old,

the teacher asked the second child how old he was, and said he was 2 years older than the third.

the teacher asked the third child how old he was, and said he was 2 years older than the fourth.

the teacher asked the fourth child how old, he said 2 years older than the fifth,

the teacher asked the fifth child how old he was, he said 10 years old.

 

Public class demo{

public static int getage (int x) {

int age;

if (x==1) {

Age = ten;

}else{

Age = Getage (x-1) +2;

}

return age ;

}

Public static void Main (string[] args) {

int age = Getage (3);

System.out.println (age);

}

}

The third part is about the construction method

Role:

I use the case to compare, this is more intuitive

no custom construct method

There is a custom construction method

the characteristics of the construction method (be sure to remember)

1. The constructor method name must be consistent with the class name

Public People (String name,char sex,int age) {//constructor method name must be consistent with class name

this. Name = name;

this. Sex = sex;

this. Age = age;

}

2. The constructor method has no return value and cannot write void

 

3. Construction method can not be shown by the programmer direct call

Public static void main (string[] args) {

People p = new people ("Li Tianpeng", ' Male ', 17);

P.tell ();

P.people ("Li Tianpeng", ' Male ', 17);/error

}

4. In the Java language, each class has at least one construction method. (object cannot be created if no method is constructed)

 

5. If the definition of the class does not display any of the constructor methods, the Java compiler automatically provides the class with a default null construction method. (no parameter list, no method body)

Eg:

Public People () {

}

6. If the definition of a class shows one or more constructed methods defined, the system will no longer provide a default null construction method.

Public class People {

Public String name;

Public char sex;

public int age;

Public People (String name,char sex,int age) {

this. Name = name;

this. Sex = sex;

this. Age = age;

}

Public void tell () {

System.out.println ("My name is: +name+", Sex: "+sex+", Age: "+age);"

}

Public static void main (string[] args) {

People p = new people ("Li Tianpeng", ' Male ', 17);

P.tell ();

People p = new people ()//error, the programmer defines a construction method on its own, the system is no longer provided as empty.

overloading of constructed methods

the overload of a method allows more than one method of the same name to exist in the same class, as long as the number or type of arguments is different. In this case, the method is called overloaded, and this process is called the overload of the method (overloading)

The Java compiler can select the appropriate method based on the number and type of parameters that are passed when the method is invoked.

 

the call to the constructor method

call another construct method in one constructor, not using the constructor method name, but using the form of this (parameter).

Note:

call another construct method in one constructor, this (parameter) must be in the first row.

No more ways to think of any place called the constructor method

A constructor method can be invoked at most one of this method in a constructor.

Fourth talk about member variables and local variables

in the Java language, variables can be grouped into two broad categories, depending on the position of the variable:

member variable local variable

 

memory space in Java: Stack, heap, global data area, global code area

Stacks: Base data type, address of reference type

Heap: The true value of the reference type (the value of the array, the properties of the object)

Global Data area: Class properties (with static properties)

 

member Variable

★ The variables defined within the scope of the class are member variables (member variables are attributes or member properties)

 

member variables fall into two categories:

Class Properties: Instance properties:

If you use the static modifier when defining a property, it is a class property, or an instance property (an instance is an object) if you do not use the static decoration .

Access class properties can use the class. Properties can also use objects. Properties

Access Instance properties can use an instance. Property

 

the difference between a class property and an instance property: The Class property is saved in the global data area, and the instance property is stored in the heap.

Local Variables

★ The variables defined within the scope of the method are local variables

 

Local variables are grouped into three categories:

formal parameters: Valid in the entire method

method local variable: In a method, from the beginning of the definition to the end of the method

code block local variable: In a code block, from the definition to the end of the code block

Case:

 

Summary: The difference between a local variable and a member variable

1. define the position of the variable in different local variables in the method member variable in class

2. save variable position different local variable in stack member variable in heap and global data area

Local variables (except formal parameters) must be initialized to use member variables to provide default values without initializing the system

 

Finally, I drew a picture to show their relationship.

 

 

 

 

 

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.