当前位置:首页 > 题库 > 正文内容

PROGRAMMING:Jmu-java-03 object oriented foundation-02-constructor and initialization block

Luz7个月前 (05-10)题库76
##1. Define a person class
**Properties: * * ` name (string) ', ` gender (Boolean)', ` age (int) ', ` ID (int)', all variables must be private.
**Nonparametric constructor: * * ` person() ', * * function: * * print * * this is constructor * *.
**Parameterized constructors: * * ` person (name, gender, age) / *, * * functions: * * assign values to attributes.
Suggestion: use eclipse to generate toString method automatically
##2. Define the initialization block of the class
Add a * * initialization block * * to the person class, assign a value to the 'ID' attribute in the initialization block, and ensure that the value of each time is higher than the value of the last created object '+ 1'. Then print 'this is initialization block, ID is...' on the next line, where '...' is the value of ID.
**Tip: * * you can define a 'static' attribute for the 'person' class to record the number of objects created.
##3. Write static initialization block
Print ` This is static initialization block`
##4. Write the main method
-First, enter n to represent the number of objects to be created.
-Then read the 'name, gender, age' of N rows from the console, and call the parameterized constructor 'person (name, age, gender)' to create a new object.
-Output the created n objects in reverse order (that is, output the 'tostring()' method).
-Create a new person object using the parameterless constructor, and then print the object directly.
##Thinking
There are several methods to initialize class and object, including constructor, initialization block and static initialization block. What is the order of execution of these three methods? Several times each.
###Input example:
```in
three
a 11 false
b 12 true
c 10 false
```
###Output example:
```out
This is static initialization block
This is initialization block, id is 0
This is initialization block, id is 1
This is initialization block, id is 2
Person [name=c, age=10, gender=false, id=2]
Person [name=b, age=12, gender=true, id=1]
Person [name=a, age=11, gender=false, id=0]
This is initialization block, id is 3
This is constructor
null,0,false,3
Person [name=null, age=0, gender=false, id=3]
```






answer:If there is no answer, please comment
分享给朋友:

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。