2021 front-end calibration and recruitment through train to realize Offer zero distance MK

Posted by Hybrid Kill3r on Fri, 12 Nov 2021 12:40:10 +0100

2021 front-end calibration and recruitment through train to realize Offer zero distance MK

1, What is a structural function

java structure function, also known as structure method, is a special function in java. The function name is the same as and has no return value.

Function: it is generally used to initialize member properties and methods, that is, after the new object is generated, the object properties and methods are called.

In an ideal life, many things are born with certain attributes and behaviors as soon as they appear. For example, once a person is born, he has age, height, weight and will cry; As soon as a car is produced, it has color, appearance, operation, etc. With these, we can define these natural attributes and behaviors in the structure function. When new instantiates the object, it will have these attributes and methods. There is no need to redefine them, so as to speed up the programming efficiency.

Structure function is to run as soon as the object is established, initialize the object, including attributes and execute the statements in the method.

Ordinary functions are executed only when the object is called. Add functions to the object in the way of ". Method name".

When an object is set up, the structure function runs only once.

Ordinary functions can be called by the object many times.

2, Characteristics of structure function

1. The function name is the same as the class name

2. There is no need to define the return value type. (unlike the return value of void type, void does not have a detailed return value type; the structure function does not even have a type.)

3. You cannot write a return statement. (there are no return value types, so you don't need a return statement.)

Note: ordinary functions cannot call structural functions. Only structural functions can call structural functions.

3, Example

1. There is only one method defined in the parameterless structure function. When a new object is, the corresponding structure function is called to execute this method. There is no need to write ". Method name".

Copy code
package javastudy;
public class ConfunDemo {

public static void main(String[] args) {
    Confun c1=new Confun();            //As soon as the Hello World. new object is output, the corresponding structure function Confun() will be called and the println statement will be executed.
}

}
class Confun{

Confun(){        //Define the structure function and output Hello World
    System.out.println("Hellow World");
}

}
Copy code
Output: Hello World

2. The parameterized structure function passes the actual parameter value to the private variable when the new object is used, which is equivalent to completing the setter function.

Copy code
package javastudy;
public class ConfunDemo3 {

public static void main(String[] args){
    Person z=new Person("zhangsan",3);        //When instantiating an object, new Person() directly calls the Person structure function and turns the argument, which is equivalent to the setter function
    z.show();
}

}
class Person{

private String name;
private int age;
public Person(String n,int m){                //There is a parameter structure function to complete the function of passing parameter values to private member variables
    name=n;
    age=m;        
}
//Getter / / when instantiating an object, after the set function is completed, the getter is required to obtain the actual parameter value.
public String getName(){
    return name;
}
public int getAget(){
    return age;
}
public void show(){                           //Get the private value and print out
    System.out.println(name+"\n"+age);
}

}
Copy code
Output:
zhangsan
3

In the above code, we can also directly put the output statement in the show() method into the structure function. When the new object is, we can directly output the value, as shown below

Copy code
package javastudy;
public class ConfunDemo3 {

public static void main(String[] args){
    Person z=new Person("zhangsan",3);        //When instantiating an object, new Person() directly calls the Person structure function, turns the argument, and executes the output statement at the same time
}

}
class Person{

private String name;
private int age;
public Person(String n,int m){                //There is a parameter structure function to complete the function of transmitting parameter values to private member variables, and directly output values at the same time
    name=n;
    age=m;
    System.out.println(name+"\n"+age);
}

}
Copy code
Output:
zhangsan
3

or

Copy code
class ConFun
{

public static void main(String[] args){
    Person z=new Person(20,"zhangsan");
    System.out.println(z.getAge()+z.getName());
}

}
class Person
{

private int age;
private String name;
public Person(int x,String y){
    age=x;
    name=y;
}
public int getAge(){
    return age;
}
public String getName(){
    
    return name;
}

}
Copy code
3. After an object is set up, the structure function runs only once.

If you want to assign a new value to the value of the object, you should use the set and get methods. At this time, it is used as an ordinary function

As follows:

Copy code
package javastudy;
public class ConfunDemo4 {

public static void main(String[] args) {
        PersonDemo s=new PersonDemo("Li San",33);        //When a new object is, the corresponding structure function is called and the value is passed. At the same time, the same object cannot be new multiple times, otherwise an error will be reported.
        s.setName("Li Wu");                            //After the object is established, if you want to change the value, you should use the set/get method to reset the new value
        s.setName("Alpha dog");                        //And the object can be called several times.
        s.print();
}

}
class PersonDemo{

private String name;
private int age;
PersonDemo(String n,int m){                //Establish a parametric structure function, which is used to assign values to two private variables name and age, and output values at the same time
    name=n;
    age=m;
    System.out.println("full name:"+name+"Age:"+age);
}
public void setName(String x){            //The set method is used to assign a value to name again
    name=x;        
}
public String getName(){                //The get method is used to obtain the assignment of name
    return name;
}
public void print(){
    System.out.println(name);
}

}
Copy code
Output results:

Name: Li San age: 33
Alpha dog

4, Acquiescence structure function

When there is no structure function defined in a class, the system will add a structure function with an implied null parameter to the class, which is convenient for the initialization of the class, but the empty structure function is hidden.

As follows, the tacit structure function of Person() {} is hidden.

class Person
{

//Person(){}

}
When the structure function is customized in this class, there is no acquiescence structure function.

If you still want the structure function, you need to add it manually in the class.

5, Overloading of structure functions

Structure function is also a kind of function, which also has the function overloading feature.

Copy code
class Person
{

private String name;
private int age;

Person()
{
    System.out.println("A:name="+name+":::age="+age);
}

Person(String n)
{
    name = n;
    System.out.println("B:name="+name+":::age="+age);
}

Person(String n,int a)
{  
    name=n;
    age=a;
    System.out.println("C:name="+name+":::age="+age);
}

}

class PersonDemo2
{

public static void main(String[] args)
{
    Person p1=new Person();
    Person p2=new Person("lishi");
    Person p3=new Person("lishi",10);
}

}
Copy code
Output results:
A:name=null:::age=0
B:name=lishi:::age=0
C:name=lishi:::age=10

Copy code
class Person
{

private String name;
private int age;

Person()
{
    System.out.println("A:name="+name+":::age="+age);
    cry();
}

Person(String n)
{
    name = n;
    System.out.println("B:name="+name+":::age="+age);
    cry();
}

Person(String n,int a)
{  
    name=n;
    age=a;
    System.out.println("C:name="+name+":::age="+age);
    cry();

}
void cry()
{
    System.out.println("Cry...............");
}

}

class PersonDemo2
{

public static void main(String[] args)
{
    Person p1=new Person();
    Person p2=new Person("lishi");
    Person p3=new Person("lishi",10);
}

}
Copy code
Output results:
A:name=null:::age=0
Cry...............
B:name=lishi:::age=0
Cry...............
C:name=lishi:::age=10
Cry...............

Topics: node.js