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:
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:
See the difference?
Ok, let me summarize and see if you can find the corresponding code in the above picture:
-
The constructor can have parameters or no parameters;
-
The constructor has no return value and does not need to declare
void
keywords; -
The constructor name must be the same as the class name.
Next, I main
create Student
the object code in the method as follows:
public static void main(String[] args){
Student stu = new Student();
Student stu1 = new Student("张三");
}
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-End
According to the prompt, supplement the code in the editor on the right :
-
Create a
Person
class,Person
define a parameterless construction method and a parameterized construction method for the class, the parameterized construction method defines twoString
type parametersname
and calls the parameterless construction method and the parameterized construction method in the methodsex
;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, new
when it is destroyed, the construction method will be called automatically.
-
Object initialization operations can be performed in the construction method, which has the following characteristics;
-
The construction method is different from the method of the object, it is called automatically when the object is created;
-
The constructor can have parameters or no parameters;
-
The constructor has no return value;
-
The constructor name must be the same as the class name;
-
If the defined class does not declare a constructor, the object will use an empty constructor by default when it is created.
-
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 **********/