Concepts, classes, objects, and reference and member methods for object-oriented programming of two-dimensional arrays

Source: Internet
Author: User
Tags class definition

1. Two-dimensional array (familiar)
1.1 Basic concepts
A one-dimensional array is essentially a contiguous storage unit that holds multiple types of data content.
A two-dimensional array is essentially an array of one-dimensional arrays, meaning that each element in a two-dimensional array is a one-dimensional array, and each element of a one-dimensional array is the exact data content.

1.2 Declaration of two-dimensional arrays
(1) syntax format
data type [] Array name = new data type [number of rows] [number of columns];
Such as:
int[][] arr = new INT[2][5]; -Declares a two-dimensional array with 2 rows and 5 column element type int
Where the row subscript range is: 0 ~ 1;
The column subscript range is: 0 ~ 4;

Thinking:
What does arr represent? What does arr[0] stand for? What does arr[0][0] stand for?
Analytical:
Arr represents the name of a two-dimensional array;
Arr[0] represents the first row in a two-dimensional array, which is a one-dimensional array;
Arr[0][0] represents the data content of the first column of the first row in a two-dimensional array;

Arr.length represents the length of a two-dimensional array, that is, the number of elements in a two-dimensional array, that is, the number of rows;
Arr[0].length represents the length of the first element in a two-dimensional array, that is, the number of columns in the first row;

(2) Initialization of elements
data type [] Array name = {{value 1, value 2,...}, ...};
Such as:
Int[][] arr = {{1, 2, 3}, {4, 5, 6}}; -Declares a two-dimensional array with 2 rows and 3 columns

2. The concept of object-oriented programming (comprehension, difficulty)
2.1 What is an object?
All things are objects!

2.2 What is object-oriented?
Object-oriented refers to the way of analyzing things in the real world in the view of attributes/characteristics and behaviors.

2.3 What is object-oriented programming?
Object-oriented programming refers to an object-oriented approach to the analysis, and then an object-oriented programming language for the translation process.
The C language is a process-oriented programming language.
The C + + language is a process oriented and object oriented programming language.
Where the Java language is a purely object-oriented programming language.

2.4 Why is object-oriented programming required?
Object-oriented programming is the demand of the development of software industrialization.

2.5 How to learn object-oriented programming
Deep understanding of the three main features of object-oriented programming: encapsulation, inheritance, polymorphism.

3. Classes, objects, and references (the heavy weight)
Such as:
String name1 = "Zhang San"; ... ...
String name2 = "John Doe"; ... ...
String Name3 = "Harry"; ... ...
String name4 = "Zhao Liu"; ... ...
... ...

Human:
Characteristics: Name, age
Behavior: Eating, entertaining

3.1 Concepts of classes and objects
object is an objective thing in real life, represented in the Java language as a piece of memory space.
Classes are simply "classifications", which are abstract descriptions of the commonalities of the same kind of things, called reference data types in the Java language, including the member variables used to describe the features and the composition of the member methods used to describe the behavior.

Definition of Class 3.2
(1) syntax format for class definitions
Class Name {
Class body;
}
Such as:
Class person{
}
Attention:
In general, if the class name consists of multiple words, the first letter of each word is required to be capitalized.

(2) syntax format for member variable declarations
Class Name {
Data type member Variable name = initial value; -where = The initial value can be omitted, but cannot be omitted
}
Such as:
Class person{
String name;
int age;
}
Attention:
In general, if a member variable name consists of multiple words, the first letter of the second word is required to be capitalized.
Extended:
Local variable-mainly refers to a variable declared within the body of the method, which is valid in the current method body;
Member variable-refers to a variable that is declared within the body of the method, which is valid throughout the class body;

3.3 Creation of objects
(1) syntax format
New class name ();
Such as:
New Person (); -Represents an object that creates a person type.

(2) How to use
When the class definition is complete, you can use the New keyword to create/construct the object, which is called instantiation of the class.
The essence of creating an object is to request a piece of storage space in memory to record the member variable information unique to that object.

3.4 Referenced declarations
(1) Basic concepts
Variables declared with reference data types in the Java language are called reference variables, referred to simply as references.
It is mainly used to record the address information of an object in memory space for easy access again.

(2) syntax format
The class name refers to the variable name;
Such as:
Person p;
Person p = new person ();

Reference variable name. member variable name;
Such as:
P.name = "Zhangfei";
System.out.println ("p.name =" + p.name); Zhangfei

Practice:
The custom point class, characterized by: horizontal and Vertical (integer), requires that the object of the class be created in the main () method, then the corresponding horizontal ordinate information is printed, then the horizontal ordinate is modified to 3 and 5, and the horizontal ordinate information is printed again.

4. Member method (the most serious)
4.1 Syntax format
Class Name {

Return value type member method name (formal parameter list) {
Method body;
}
}
Such as:
Class person{
void Eat () {
System.out.println ("Open to eat!" ");
}
}
Attention:
Typically, when a member method name consists of multiple words, the first letter of the second word is required to be capitalized.

4.2 Explanation of the method
(1) Return value type
The return value mainly refers to the data content returned from the method body to the method body;
The return value type primarily refers to the data type of the return value, either the base data type or the reference data type;
Such as:
When the return value content is 66 o'clock, the return value type writes int type;
When the return value content is 3.14, the return value type is written in double type;
When the return value content is "Hello", the return value type is written in string type;
Returns the data content in the method body using the return keyword and ends the current method.
Such as:
When the return value content is 66 o'clock, the method body is written in: return 66;
When the return value content is num, the method is written in the body: return num;
When no data in the method body needs to be returned, the return value type is write void;

(2) formal parameter list
Formal parameters are primarily used to bring data content outside the method body into the body of the method, in the syntax format: data type parameter name
The formal parameter list is primarily used to bring in multiple data content in the form of data type parameters 1, data type argument 2,...
Such as:
When the incoming data content is 66 o'clock, the formal parameter list is written: int I.
When the incoming data content is 3.14, the formal parameter list is written: Double D.
When the incoming data content is "Hello", the formal parameter list is written: String S.
When the incoming data content is 66 and 3.14, the formal parameter list is written: int i, double d.
When the incoming data content is 66 and "Hello", the formal parameter list is written: int i, String S.
When no data content needs to be passed in, the parameter list position is not written.

(3) Method body
A block of statements that describes the functionality of the method is primarily written in the method body.

4.3 Invocation of Member methods
(1) syntax format
Reference. Member method name (argument list);
Such as:
P.eat ();

(2) How to use
The actual parameter list is primarily used to initialize/assign operations to the formal parameter list of the method, so the number, type, and order of the argument list must be consistent with the formal parameter list.
Actual parameters can pass literals, variables, expressions, and invocation of methods.

Concepts, classes, objects, and reference and member methods for object-oriented programming of two-dimensional arrays

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.