当前位置:网站首页>Detailed explanation of the difference between construction method and method

Detailed explanation of the difference between construction method and method

2022-06-11 09:19:00 lwj_ 07

 

Conclusion !!!

Although there is no construction method in the student class But in the test code Student Object is also created . The reason is that when there is no constructor in the class, the system will construct a parameterless constructor by default  

The difference between construction method and common method structure is as follows :

 [ List of modifiers ] Constructor name ( List of formal parameters ){
             Construct method body ;
            // You usually assign values to attributes in the constructor body , Complete the initialization of the property .
        }
         Be careful :
             First of all : The modifier list is currently uniformly written as :public It must not be written as public static
             second : Constructor name and class name must be consistent
             Third : The constructor does not need to specify the return value type , You can't write void, Writing words has become a common method .

The common method structure is :
        [ List of modifiers ] return type Method name ( List of formal parameters ){
             Method body ;
        }
         Be careful : The modifier list is : public static

How to call the constructor ?

The difference between a constructor and a normal method call :

Common method :

public class OOTest01
{
	public static void main(String[] args){
			//  Calling normal methods 
			dosome();   //  In the same class, there is no   Class name .dosome();  It's fine too 
            

	
	//  Common method 
		public static void dosome(){
			System.out.println("dosome");
		}
}

Construction method :

Use new Operator to call the constructor .
         Grammar format :
            new Constructor name ( List of formal parameters );

        // Student s =new Student(); among Student(); It's actually a construction method

Students : 

public class Student
{
	// Student number 
	int no;
	// full name 
	String name;
	// Age 
	int age;

	// Current Student There is no constructor defined in this class 
	// But the system will default to Student Class provides a parameterless constructor 

	//  The construction method without parameters ( Default constructor ) Write it down as follows :
	public Student(){
		System.out.println(" The parameterless construction method is implemented ~");
	}

}

Test code :

public class OOTest01
{
	public static void main(String[] args){
			//  Call constructor 
			new Student();	//  First execution 
			
			//  Calling normal methods 
			dosome();

		//  establish Student Object of type 
		Student s =new Student();	//  Second execution    new Student() 

		//System.out.println(s.name);   // null

		//  Output " quote s"
		//  As long as the output is not null  This indicates that this object must have been created .
	    //  Output results : [email protected]
		System.out.println(s);
		}
	
	//  Common method 
		public static void dosome(){
			System.out.println("dosome");
		}
}

Running results :

When defining a constructor with parameters :

public class Student
{
	// Student number 
	int no;
	// full name 
	String name;
	// Age 
	int age;

	// Current Student There is no constructor defined in this class 
	// But the system will default to Student Class provides a parameterless constructor 

	// The construction method without parameters ( Default constructor ) Write it down as follows :
	/*
	public Student(){
		System.out.println(" The parameterless construction method is implemented ~");
	}
	*/

	// Define a construction method with parameters 
	public Student(int i){
		System.out.println("~");
	}
}

Test code :

public class OOTest01
{
	public static void main(String[] args){
			//  Call constructor 
			new Student();	//  First execution 
			
			//  Calling normal methods 
			dosome();

		//  establish Student Object of type 
		Student s =new Student();	//  Second execution    new Student() 

		//  As long as the output is not null  This indicates that this object must have been created .
	    //  Output results : [email protected]
		System.out.println(s);
		}
	
	//  Common method 
		public static void dosome(){
			System.out.println("dosome");
		}
}

  The operation results are as follows :

  When defining a parameterless constructor and a parameterless constructor :【 Similar method overloading mechanisms 】

public class Student
{
	// Student number 
	int no;
	// full name 
	String name;
	// Age 
	int age;

	// Current Student There is no constructor defined in this class 
	// But the system will default to Student Class provides a parameterless constructor 

	// The construction method without parameters ( Default constructor ) Write it down as follows :
	
	public Student(){
		System.out.println(" The parameterless construction method is implemented ~");
	}
	

	// Define a construction method with parameters 
	public Student(int i){
		System.out.println("~");
	}
}

Test code :

public class OOTest01
{
	public static void main(String[] args){
			//  Call constructor 
			new Student();	//  First execution 
			
			//  Calling normal methods 
			dosome();

		//  establish Student Object of type 
		Student s =new Student();	//  Second execution    new Student() 

		//  As long as the output is not null  This indicates that this object must have been created .
	    //  Output results : [email protected]
		System.out.println(s);
		}
	
