Beginners understand the basic knowledge of java classes objects and encapsulation

  • 2021-10-24 22:49:22
  • OfStack

Directory 1, Class 2, Object 3. Method Overload What is Method Overload 4, Constructor What is Constructor Method Constructor Syntax 5. this this refers to the current object, that is, which object is called to refer to which object 6. How to encapsulate the role of encapsulation

1. Class

What is a class

Class is a collection of entities with some common characteristics, and it is an abstraction of entities with the same characteristics.

Basic syntax for defining classes in Java

Access modifier class class name {}


public class Student{
    // Defines the 1 Student class 
}

Member variable

A member variable is a property owned by an object

Basic grammar

Access modifier [optional modifier] data type variable name;


public class Student{
    // Defines the 1 Student class 
    public int age;
    String name;
    protected long tel;
    private double a;
}

Method

Placement can be used to describe object actions

Basic grammar

Access modifier [optional modifier] data type method name (parameter list) {method body};


public class Student{
    public void print(){
       System.out.print(" Method ") 
    }
    public int sum(int a, int b){
        return a + b;
    }
}

2. Objects

What is an object

An object is an instance of a class

Object creation

Basic grammar

Type object name = new type ();


public class StudentTest{
    public static void main(String[] args){
        Student stu = new Student();
    }
}

3. Method overload What is a method overload

Method overloading means that there are multiple methods with the same name in one class;

These methods follow the following rules

Same method name The parameter list of the method is different 1) The number of parameters is different 2) Parameter types are not identical, including data types and the order of data types 3) Independent of parameter name Independent of the return value type and access modifier of the method

public class Student{
    /**
    * Definition 1 The summation method sums the value of the passed parameter and returns the result 
    */
    public int sum(int a,int b){
        return a + b;
    }
    // The number of parameters is different 
    public int sum(int a,int b,int c){
        return a + b + c;
    }
    // Different types of parameter lists 
    public int sum(int a,short b){
        return a + b;
    }
    // Parameter list types are in different order 
    public int sum(short a,int b){
        return a + b;
    }
}

4. Constructor What is a construction method

The structure of the constructor is similar to that of the ordinary method, but two code blocks completely different from that of the ordinary method; A constructor is also a method, which means that when a constructor is used, the method executes and completes the function Constructor is a block of code "is a structure, and constructor and field methods are both members of a class The constructor should also exist in the structure of the class

Constructor syntax:

The name of the constructor and the class name are completely 1 (including the case is completely 1) There is no return value type (void is not acceptable) Constructor method body cannot return any value (that is, return value cannot be in method body) Others, similar to ordinary methods, can have modifiers ( public、protected、private、默认 ), you can have a tangible list of parameters Constructor must have method body Constructors cannot be decorated with any non-access modifiers, such as static、final、synchronized、abstract Can't modify the constructor, etc.

Note: new Student() This whole is both a Student Object, which is the invoked Student Parameterless construction method based on

Characteristics of construction method

1. Features of constructors in classes:

(1) There is at least one construction method in every class; (2) If it is not seen (displayed), there is an implicit parameterless construction method; If there is an explicit constructor in a class, then there is no implicit parametric constructor;

The role of construction methods

Assign values to member variables of objects while creating them (initialization work)

Template syntax


public class  Class name {
 Member variable   ( Field ); 
 Construction method (with and without parameters) ;
 Method  ( Specific 1 Individual functional behavior ); 
}

public class Student{
    public String name;
    public int age;
    /**
     Parametric structure 
    */
    public Student(){}
     /**
     Parametric structure 
    */
    public Student(String name,int age){
    }
}

5. this this refers to the current object, that is, which object is called

this**** Purpose:

Solve the duality of local variables and member variables Between this class, mutual calls between constructors this () calls constructors without parameters, and this (…) can add parameters to mean calling constructors with parameters this is passed as a parameter and this as a return value

public class Student{
    public String name;
    public int age;
    /**
     Parametric structure 
    */
    public Student(){}
     /**
     Parametric structure 
    */
    public Student(String name,int age){
        this();// Calls the constructor, which can only be written in the 1 Sentence 
        this.name = name // Invoke property 
    }
}

Step 6 Package

Encapsulation: Refers to privatizing members of a class that do not want to be accessed by the outside world.

The role of packaging

Encapsulation is to protect the security of internal data:

1. You do not want random access to an object's member variables in an external class

2. Only those who meet the permission requirements can access

How to package

1. How do I control access in my program?

Encapsulation (access control) by adding access modifiers to members (fields, methods, constructors) in a class

2. What is access: Simply think that access means that people at different levels can do things at different levels, and people at different levels can see different pages

Steps

1. Privatize member variables (modify member variables with private) 2. For every 1 member variable, provide a reasonable **getXxx () ** method gets the value of the member variable and changes getXxx () to isXxx () if the current member variable type is boolean **setXxx (...) ** Method sets the value of a member variable 3. Provide a parametric construct and a parametric construct 4. The class is modified with public

public class Student{
    private String name;
    private int age;
    public String getName(){
       return this.name;
    }
    public void setName(String name){
        this.name = name;
    }
      public String getAge(){
       return this.age;
    }
    public void setAge(String age){
        this.age = age;
    }
}

Summarize

That's all for this article. I hope I can help you, and I hope you can pay more attention to this site!


Related articles: