Java - about classes and objects

1. Preliminary cognition

C Language is**Process oriented**(Pay attention to the process. The behavior involved in the whole process is the method), analyze the steps to solve the problem, and solve the problem through function call.
Java Is based on**object-oriented**(The focus is on objects. Objects are the subjects involved in the process, which are connected one by one through logic. A thing is divided into different objects, which is completed by the interaction between objects.

Class is the general name of a class of objects, and objects are the concrete implementation of this class.
In short, object-oriented is a way to describe things in the objective world with code.

2. Classes and instantiation of classes

Class is the general name of a class of objects, and objects are the concrete implementation of this class.
Simple example: moon cake mold (class) moon cake (object)
A class is a template, and an object is a sample. There are countless objects in a class.
Declaring a class is to create a new data type, and the class is a reference type in Java. In Java, class is used to declare the class

1. Class instantiation

A class is a model that can instantiate multiple objects.

2. Members of the class

Class members include fields, methods, code blocks, internal classes and interfaces.

2.1 field / attribute, member variable (one meaning)


Use "." Access the object. If the access is null, it will cause null pointer exception.

Since the initial value is not set, the number type defaults to 0; boolean type is false by default; The reference type is null by default;

Field local initialization


Output result:

2.2 method


Execution result:

2.3 static keyword

a. Modifier attribute

Different real columns of the same class share a static attribute

b. Modification method

You can call static methods directly without creating an instance of the class
Static methods can access static data members and change static data, but they cannot directly call non static data members and non static methods.

Output:

3. Packaging

public and private denote "access rights"
·public can be used directly by the caller of the class
·private cannot be used directly by the caller of a class
Use show to access private

class Person {
   private static  int age=18;//Member variable
    private static String name="Zhang San";
    private static String sex="male";
    public void show(){
        System.out.println("My name is"+name+",this year"+age+"year");
    }
}
class Main{
    public static void main(String[] args) {
        Person person=new Person();
        person.show();


    }
}

Compilation result:

My name is Zhang San. I'm 18 years old

getter and setter

·Get and set

class Person {
   private static  int age=18;//Member variable
    private static String name="Zhang San";
    private static String sex="male";
   public void setAge(int age){
       this.age=age;//this must be used to represent the object that calls the method

   }
   public int getAge(){
       return age;
   }
    public void show(){
        System.out.println("My name is"+name+",this year"+age+"year");
    }
}
class Main{
    public static void main(String[] args) {
        Person person=new Person();
        person.show();
        person.setAge(280);
        person.show();


    }
}```

```java
 Output result:
My name is Zhang San. I'm 18 years old
 My name is Zhang San. I'm 280 years old

4. Construction method

·Constructor is a special method, which will be called automatically when instantiating an object with new
The method name must be the same as the class name
No return value declaration
There is at least one in each class. If there is no definition, a parameterless construct will be automatically generated

class Person {
    private static  int age=18;//Member variable
    private static String name="Zhang San";
    private static String sex="male";
    public Person(){
        this.name="Galen";
        this.age =28;
    }//Default constructor construction object
    public Person(String name,int age,String sex){
        this.name=name;
        this.age=age;
        this.sex=sex;


    }
   public void show(){
       System.out.println("name:"+name+";Age:"+age+";Gender:"+sex+".");
}
}
class Main{
    public static void main(String[] args) {
       Person person1=new Person();
        person1.show();
       Person person2=new Person("Zhang Sanfeng",56,"male");
        person2.show();
    }
}


Output:
name:Galen;Age: 28;Gender: Male.
name:Zhang Sanfeng;Age: 56;Gender: Male.

this indicates the current object reference (note that it is not the current object)

5. Code block

A piece of code defined with * * {} * * is a code block
·Normal code block (code block defined in method)

·Member initialization block (also called instance code)
Takes precedence over constructor execution

·Static code block (code block defined by static, which is generally used to initialize static member attributes)

class Person{
    private String name;//Instance member variable
    private  int age;
    private  String sex;
    private static int count;//Static member variable
    //Instance code block
    {
        this.name="Zhang San";
        this.age=12;
        this.sex ="male";
    }
    //Static code block defined by static, which statically initializes the static member variable count
     static{
        count=9;
        System.out.println(count);
     }
     public void show(){
         System.out.println("name:"+name+" age:"+age+" sex:"+sex);
     }
}
 class Main{
     public static void main(String[] args) {
         Person person=new Person();
         person.show();
         Person person1=new Person();
         person1.show();
     }
}
Output result:
9
name:Zhang San age:12 sex:male
name:Zhang San age:12 sex:male

No matter how many objects are generated from static code blocks, they will be executed only once and first
The execution order is: static code block - instance code block - constructor

6. Supplement

6.1 toString method

Convert object to string

class Person{
    private String name;//Instance member variable
    private  int age;
    private  String sex;
    //Instance code block
    {
        this.name="Zhang San";
        this.age=12;
        this.sex ="male";
    }
    
     public void show(){
         System.out.println("name:"+name+" age:"+age+" sex:"+sex);
     }
//Convert object to string form
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}
 class Main{
     public static void main(String[] args) {
         Person person=new Person();
         person.show();
         System.out.println(person);
     }
}
Operation results:
name:Zhang San age:12 sex:male
Person{name='Zhang San', age=12, sex='male'}

The toString method will be called automatically when prinfln.

6.2 anonymous objects

class Person{
    private String name;//Instance member variable
    private  int age;
    private  String sex;
    private static int count;//Static member variable
    public Person(String name,int age,String sex){
        this.name=name;
        this.age=age;
        this.sex=sex;
    }
    public void show(){
        System.out.println("name:"+name+" age:"+age+" sex:"+sex);
    }
}
 class Main{
     public static void main(String[] args) {
         new Person("Zhang San",19,"male").show();//Anonymous object calls show method


     }
}

Operation results:
name:Zhang San age:19 sex:male

over!!!!

Keywords: Java

Added by kpulatsu on Tue, 08 Mar 2022 21:27:27 +0200