Java 在构造函数中添加组而不是仅添加一个组
对不起,如果我有点含糊不清,但在不是我的母语和我是初学者之间,我正在尽力把问题说清楚 我被困在一个我找不到任何解决方案的练习中,即使在谷歌搜索之后。至少我不明白 我必须创建两个构造器,其中一个,除了变量之外,接收一组教师,另一个只接收一个教师Java 在构造函数中添加组而不是仅添加一个组,java,Java,对不起,如果我有点含糊不清,但在不是我的母语和我是初学者之间,我正在尽力把问题说清楚 我被困在一个我找不到任何解决方案的练习中,即使在谷歌搜索之后。至少我不明白 我必须创建两个构造器,其中一个,除了变量之外,接收一组教师,另一个只接收一个教师 private Set<Teacher> teacher; public DespachoImpl(String nombre, Integer planta, Integer aforo, Teacher teacher) { su
private Set<Teacher> teacher;
public DespachoImpl(String nombre, Integer planta, Integer aforo, Teacher teacher) {
super(TipoLugar.OTRO, nombre, planta, aforo);
this.teacher = new HashSet<Teacher>();
}
私人设置教师;
公共DespachoImpl(字符串名称、整数植物、整数植物、教师){
超级(蒂波卢加、奥特罗、诺姆布雷、普兰塔、阿福罗);
this.teacher=newhashset();
}
我找不到任何关于如何使它们不同的东西,因此一个构造函数只添加了一个教师,而另一个构造函数添加了几个教师。感谢您的帮助,如果您也链接了一些资源,那就太好了。如果您至少需要一个教师(其他教师可以是可选的),即一对多关系,您不需要两个构造函数。一个参数就足够了
public DespachoImpl(String nombre, Integer planta, Integer aforo, Teacher required, Teacher... moreTeachers) {
super(TipoLugar.OTRO, nombre, planta, aforo);
this.teacher = new HashSet<Teacher>();
this.teacher.add(required);
this.teacher.addAll(Arrays.asList(moreTeachers));
}
public DespachoImpl(字符串名称、整数planta、整数aforo、教师必选、教师…更多教师){
超级(蒂波卢加、奥特罗、诺姆布雷、普兰塔、阿福罗);
this.teacher=newhashset();
this.teacher.add(必选);
this.teacher.addAll(Arrays.asList(morethers));
}
您需要尝试构造函数重载
public class Teacher
{
public Teacher(string teacherName)
{
// Add single teacher
}
public Teacher(List<string> teacherNames)
{
// Add multiple teachers
}
}
公共课堂教师
{
公共教师(字符串教师姓名)
{
//添加单个教师
}
公立教师(列出教师姓名)
{
//添加多个教师
}
}
希望这有帮助您可以获得任意数量的教师,然后检查教师数量是否至少为1:
private Set<Teacher> teachers;
public DespachoImpl(String nombre, Integer planta, Integer aforo, Teacher... teachers) {
super(TipoLugar.OTRO, nombre, planta, aforo);
if (teachers.length == 0) {
throw new IllegalArgumentException("No teachers given");
}
this.teachers = new HashSet<Teacher>();
this.teachers.addAll(Arrays.asList(teachers));
}
私人设置教师;
公共DespachoImpl(字符串名称、整数植物、整数植物、教师…教师){
超级(蒂波卢加、奥特罗、诺姆布雷、普兰塔、阿福罗);
如果(teachers.length==0){
抛出新的非法辩论例外(“未给出教师”);
}
this.teachers=newhashset();
this.teachers.addAll(Arrays.asList(teachers));
}
如果需要两个构造函数,那么这是一种方法:
private Set<Teacher> teachers;
public DespachoImpl(String nombre, Integer planta, Integer aforo, Teacher teacher) {
this(nombre, planta, aforo, new Teacher[] { teacher });
}
public DespachoImpl(String nombre, Integer planta, Integer aforo, Teacher[] teachers) {
super(TipoLugar.OTRO, nombre, planta, aforo);
if (teachers.length == 0) {
throw new IllegalArgumentException("No teachers given");
}
this.teachers = new HashSet<Teacher>();
this.teachers.addAll(Arrays.asList(teachers));
}
私人设置教师;
公共DespachoImpl(字符串名称、整数植物、整数植物、教师){
这(nombre、planta、aforo、新教师【】{Teacher});
}
公共DespachoImpl(字符串名称、整数planta、整数aforo、教师[]教师){
超级(蒂波卢加、奥特罗、诺姆布雷、普兰塔、阿福罗);
如果(teachers.length==0){
抛出新的非法辩论例外(“未给出教师”);
}
this.teachers=newhashset();
this.teachers.addAll(Arrays.asList(teachers));
}
这对我来说似乎很愚蠢,但第一个构造函数必须能够调用第二个构造函数。除此之外,它和第一个一样是一个固态解。我创建了两个构造函数,其中一个除了变量之外,还接收一组教师作为
列表教师
,另一个只接收一个教师对象
private Set<Teacher> teacher;
私人设置教师;
将单个教师对象添加到集合中
public DespachoImpl(String nombre, Integer planta, Integer aforo,Teacher teacher) {
super(TipoLugar.OTRO, nombre, planta, aforo);
this.teacher = new HashSet<Teacher>();
this.teacher.add(teacher);
}
public DespachoImpl(字符串名、整数planta、整数aforo、教师){
超级(蒂波卢加、奥特罗、诺姆布雷、普兰塔、阿福罗);
this.teacher=newhashset();
this.teacher.add(teacher);
}
将教师列表添加到集合中
public DespachoImpl(String nombre, Integer planta, Integer aforo,List<Teacher> teachers) {
super(TipoLugar.OTRO, nombre, planta, aforo);
this.teacher = new HashSet<Teacher>(teachers);
}
public DespachoImpl(字符串名、整数planta、整数aforo、列表教师){
超级(蒂波卢加、奥特罗、诺姆布雷、普兰塔、阿福罗);
this.teacher=新哈希集(教师);
}
或者可以将多个教师作为变量参数传递
public DespachoImpl(String nombre, Integer planta, Integer aforo, Teacher... teachers) {
super(TipoLugar.OTRO, nombre, planta, aforo);
this.teachers = new HashSet<Teacher>();
this.teachers.addAll(Arrays.asList(teachers));
}
public DespachoImpl(字符串名称、整数planta、整数aforo、教师……教师){
超级(蒂波卢加、奥特罗、诺姆布雷、普兰塔、阿福罗);
this.teachers=newhashset();
this.teachers.addAll(Arrays.asList(teachers));
}
正如建议的那样,应该使用构造函数重载
Java知道使用这两个构造函数中的哪一个,因为它们的参数不同。因此,您可以创建两个不同的构造函数,只要参数的类型或数量不同就可以了
在您的情况下,参数的数量相同,但最后一个参数的类型不同。对于一个构造函数,类型是Teacher
,对于另一个Set
。因此,用Java编写这篇文章是合法的
例如:
private Set<Teacher> teacher;
// Constructor with only one teacher.
public DespachoImpl(String nombre, Integer planta, Integer aforo, Teacher teacher) {
super(TipoLugar.OTRO, nombre, planta, aforo);
this.teacher = new HashSet<Teacher>();
this.teacher.add( teacher );
}
// Constructor with a set of teachers.
public DespachoImpl(String nombre, Integer planta, Integer aforo, Set<Teacher> teachers) {
super(TipoLugar.OTRO, nombre, planta, aforo);
this.teacher = new HashSet<Teacher>(teachers);
}
它将调用第一个构造函数。但如果你打电话:
new DespachoImpl( "XXXX", 5, 8, new Teacher(....) );
Set<Teacher> teachers = new HashSet<>();
teachers.add( new Teacher(...));
teachers.add( new Teacher(...));
new DespachoImpl( "XXXX", 5, 8, teachers );
Set teachers=newhashset();
教师。添加(新教师(…);
教师。添加(新教师(…);
新DespachoImpl(“XXXX”,5,8,教师);
它将调用第二个构造函数。只需编写第二个构造函数。使用构造函数重载创建两个构造函数
列出教师
,以添加多个教师。@atishhimpi那么,不是构造函数中的教师,而是列表?然后在内部,this.teachers=newhashset();你的作业有没有说明教师的“群体”应该是什么?我看到您将Set
定义为私有变量。你应该得到一套教师吗?列表
?任何类型的集合
?数组?至于练习,我需要两个。必需的不是多余的吗<代码>老师。。。教师本身就足够了。@olavimutanoja否,因为您可以创建一个没有教师的DespachoImpl
对象。我认为它至少需要一名教师。@BrickTop这似乎是不必要的要求。如果你能用一个构造器,为什么还要添加两个构造器呢?@ZouZou不确定,这是写在纸上的。这就是为什么我不知道如何让两个构造函数在不同的情况下向同一个列表添加1/2+。如何指定其中一个仅在列表中添加一名教师时使用,另一个仅在列表中添加几名教师时使用