object-oriented
object-oriented programming
Essence: organize code in the form of classes and organize (encapsulate) data with objects.
Characteristics: encapsulation, inheritance, polymorphism.
method
break: jump out of the switch and end the loop
Return: end the method. The result has been returned. The return value and return value type are the same
Parameter list: (parameter type, parameter name)
Method call:
Static method: called through (class name. Method) in main method
Dynamic method: instantiate this class
Object type object name = object value;
Student student= new Student();
student.say();
Examples of calling static and non static methods:
//Demo02 class public class Demo02 { public static void main(String[] args) { //Call of non static method //Instantiate this class //Object type object name = object value; Student student = new Student(); student.say(); //Call of static method Student.say1(); } } //Student class public class Student { //Non static method public void say() { System.out.println("Class is over"); } //Static method public static void say1() { System.out.println("Class is over"); } }
be careful:
//static is loaded with the class,
//public static void a() {b();} An existing call and a non-existent call will report an error
public void a() {b();}
//Class does not exist until it is instantiated
public void b() {}
Value passing and reference passing:
//Reference passing: object, essence or value passing public class Demo05 { public static void main(String[] args) { Person person = new Person(); System.out.println(person.name);//null Demo05.change(person); System.out.println(person.name);//Qin Jiang } public static void change(Person person) { person.name="xyz"; } } class Person{ String name; }
Relationship between class and object
Class: it is an abstract data type. It is the overall description / definition of a certain thing, but it cannot represent a specific transaction.
Object: a concrete instance of an abstract concept.
Example 1
Student class:
//Student class public class Student { //attribute String name; int age; //method public void study() { System.out.println(this.name+"I'm learning"); } }
Application class:
//There should be only one main method in a project public class Application { public static void main(String[] args) { //Class: abstract, instantiated //Class will return its own object after instantiation //The stu object is a concrete instance of the Student class Student xm= new Student(); Student xh= new Student(); xm.name="Xiao Ming"; xm.age=18; xh.name="Xiao Hong"; xh.age=16; System.out.println(xm.name+":"+xm.age); System.out.println(xh.name+":"+xh.age); } }
Parametric structure and nonparametric structure
Example:
public class Person { //Even if a class is not written, it will have a method //Display definition constructor String name; //Nonparametric construction method //1. Using the new keyword is essentially calling the constructor //2. Used to initialize values public Person() { } //Structure with parameters: once a structure with parameters is defined, a structure without parameters must display the definition //The first name refers to the upper String name; //The second name refers to the parameter passed in //heavy load public Person(String name) { this.name=name; } } /* //test public static void main(String[] args) { //new Instantiated an object Person person = new Person("kuangshen");//Call the parameterized construct in Person System.out.println(person.name); } */
Constructor:
characteristic:
1. Same as class name
2. No return value
effect:
1.new essentially calls the construction method again
2. Initialize the value of the object
Note:
1. After defining a parameterized structure, if you want to use a parameterless structure, a parameterless structure will be defined.
eclipse shortcuts:
1.alt + / generate parameterless constructor or promotion information
2.alt+shift+s+o generation zone parameter structure
Create object memory analysis
Summary:
1. Class and object
Class is a template, abstract
The object is concrete
2. Method:
definition
Call: static and dynamic
3. Reference of object:
reference type
Basic type: 8
Objects are operated by reference: stack - > heap
4. Attributes:
Fields, member variables
Default initialization:
Number: 0.0
char:u000
bolean:false
Reference type: null
Definition of attribute: modifier attribute type attribute name = attribute value;
5. Creation and use of objects
1) You must use the new keyword to create an object, and the constructor Person xyz = new Person();
2) Object properties: XYZ name
3) Object method: xyz,sleep();
6. Category:
Static properties
Dynamic behavior method