创建适当的重载构造函数伪代码

发布于 2024-09-30 16:49:56 字数 1132 浏览 8 评论 0原文

我对如何创建重载构造函数有点困惑。

下面是我的作业:

包括以下内容:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅一夜美梦 2024-10-07 16:49:56

正是因为这个原因,我一直讨厌课堂环境中的伪代码。你写了多少?我可以放弃什么,或者换句话说,我们在这里认为理所当然的是什么?

至于重载函数,看起来你理解得很好。

这是不带任何参数的默认构造函数:

public Employee ()

这是另一个带参数的构造函数。

public Employee (id : integer, salary : integer)

所以,回答你原来的问题,你是对的。重载构造函数只是具有多个采用不同参数的构造函数。对于您的情况,我可以通过两种不同的方式调用构造函数来创建该类:

Employee myEmployee = new Employee();

或者

Employee myEmployee = new Employee(12, 64500);

用程序员的话来说,您有一个重载的构造函数。

至于代码的其余部分:

您做错的一件事是您没有声明“numberofaccidents”。像这样:

private employeeid : integer
private employeesalary : integer
private numberofaccidents : integer

将访问器和修改器视为“getters”和“setters”。不过,您可能想将“setCustomerget”更改为“setCustomerId”。此外,不存在“事故数量”。尝试将 getter 和 setter 配对,这样您就可以随时判断是否缺少一个。像这样的东西:

public num getSalary() 
return employeesalary

public void setSalary(integer salary)
employeesalary = salary

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:

public Employee ()

And here is your other constructor that does take arguments.

public Employee (id : integer, salary : integer)

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:

Employee myEmployee = new Employee();

OR

Employee myEmployee = new Employee(12, 64500);

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:

private employeeid : integer
private employeesalary : integer
private numberofaccidents : integer

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:

public num getSalary() 
return employeesalary

public void setSalary(integer salary)
employeesalary = salary
霞映澄塘 2024-10-07 16:49:56

是的。您的代码看起来像是在正确的行上。但是,方法/函数不应该是类的一部分吗?

  • 重载 X 是具有不同参数集的 X。
  • 访问器是一个从对象获取某些内容的函数(有时称为属性)。
  • 增变器是一个改变对象某些方面的函数(有时称为属性)。

Yes. You code looks like its on the right lines. However shouldn't the methods/functions be part of the class?

  • An overload X is an X with different set of parameters.
  • An Accessor is a function (sometimes called a property) that gets something from the object.
  • A mutator is a function (sometimes called a property) that changes some aspect of the object.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文