JAVA constructor / constructor and this usage

Keywords: Java Eclipse

Construction method / constructor

● basic grammar

```java
·[Modifier ]Method name(parameter list ){
Method body;
}
```

1. The modifier of the constructor can be default or public protected private

2. The constructor has no return value

3. The method name and class name must be the same

4. The parameter list has the same rules as the member method

5. The calling system of the constructor is completed

Basic introduction

1. The method name and class name are the same

2. No return value

3. When creating an object, the system will automatically call the constructor of this class to initialize the object

4. The constructor is to complete the initialization of the object, not to create the object

5. When creating an object, the system automatically calls the construction method of this class

public class Main{
    public static void main(String[] srgs){
        Person p1 = new Person("king",40);
        Person p2 = new Person("tom");
    }
}
//First constructor
class Person{
public Person(String pName;int pAge){
    name = pName;
    Age = pAge;
}
//The second constructor specifies only the name, not the age
public Person(String pName){
    name = pName;
}
}

6. If no constructor is defined, the system will automatically generate a default parameterless constructor (also known as the default constructor), such as Dog() {}, and decompile it with javap instructions

7. Once you define your own constructor, the default constructor will be overwritten, and you can no longer use the default parameterless constructor, unless you define it in the display, that is: Person() {}

public class Main{
    public static void main(String[] srgs){
        Dog d = new Dog();
    }
}
class Dog{
    /**If no constructor is defined, the system will automatically generate a default parameterless constructor (also known as the default constructor), such as Dog() {}. Decompile it with javap instruction / / javap Dog.class
    default constructor 
    Dog(){
    }
    */
    //Once you define your own constructor, the default constructor will be overwritten, and you can no longer use the default parameterless constructor, unless the definition shown is: Person() {}
    public Dog(String pName){ 
    ...... 
    }
    
}

this

public class Main{
    public static void main(String[] srgs){
        Dog d = new Dog("Dazhuang",10);
        d1.info();
    }
}
class Dog{ // class
    String nmae;
    int ang;
    public dog(String dName,int dAge){
        name = dName;
        age = dAge;
    }
    //It would be better if the formal parameters of our constructor could be written directly as attribute names
    public void info(){//Member method, output attribute + information
        System.out.println(name + "\t" + age + "\t");
    }
}

Problem: the input parameter name of the constructor is not very good. If you can change dName to name,

However, we will find that according to the scope principle of variables, the value of name is null. How to solve it

Export - > this

public class Main{
    public static void main(String[] srgs){
         Dog d = new Dog("Dazhuang",10);
        d1.info();
    }
}
class Dog{ // class
    String nmae;
    int ang;
    //The constructor's name and age are local variables, not attributes
    public dog(String name,int age){
        //this.name is the current object attribute nmae
       this.name = name;
        //this.age is the current object attribute age
        this.age = age;
    }
    public void info(){//Member method, output attribute + information
        System.out.println(name + "\t" + age + "\t");
    }
}

  Precautions and use details of this

1.this keyword can be used to access the properties, methods and constructors of this class

2. this is used to distinguish the attributes and local variables of the current class
3. Syntax for accessing member methods: this. Method name (parameter list);
4. Access constructor syntax: this (parameter list); Note that it can only be used in constructors
5. this cannot be used outside the class definition, but only in the methods defined by the class.

public class Main{
    public static void main(String[] srgs){
       T t = new T(); 
        t.f2(); 
    }
}
class T{
    /*
    Note: access constructor syntax: this (parameter list);
    The first statement must be placed
    */
    public T(){
        //Here you can access the T(String name,int age) constructor
        this("jack",1000);
        System.out.println("T()constructor ");
    }
    public T(String name,int age){
        System.out.println("T(String name,int age)constructor ");
    }
    public void f1(){
         System.out.println("f1()method..");
    }
     public void f2(){
         System.out.println("f2()method..");
         //Call f1 of this class
         //The first method
         f1();
         //The second method
         this.f1();
    }
}

Examples

·Define the Person class, which has the name and age attributes, and provide the compareTo comparison method to judge whether it is equal to another Person. Provide the test class Main for testing. If the name and age are exactly the same, return true, otherwise return false
·

public class Main{
    public static void main(String[] srgs){
      Person p = new Person("mary",20);
         Person p = new Person("smith",30);
         System.out.println(p1.compareTo(p2));
    }
}
class Person{
    String name;
    int age;
    //constructor 
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }
    //compareTo method
    public Boolean compareTo(Person p){
        if(this.name.equals(p.name)&&this.age == p.age){
            return true;
        }else{
            return false;
        }
    }
}//The result returns flash  


 

Posted by jphilapy on Fri, 26 Nov 2021 17:18:43 -0800