	//  Common method 
		public static void dosome(){
			System.out.println("dosome");
		}
}

Running results :

  When giving test code Student(); When parameters are passed :

public class OOTest01
{
	public static void main(String[] args){
			//  Call constructor 
			new Student(100);	//  First execution 
			
			//  Calling normal methods 
			dosome();

		//  establish Student Object of type 
		Student s =new Student(100);	//  Second execution    new Student() 

		//  As long as the output is not null  This indicates that this object must have been created .
	    //  Output results : [email protected]
		System.out.println(s);
		}
	
	//  Common method 
		public static void dosome(){
			System.out.println("dosome");
		}
}

Running results :

  reflection :【 In the future, try to write the parameterless construction method 】

    1、 reflection : When the instance variable is not assigned manually , In fact, the system will assign values by default ,
     So when is the default assignment performed ?
         Do you assign values to these instance variables when the class is loaded ?
             No ( Class loading occurs in the method area At this time, it is not in the stacking area new An object )
             Instance variables are initialized during the execution of the constructor , Complete the assignment .
            【 Because when new Object time Such as new Student(); Student(); It's actually a construction method 】

public class User
{
	int id;
	String name;
	int age;
	
	//  Parameterless construction method 
	public User(){
		// There are actually three lines of code that you can't see .
		// Although there is no code written in the body of the parameterless constructor ,
		// But in fact, the default value initialization of instance variables is carried out in this method body ( Instance variables can be modified )
		/*
		id =0;
		name ="junker";
		age =0;
		*/
	}

	//  There are parameter construction methods 
	public User(int i){
		/*
		id =0;
		name =null;
		age =0;
		*/
	}
}
public class OOTest02
{
	public static void main(String[] args){
		
		User u =new User();
		System.out.println(u.name);//junker
		User a =new User(100);
		System.out.println(a.name);//null
	}
}

Constructor code walkthrough

public class Vip
{
	// Membership number 
	long no;
	// Membership name 
	String name;
	// Birthday 
	String birth;
	// Gender 
	boolean sex;

	// Parameterless construction method 
	public Vip(){
		
	}
	
	// There are parameter construction methods 
	public Vip(long huiyuanHao){
		no =huiyuanHao;
		// Default name =null;birth =null;sex =false; 
		
	}
	public Vip(long huiyuanHao,String xingMing){
		no =huiyuanHao;
		name =xingMing;
	}
	public Vip(long huiyuanHao,String xingMing,String shengRi){
		no =huiyuanHao;
		name =xingMing;
		birth =shengRi;
	}
	public Vip(long huiyuanHao,String xingMing,String shengRi,boolean xingBie){
		no =huiyuanHao;
		name =xingMing;
		birth =shengRi;
		sex =xingBie;
	}	
}
public class OOTest03
{
	public static void main(String[] args){
		
		Vip v1 =new Vip();
		System.out.println(v1.no);//0
		System.out.println(v1.name);//null
		System.out.println(v1.birth);//null
		System.out.println(v1.sex);//false

		Vip v2 =new Vip(11111L);
		System.out.println(v2.no);//11111L
		System.out.println(v2.name);//null
		System.out.println(v2.birth);//null
		System.out.println(v2.sex);//false

		Vip v3 =new Vip(22222L," Little sheep ");
		System.out.println(v3.no);//22222L
		System.out.println(v3.name);// Little sheep 
		System.out.println(v3.birth);//null
		System.out.println(v3.sex);//false

		Vip v4 =new Vip(33333L," The Wolf ","60-6-6");
		System.out.println(v4.no);//33333L
		System.out.println(v4.name);// The Wolf 
		System.out.println(v4.birth);//60-6-6
		System.out.println(v4.sex);//false

		Vip v5 =new Vip(44444L," Iron man ","10-1-1",false);
		System.out.println(v4.no);//44444L
		System.out.println(v4.name);// Iron man 
		System.out.println(v4.birth);//10-1-1
		System.out.println(v4.sex);//false
		
	}
}

原网站

版权声明
本文为[lwj_ 07]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020507216471.html