Java Basics

“This is the third day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.

1, private implementation of encapsulation processing

If you want to know about encapsulation, you first have to know what happens if there is no encapsulation?

Without encapsulating properties in a method, all methods can be accessed without permission after they have been called. However, after the encapsulation operation, when instantiating objects to access the method, there will be an unreachable problem.

TestDemo1.java:11: error: Name is in PersonprivateAccess control per.name ="Zhang";
                   ^
TestDemo1.java:12: error: age is in PersonprivateAccess control per.age =13;
                   ^
2A mistakeCopy the code

To solve this problem, we need to use getters and setters to assign and value.

class Person{
    private String name;
    private int age;
    public void setName(String name){
        this.name = name;
    }
    public String getName(a){
        return name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public int getAge(a){
        return age;
    }
    public void info(a){
        System.out.println("name = "+ name +" \nage = "+ age); }}public class TestDemo1{
    public static void main(String args[]){
        Person per = new Person();
        per.setName("Zhang");
        per.setAge(13); per.info(); }}Copy the code

The biggest feature of private implementation encapsulation is that only this class is allowed, and no external class is allowed to access

Private is only the first step in encapsulation. If you want to understand it, you need to learn about polymorphism and inheritance.

Constructors and anonymous objects

Constructors are called when a new object is instantiated using the keyword new, but the following principles apply to constructors

  • The method name must be the same as the class name, and the constructed method must have no return value. There must be at least one constructor in each class. If no constructor is explicitly defined in the class, if no constructor is defined in the class, a constructor with no arguments is automatically generated
  • The constructor is executed when the object is new

Question: Since there is no data returned in the constructor, why not use void capsules?

Now the composition of the class: properties, common methods, constructors

Property is used to make space available when an object opens up heap memory

The constructor is called with the keyword new

Normal methods are called after new instantiates the object

The function of the construction method:

  • The constructor is invoked almost synchronously with the object’s memory allocation, so you can use the constructor to set the property contents of the class, and the constructor can initialize the properties of the class
  • By setting the content with the constructor, you can actually avoid repeated setter calls
  • Setter methods can also modify data in addition to setting content.
  • Since the constructor itself is a method, the method must be overloaded, and overloading a constructor is easier because the method name is the class name, so all you can do is implement different types and numbers of arguments.

When defining several constructors, order them in ascending or descending order according to the number of arguments.

class Person{
    private String name;
    private int age;
    public Person(a){}
    public Person(String name){
        name = name;
    }
    public Person(String name, int age){
        name = name;
        age = age;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(a){
        return name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public int getAge(a){
        return age;
    }
    public void info(a){
        System.out.println("name = "+ name +" \nage = "+ age); }}public class TestDemo1{
    public static void main(String args[]){
        Person per = new Person("Bill".80);
        per.setName("Zhang");
        per.setAge(13); per.info(); }}Copy the code

Simple Java classes

class Emp{
    private int empno;
    private String ename;
    private String job;
    private double sal;
    private double comm;
    public Emp(int no,String name,String j,double s,double c){
        setEmpno(no);
        setEname(name);
        setJob(j);
        setSal(s);
        setComm(c);
    }
    public void setEmpno(int no){
        empno = no;
    }
    public void setEname(String name){
        ename = name;
    }
    public void setJob(String job){
        job = job;
    }
    public void setSal(double s){
        sal = s;
    }
    public void setComm(double c ){
        comm = c;
    }
    public int getEmpnoP(a){
        return empno;
    }
    public String getEname(a){
        return ename;
    }
    public String getJob(a){
        return job;
    }
    public double getSal(a){
        return sal;
    }
    
    public double getComm(a){
        return comm;
    }
    public String getInfo(a){
        return "empno = " + empno + "\n" +
                "ename = " +ename + "\n" +
                "job = " +job + "\n" +
                "sal = " +sal + "\n" +
                "comm = "+comm ; }}public class TestDemo2{
    public static void main(String args[]){
        / / test class
        System.out.printf(new Emp(2021."Joker"."CEO".2000.0.0.0).getInfo()); }}Copy the code

4, arrays,

Array is a reference type, so be sure to open up space before using it. In addition to declaring and opening up space, there is another open up mode, which uses our distribution mode to open up array space. NullPointerException occurs if you use arrays without space.

public class TestDemo3{
    public static void main(String args[]){
        // The first way to declare
        int [] num = new int [3];
        // The second way to declare an array
        int [] num = null;
        num = new int [3];
        num[0] = 0;
        num[1] = 1;
        num[2] = 2;
        for(int i = 0; i < num.length ; i++){ System.out.println(num[i]); }}}Copy the code

Arrays will definitely be used in the development process, but there are fewer arrays as described in some of the actual development, in the future, we will use the concept of arrays more, and use arrays directly 99% of the time just for the array loop.