Class and object summary

Posted by Retired Bill on Sat, 05 Feb 2022 08:08:11 +0100

catalogue

1, Preliminary understanding of class

1. Basic understanding

2. Instantiation

2, Member of class

1. Field / attribute / member variable

2. Know null

3. static keyword

1. Modifier attribute

2. Modification method

3, Encapsulation

1. Definition

2. Access rights qualifier

3. getter and setter methods

4. Meaning of encapsulation

4, Construction method

1. Grammar rules

2. Precautions

3. this keyword

5, Recognize code blocks

6, Anonymous object

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

In short, object orientation is using code ( class ) A way to describe things in the objective world . A class mainly contains the attributes and behavior of a thing

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 by Define the keyword of the class, ClassName Is the name of the class, { } Is the body of the class. The elements in the class are called member attributes. The functions in the class are called member methods.
give an example:
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

       null stay Java Zhongwei " Null reference ", Indicates that no object is referenced
If yes null conduct . The operation will throw an exception

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:

        1
        1
        ============
        1
        2

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

(1) If the static keyword is applied to any method, this method is called a static method.
(2) Static methods belong to classes, not objects that belong to classes.
(3) You can call static methods directly without creating an instance of the class.
(4) Static methods can access static data members and change their values
(5) If a method is static decorated, non static data members cannot be accessed inside the 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

private/ public These two keywords represent " Access control " .
  • 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

The construction method is a special method , Use keywords new Automatically called when a new object is instantiated , Used to complete the initialization operation

1. Grammar rules

   1. The method name must be the same as the class name
   2. Constructor has no return value type declaration
   3. There must be at least one construction method in each class (if there is no explicit definition, the system will automatically generate a parameterless construction)

2. Precautions

(1) If no constructor is provided in the class, the compiler will generate a constructor without parameters by default
(2) If a construction method is defined in the class, the default parameterless construction will no longer be generated .
(3) The constructor supports overloading .

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
   } 
}

Topics: Java Class