catalogue
1, Preliminary understanding of class
1. Field / attribute / member variable
1, Preliminary understanding of class
1. Basic understanding
Java is based on object-oriented and focuses on objects. It divides one thing into different objects and completes it by the interaction between objects.
Object oriented focuses on the object, that is, the subject involved in the process. It is to connect each function realization through logic
2. Instantiation
The process of creating objects with class types is called class instantiation
Class is a general term for a class of objects. Object is an instance of this kind of materialization.
Class is equivalent to a template, and the object is the sample generated by the template. A class can produce countless objects.
//Create class
class < class_name >{
field; / / member properties
method; / / member method
}
//Instantiate object
< class_ Name > < object name > = new < class_ name >;
class Person { public int age;//Member property instance variable public String name; public String sex; public void eat() { //Member method System.out.println("having dinner!"); } public void sleep() { System.out.println("sleep!"); } } public class Main{ public static void main(String[] args) { Person person = new Person(); //Instantiate objects through new person.eat(); //Member method calls need to be called through the reference of the object person.sleep(); //Generate object # instantiate object Person person2 = new Person(); Person person3 = new Person(); } }
be careful:
- The new keyword is used to create an instance of an object
- use. To access properties and methods in an object
- You can create multiple instances of the same class
2, Member of class
1. Field / attribute / member variable
class Person { public String name; // field public int age; } class Test { public static void main(String[] args) { Person person = new Person(); System.out.println(person.name); System.out.println(person.age); } }
- use. Access the fields of the object
- Access includes both read and write
- For the field of an object, if the initial value is not explicitly set, a default initial value will be set
Default value rule:
For various numeric types, the default value is 0
For boolean type, the default value is false
For reference types, the default value is null
2. Know null
3. static keyword
1. Modifier attribute
class TestDemo{ public int a; public static int count; } public class Main{ public static void main(String[] args) { TestDemo t1 = new TestDemo(); t1.a++; TestDemo.count++; System.out.println(t1.a); System.out.println(TestDemo.count); System.out.println("============"); TestDemo t2 = new TestDemo(); t2.a++; TestDemo.count++; System.out.println(t2.a); System.out.println(TestDemo.count); } }
The output result is:
count is modified by static and shared by all classes. And it does not belong to the object. The access method is: class name Attributes
2. Modification method
3, Encapsulation
1. Definition
At the syntax level, both fields and methods are modified by private. At this time, it is said that the field or method is encapsulated
2. Access rights qualifier
- Member variables or member methods modified by public can be directly used by class callers
- The member variable or member method modified by private cannot be used by the caller of the class
- private can modify not only fields, but also methods
3. getter and setter methods
If you need to get or modify this private property, you need to use the getter / setter method
class Person { private String name;//Instance member variable private int age; public void setName(String name){ //name = name;// You can't write that this.name = name;//this reference represents the object that calls the method } public String getName(){ return name; } public void show(){ System.out.println("name: "+name+" age: "+age); } }
- getName is the getter method, which means to get the value of this member
- setName is the setter method, which means to set the value of this member
-
stay IDEA Can be used in alt + insert ( perhaps alt + F12) Fast generation setter / getter method . stay VSCode You can use the right mouse button menu - > Automatically generated in source code operation setter / getter method .
4. Meaning of encapsulation
(1) safety
(2) reduce the use cost of the class
4, Construction method
1. Grammar rules
2. Precautions
3. this keyword
This represents the current object reference (note that it is not the current object) You can use this to access the fields and methods of the object
5, Recognize code blocks
A piece of code defined with {}
1. Common code block
Code blocks defined in methods
2. Tectonic block
A block of code defined in a class (without modifiers). Also known as: instance code block. General member code used to initialize an instance
Instance code blocks take precedence over constructor execution
3. Static block
Code blocks defined using static. Generally used to initialize static member properties
- No matter how many objects are generated, the static code block will only be executed once and will be executed first.
- After the static code block is executed, the instance code block (construction block) is executed, and then the constructor is executed.
6, Anonymous object
Anonymity simply means an object without a name
1. Objects that are not referenced are called anonymous objects
2. Anonymous objects can only be used when creating objects
3. If an object is used only once and does not need to be used later, consider using anonymous objects
Code example:
class Person { private String name; private int age; public Person(String name,int age) { this.age = age; this.name = name; } public void show() { System.out.println("name:"+name+" " + "age:"+age); } } public class Main { public static void main(String[] args) { new Person("caocao",19).show();//Calling methods through anonymous objects } }