Java Basics - 01 Object-Oriented

 

object-oriented way of thinking is a habit of thinking more in line with people's ideas

process-oriented way of thinking is reflected in more executives (their own thing), object-oriented is more a manifestation of the commander (command object is to do things).

 

1. Classes and Objects

1) by describing the code, to know the true meaning of class is to describe things. Properties and functions are collectively referred to as members of things.

 

Members divided into two things: member properties and member functions.

 

Member property embodied in the code is a member variable

 

Member function embodied in the code is a member method

Under review to create an object format:

2) class name object name = new class name ();

The difference between classes and objects that: class is an abstract description of a certain class of things, but in reality the individual objects used to represent the class of things

3) member variables and local variables

 The difference is:

Define different positions: a difference

 

Variables defined in the class member variables

 

The method defined in or {} statement inside the variables are local

 

Difference between the two: a different location in memory

 

Member variables are stored in the heap memory object

 

In the method of local variables are stored in the stack memory

 

Three differences: the life cycle of different

 

With the advent of the member variable objects out of the heap now, with the disappearance of the object disappeared from the heap ( gc garbage collection )

 

With the local variables of the method now run out of the stack, with the popping method disappear

 

Distinguish four: different initialization

 

Because the member variables in heap memory, all the default initialization value

 

Local variables are not the default initialization values, you must manually assign it a value before you can use.

 

 

 2. Package

    The method of the package to a class and a method of packaging into specific functions, in fact, these are packages.

   Package, it is also one of the features of object-oriented. There are three characteristics of object-oriented: encapsulation, inheritance, polymorphism.

 

Packaging performance:

 

The method is a basic package.

 

Class is actually a package.

 

Concluded from the above two points, the package benefits:

 

Improved reusability of code.

 

Hide implementation details, but also to provide external access ways . Easy to use caller. This is one of the core can also be understood as is the concept of the package.

 

Improves security.

 

 

  1) private private

    Sometimes we describe a class, although the description has been clear, but it can be accessed with the use of random people, which is not in line with actual demand,

  Therefore java provides a modifier, Private (private, authority modifier), so that it can not be arbitrary class private visit.

    We also offer a way to access the external, the method as long as you can provide external access, allowing other programs to access these methods. At the same time the data can be verified in the process.

 General access to the action of member properties: assignment ( setting the SET) , value ( acquisition GET) , and therefore the way to access the private variables can provide the corresponding setXxx or getXxx methods.

 

Content class is not required to provide both were privatized, including properties and methods.

 

Later describe things, properties are privatized, and to provide setXxx getXxx method to access it.

 

  E.g:

class Person {

 

// private member variables

 

private int age;

 

private String name;

 

 

 

// provide external set of member variables method

 

public void setAge(int a) {

 

// Because it is a member variable settings, where you can add a verification data

 

if (a < 0 || a > 130) {

 

. The System OUT .println (A + " does not meet the age range data " );

 

return;

 

}

 

age = a; 

 

}

 

 

 

// provide external access to member variables of the method

 

public void getAge() {

 

return age;

 

}

 

}

 

 2) this keyword

             When there has been in the process of local variables and member variables of the same name, when then how do distinguish member variables local variables in the method?

  We can add the member variable name in front of this. Distinguished member variables and local variables. this modification is a member variable

 Example: to determine whether the two peers

class Person {

 

private int age;

 

private String name;

 

 

 

public int getAge() {

 

return age;

 

}

 

 

 

public void setAge(int age) {

 

this.age = age;

 

}

 

 

 

public String getName() {

 

return name;

 

}

 

 

 

public void setName(String name) {

 

this.name = name;

 

}

 

 

 

public void speak() {

 

System.out.println("name=" + this.name + ",age=" + this.age);

 

}

 

 

 

// determine whether peers

 

public boolean equalsAge(Person p) {

 

// use the current call equalsAge method object age and passed in p of age to compare

 

// Unable to determine which specific object call equalsAge method, where you can use this instead

 

/*

 

 * if(this.age == p.age) { return true; } return false;

 

 */

 

return this.age == p.age;

 

}

 

}

 

   

 

  

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/sunlangui/p/11456983.html