What is an object?
In Java, all people, things, or modules are an object.
The same object has some same features.
Dog, cat, and Snake (animal object)
Apple, pear, and orange (fruit object)
What is a class?
Abstract objects in real life
This abstract data type is called a class.
Animal (animal)
Fruit (fruit)
Relationship between classes and objects
A class is an object template (abstract representation), and an object is an instantiation of A Class (concrete presentation)
Class Structure
Java is purely object-oriented (except for eight basic data types)
Objects are generated from classes. Therefore, classes are the most basic and core components of Java programs.
Element.
Variable (attribute, member variable)
Method (function, member method, responsibility)
Message (communication between objects and transmission between object data)
Attribute Definition
Attribute Definition
Attribute Initial Value (default)
Booleanfalse
Byte/short/INT/long 0
Float 0.0f
Double 0.0d
Char '\ u0000'
Object Type null
Classification and scope of Variables
Class variable: The Class Object belongs to the object. Static modified variables are called class variables.
Static zone. It is instantiated only once, so it occupies only one memory.
Object member variable: The member variable belongs to the object.
Heap area.
Local variable. The variables defined in the local method and the formal parameters are all local variables.
Stack area.
Variable scope (lifecycle)
The time when different types of variables survive in the memory.
Classification of methods:
Divided by return values:
Return Value
No return value
Divided by parameters:
Parameter Method
No Parameter Method
Void sum (int x, int y): separated by commas.
Sum (x, y); // calls are also separated by commas.
Real parameters (actual parameters)
Parameters ).
When passing parameters, there are two ways to pass the real parameters to the form parameters:
A: if the transfer is a basic data type, it is just a copy of the value.
B: If the object type is passed, the object address is passed.
By Function
Constructor
Instance method (Object method, member method)
Class Method
Constructor:
Is the operation that must be performed on the instantiated object.
This API is used to initialize a new object.
If no constructor is created, the system automatically creates an empty constructor.
If there is a constructor, the system will not build any more.
/*** Define a student class */public class student {// part of the attribute declaration int stu_id; char stu_sex; string stu_name; // class variable static string class_id = "jn1001"; // method with return value int getscore () {// defines the local variable int English = 0; return English ;} int getscope2 () {double math = 12.345; Return (INT) math;} // void Study () {system. out. println ("Everyone is learning Java");} public static void main (string ARGs []) {student Stu = new student (); Stu. study (); int result = Stu. getscore (); int result2 = Stu. getscope2 (); system. out. println ("result =" + Result + ", result2 =" + result2 );}}
Public class student_method {// constructor public student_method () {system. out. println ("object constructor");} // object (member) method int divde (int x, int y) {return x/y ;} // class method static int sum (int x, int y) {return X + Y;} public static void main (string ARGs []) {// call the class method student_method.sum (2, 3); // call the member method // student_method.divde (2, 3); // The system cannot be called. out. println ("1111"); student_method test = new student_method (); system. out. println ("222"); test. divde (3, 4 );}}