Java Object-oriented
1. Defining classes
2. Creating objects, calling methods
Classes and objects:
A conceptual definition of a class of objects.
For example: human-abstract concept (not specific to someone)
Object- An instance produced under the concept of a class, which is an object.
In a Java program, A class is the smallest program unit -all Java programs must start with a defined class
to define the syntax format for a class :
[modifier] class name {
0 ~ N Field Definitions
0 ~ N Method definitions
0 ~ N Constructor Definitions
}
There can be up to five components in a class: Field, Method, constructor
The most common components of a class are:Field, Method, and constructor
[modifier]: Can be omitted, can be public|final|abstract. Only three or omitted
Class Name: Custom fill in the blanks, from the grammatical point of view as long as the identifier can be
rule constraint: A class name is made up of one or more meaningful words (the first letter of each word is capitalized ), and the class name allows you to know what the class is doing.
define Field The syntax format :
[Modifier] Type field name [= default value];
[modifier]: Can be omitted, can also be public|protected|private,static,final. Transient-serialization used to
Type: cannot be omitted, can be a primitive type, an array, any class (JDK class, custom class, interface, enumeration)
Field name: cannot be omitted, rule constraint: The class name is composed of one or more meaningful words (the first letter is lowercase, followed by the first letter of the word).
[= default]: Can be omitted, the default value must be the same as the type it declares (Java is a strongly typed language)
Defines the syntax format for a method:
[modifier] Returns a value type method name ([0~n parameter declaration]) {
0 ~ N Executable Statements
If there is a return value type declaration in the method signature, the method must contain a valid return statement
}
[modifier]: Can be omitted, can be public|protected|private,static,final,abstract (abstract), abstract and final cannot appear simultaneously
Return value type: cannot be omitted, can be primitive type, array, void (declares that the method has no return value), any class (JDK class, custom class, interface, enumeration)
Method Name: cannot be omitted, stipulation constraint: The class name is composed of one or more meaningful words (first letter to lowercase, after the first letter capitalized), through the method name to know what the method is to do-should be a verb
Parameter declarations: each formal parameter declaration satisfies the format of "type variable", separated by "," between multiple parameter declarations
Method Body: Code in the method body must be executable code, code execution is from top to bottom
Defines the syntax format for the constructor:
[modifier] Constructor name (0 ~ n parameter declaration) {
0 ~ N Executable Statements
}
[modifier]: Can be omitted, can be public|protected|private
Constructor Name: cannot be omitted, must be the same as the class name
Parameter declarations: each formal parameter declaration satisfies the format of "type variable", separated by "," between multiple parameter declarations
Constructor execution body: The executor must be executable code, code execution is from top to bottom
The role of the constructor:
Constructors can be thought of as a special method, but the role of constructors is to produce objects .
Popular to: But after we define a class, we need to get an instance of the current class through the constructor .
Note : If the program does not provide any constructors for the class, the system provides a parameterless constructor for the class
Local variables: Local variables , formal parameters, and local variables of code blocks in a method
Only modifiers before local variables : Final
Local variables must be assigned to the initial value by the programmer
Instance:
public class apple{
Field definition: [modifier] Type field name [= default value];
private int age;
protected String name;
Protected static string[] subs;
Double weight = 23.3;
int grade;
Grade = There are only five components in the 2;//class, it is not field, nor method, constructor, so it is wrong
/*
[modifier] Returns a value type method name ([multiple parameter declarations]) {
0 ~ N Executable Statements
If there is a return value type declaration in the method signature, the method must contain a valid return statement
}
*/
String SayHello (string name) {
// assignment statements are execution statements that can be placed inside a method
The return value type is string, so you must include a valid return statement
Grade = 2;
Return name + ", Hello";
/*
At compile time, if is just a Boolean expression
The system considers that the return statement in the IF may not be executed, so the return is not valid
Unless you add else return to be valid,
Ensure that at least one statement in the compile phase can actually execute to be a valid return statement
if (true) {
Return name + ", Hello";
}
*/
}
}
Public class Testapple {
Public static void Main (string[] args) {
//apple is a local variable defined within a method
Apple Apple;
You must first assign an initial value before using a local variable
//system.out.println (apple);
behind the//new is a constructor that creates a Testapple object
//and have Apple reference variable point to Testapple object
//testapple objects are stored in the heap.
//apple is defined in the main method, so the Apple reference variable is stored in the main method stack
Apple = new Apple ();
//To invoke the method must satisfy each other's requirements (formal parameters)
System.out.println (Apple.sayhello ("Golden Mountain"));
//Direct Print object, the output must be "class name @hashcode value"
System.out.println (apple);
}
}
Crazy Java Learning Notes Object-oriented-define classes, methods, constructors