创建适当的重载构造函数伪代码
我对如何创建重载构造函数有点困惑。
下面是我的作业:
包括以下内容:
i) 一个默认构造函数和一个重载构造函数。
ii) 每个属性的访问器和修改器方法。
下面是我的伪代码。从到目前为止我创建的内容来看,我假设 OVERLOADED 构造函数是一个具有参数的构造函数,但我对此并不确定。
public class Employee
// declarations
private employeeid : integer
private employeesalary : integer
public Employee ()
employeeid = 0
employeesalary = 0
return
public Employee (id : integer, salary : integer)
employeeid = id
employeesalary = salary
return
public num getemployeeid ( )
return employeeid
public num getemployeesalary ( )
return employeesalary
public String getnumberofaccidents ( )
return numberofaccidents
public void setCustomeraget(integer id)
employeeid = id
return
public void setEmployeesalary (integer salary)
employeesalary = salary
return
End Class
例子:
public policy holder (nr : num, age : num, nracct : num)
set policynumber (nr : num)
set customerage (age : num)
set NumberAccident (nrAcct)
I am a bit confused on how to create a overloaded constructor.
Below is my assignment:
Include the following:
i) A default constructor and an overloaded constructor.
ii) Accessor and mutator methods for each attribute.
Below is my pseudocode. From what I created so far I assume a OVERLOADED constructor is a constructor that has parameters but I am not absolutely sure about that.
public class Employee
// declarations
private employeeid : integer
private employeesalary : integer
public Employee ()
employeeid = 0
employeesalary = 0
return
public Employee (id : integer, salary : integer)
employeeid = id
employeesalary = salary
return
public num getemployeeid ( )
return employeeid
public num getemployeesalary ( )
return employeesalary
public String getnumberofaccidents ( )
return numberofaccidents
public void setCustomeraget(integer id)
employeeid = id
return
public void setEmployeesalary (integer salary)
employeesalary = salary
return
End Class
Example:
public policy holder (nr : num, age : num, nracct : num)
set policynumber (nr : num)
set customerage (age : num)
set NumberAccident (nrAcct)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正是因为这个原因,我一直讨厌课堂环境中的伪代码。你写了多少?我可以放弃什么,或者换句话说,我们在这里认为理所当然的是什么?
至于重载函数,看起来你理解得很好。
这是不带任何参数的默认构造函数:
这是另一个带参数的构造函数。
所以,回答你原来的问题,你是对的。重载构造函数只是具有多个采用不同参数的构造函数。对于您的情况,我可以通过两种不同的方式调用构造函数来创建该类:
或者
用程序员的话来说,您有一个重载的构造函数。
至于代码的其余部分:
您做错的一件事是您没有声明“numberofaccidents”。像这样:
将访问器和修改器视为“getters”和“setters”。不过,您可能想将“setCustomerget”更改为“setCustomerId”。此外,不存在“事故数量”。尝试将 getter 和 setter 配对,这样您就可以随时判断是否缺少一个。像这样的东西:
I always hated psudocode in classroom settings for this very reason. How much do you write? What am I allowed to leave off, or in other words, what are we taking for granted here?
As far as the overloaded function, it looks as though you understand it just fine.
Here is your default constructor that doesn't take any arguments:
And here is your other constructor that does take arguments.
So, to answer your original question, you are correct. An overloaded constructor is simply having multiple constructors that take different arguments. In your case, I can create the class by calling the constructor two different ways:
OR
In programmer speak you have an overloaded constructor.
As for the rest of your code:
One thing you are doing wrong is that you have not declared "numberofaccidents". Like so:
Think of accessors and mutators as "getters" and "setters". You might want to change "setCustomeraget" to "setCustomerId" though. Also, there is no "setnumberofaccidents". Try and pair up your getters and setters so that you can always tell if you are missing one. Something like this:
是的。您的代码看起来像是在正确的行上。但是,方法/函数不应该是类的一部分吗?
Yes. You code looks like its on the right lines. However shouldn't the methods/functions be part of the class?