当前位置:网站首页>构造函数与toString
构造函数与toString
2022-04-22 07:44:00 【真题OK撒】
7-1 jmu-Java-03面向对象基础-01-构造函数与toString (3 分)
定义一个有关人的Person类,内含属性:String name、int age、boolean gender、int id,所有的变量必须为私有(private)。
注意:属性顺序请严格按照上述顺序依次出现。
1.编写无参构造函数:
- 打印"This is constructor"。
- 将name,age,gender,id按照
name,age,gender,id格式输出
2.编写有参构造函数
依次对name,age,gender赋值。
3.覆盖toString函数:
按照格式:类名 [name=, age=, gender=, id=]输出。建议使用Eclipse自动生成.
4.对每个属性生成setter/getter方法
5.main方法中
- 首先从屏幕读取n,代表要创建的对象个数。
- 然后输入n行name age gender , 调用上面2编写的有参构造函数新建对象。
- 然后将刚才创建的所有对象
逆序输出。 - 接下来使用无参构造函数新建一个Person对象,并直接打印该对象。
输入样例:
3
a 11 false
b 12 true
c 10 false
输出样例:
Person [name=c, age=10, gender=false, id=0]
Person [name=b, age=12, gender=true, id=0]
Person [name=a, age=11, gender=false, id=0]
This is constructor
null,0,false,0
Person [name=null, age=0, gender=false, id=0]
不清楚评论区欢迎提问^_^
/*
* 7-1 jmu-Java-03面向对象基础-01-构造函数与toString (3 分)
定义一个有关人的Person类,内含属性:
String name、int age、boolean gender、int id,所有的变量必须为私有(private)。
注意:属性顺序请严格按照上述顺序依次出现。
1.编写无参构造函数:
打印"This is constructor"。
将name,age,gender,id按照name,age,gender,id格式输出
2.编写有参构造函数
依次对name,age,gender赋值。
3.覆盖toString函数:
按照格式:类名 [name=, age=, gender=, id=]输出。建议使用Eclipse自动生成.
4.对每个属性生成setter/getter方法
5.main方法中
首先从屏幕读取n,代表要创建的对象个数。
然后输入n行name age gender , 调用上面2编写的有参构造函数新建对象。
然后将刚才创建的所有对象逆序输出。
接下来使用无参构造函数新建一个Person对象,并直接打印该对象。
输入样例:
3
a 11 false
b 12 true
c 10 false
输出样例:
Person [name=c, age=10, gender=false, id=0]
Person [name=b, age=12, gender=true, id=0]
Person [name=a, age=11, gender=false, id=0]
This is constructor
null,0,false,0
Person [name=null, age=0, gender=false, id=0]
*/
package pta_Java面向对象程序设计3面向对象基础;
import java.util.*;
public class JH_1 {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
Person a[] = new Person[n];
for (int i = 0; i < n; i++) {
String name = cin.next();
int age = cin.nextInt();
boolean gender = cin.nextBoolean();
a[i] = new Person(name, age, gender);
}
for (int i = n - 1; i >= 0; i--) {
a[i].toString();
}
new Person();
}
}
class Person {
private String name = null;
private int age = 0;
private boolean gender = false;
private int id = 0;
Person() {
System.out.println("This is constructor");
System.out.println(name + "," + age + "," + gender + "," + id);
System.out.println("Person [name=" + name + ", age=" + age + ", gender=" + gender + ", id=" + id + "]");
}
Person(String name, int age, boolean gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String toString() {
System.out.println("Person [name=" + name + ", age=" + age + ", gender=" + gender + ", id=" + id + "]");
return "1";
}
}
版权声明
本文为[真题OK撒]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_51916951/article/details/124223459
边栏推荐
- shell脚本学习——实战案例
- One question per day: improve the mold assembly on the 15th day of sprint in the big factory
- eventBus
- 指针和数组(操作详解)
- 101. 对称二叉树(Easy)
- 226. 翻转二叉树(Easy)
- winform中网络开发
- Make the airtest ide into a script and make the script run
- The fluent modul class and JSON are converted to each other
- Is NFT just a beautiful foam?
猜你喜欢

The domestic cloud security market has exceeded 10 billion yuan. What is the future development trend?

【论文阅读】【3d目标检测】pvgnet

MySQL in-depth study (Trinity): the use of PowerDesigner

C的动态内存管理

Flutter GridView Demo

leaflet、cesium加载百度地图,加载自定义样式百度地图

The industrialization of SCRM has accelerated, and the manufacturing industry has begun to play with private traffic

Machine learning notes - Mathematics in principal component analysis

vscode的插件

广城云服务实现每天定时填写一日一报
随机推荐
shell笔记
用OnLayoutChangeListener的方法解决getTop=0的问题
AC380V降5V12V24V200MA,超高压非隔离芯片IC方案
每日一题冲刺大厂第十五天 终于结束的起点
cesium 采集地形高度,采集模型高度 (异步方法,适合数据较多的时候)
What are the suffocating questions in the interview of computer guarantee research?
手把手教你实现RecyclerView的下拉刷新和上拉加载更多
技术选型分类(2022)
js学习笔记
MaterialApp
The domestic cloud security market has exceeded 10 billion yuan. What is the future development trend?
I feel that writing a paper is a waste of time. Do I want to write a paper?
Visualization of unity perspective projection matrix transformation
微服务(分布式架构)
[跟着官方文档学JUnit5][二][WritingTests][学习笔记]
eventBus
Leetcode 111 Balanced binary tree (2022.04.21)
Pyqt5 uses the built-in database sqllite
7-34 delete duplicate characters (set usage) & 7-35 count the number of characters (unordered_map)
EFCore优化之AsNoTracking