The second pass: the construction method


mission details

The task of this level: Create an object and define a no-argument construction method and a parameter construction method for the object.

related information

In order to complete the task of this level, you need to master: 1. What is a construction method, 2. How to define and call a construction method.

What is a constructor

Construction method: The method that will be called when the object is created . When the object is created, that is, when it is destroyed , the construction methodnew will be called automatically .

for example:

810490cbc1148ee5dfdc76de895ac56a.png

output:

我被调用了

How to define and use constructors

How to define the constructor? What is the difference between the construction method and the method we learned before?
Please see the picture:

484647b416551b6b6667d042794f5341.png

See the difference?
Ok, let me summarize and see if you can find the corresponding code in the above picture:

  1. The constructor can have parameters or no parameters;

  2. The constructor has no return value and does not need to declare voidkeywords;

  3. The constructor name must be the same as the class name.

Next, I maincreate Studentthe object code in the method as follows:

 
  1. public static void main(String[] args){
  2. Student stu = new Student();
  3. Student stu1 = new Student("张三");
  4. }

Do you think there will be an output, and if so what will the output be? If not, what do you think is the reason?

programming requirements

Begin-EndAccording to the prompt, supplement the code in the editor on the right :

  • Create a Personclass, Persondefine a parameterless construction method and a parameterized construction method for the class, the parameterized construction method defines two Stringtype parameters nameand calls the parameterless construction method and the parameterized construction method in the method sex;main

  • Call the no-argument constructor output: 一个人被创建了;

  • Calling the parameterized construction method should output the corresponding result according to the input data, such as input: 张三, then output: 姓名:张三,性别:男,被创建了.

Test instruction

Test input: 张富贵, 未知
expected output:
一个人被创建了
姓名:张富贵,性别:未知,被创建了

Summary:
The construction method of the object:
when the object is created, that is, newwhen it is destroyed, the construction method will be called automatically.

  1. Object initialization operations can be performed in the construction method, which has the following characteristics;

  2. The construction method is different from the method of the object, it is called automatically when the object is created;

  3. The constructor can have parameters or no parameters;

  4. The constructor has no return value;

  5. The constructor name must be the same as the class name;

  6. If the defined class does not declare a constructor, the object will use an empty constructor by default when it is created.

  7. package step2;
    
    import java.util.Scanner;
    
    public class Test {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		String name = sc.next();
    		String sex = sc.next();
    		/********** Begin **********/
    		//分别使用两种构造器来创建Person对象  
    		Person p=new Person();
    		Person p1=new Person(name,sex);
    		
    
    
    		/********** End **********/
    		
    	}
    }
    
    //创建Person对象,并创建两种构造方法
    /********** Begin **********/
    class Person{
    	String name1;
    	String sex1;
    	public Person()
    	{
    		System.out.println("一个人被创建了");
    	}
    	public Person(String name,String sex)
    	{ // this.name1=name;
    	    //this.sex1=sex;
    	   
            System.out.println("姓名:"+ name +",性别:" + sex +",被创建了");
    		
    
    	}
    
    
    
    }
    
    
    /********** End **********/
    

     

 

 

Guess you like

Origin blog.csdn.net/qq_58259539/article/details/123643168