如何使用构造函数添加员工列表 [英] how to add employee list using constructor
问题描述
我创建了一个名为employee的类,其成员ID,名称,薪水.我在此回答的几个问题我不确定,请检查并告诉我答案
这是问题
Q 1)默认的构造函数是什么?
回答:
I have created a class named employee with its members id,name,salary.I have few questions in this whose answer i am not confident .Please check it and tell me the answers
here are the questions
Q 1 ) what will be its default constructor?
ans:
Sub New()
End Sub
Q 2)编写一个重载的构造函数,该构造函数传递雇员的姓名和薪水?
答案:
Q 2 ) write an overloaded constructor that passes the employees name and salary?
ans:
Sub New(ByVal name1 As String, ByVal salary1 As Decimal)
name = name1
salary = salary1
End Sub
Q 3)编写工资的getter和setter方法
ans:
Q 3 ) write getter and setter method for salary
ans:
Property salary1() As Decimal
Get
salary1 = salary
End Get
Set(ByVal value As Decimal)
salary = value
End Set
End Property
问题4)只为ID写一个吸气剂?
回答:
Q 4 ) write a getter only for the id?
ans:
ReadOnly Property id1() As Decimal
Get
id1 = id
End Get
问题5)创建员工列表.
我不知道答案.
问题6)使用默认构造函数将新员工添加到列表中
我不知道答案.
问题7)使用重载的构造函数将新员工添加到列表中
回答:
Q 5 ) create a list of employees.
i don''t know its answer.
Q 6 ) add a new employee to the list using default constructor
i don''t know its answer.
Q 7) Add a new employee to the list using overloaded constructor
ans:
Private emp As Employee
emp = New Employee(txtid.Text, txtname.Text, txtsalary.Text)
txtid,txtname,txtsalary是表单上的文本框
问题8)编写将遍历员工列表并输出其薪水的代码
我不知道它的答案
问题9)如果重载表单的构造函数,应该首先调用哪个函数?
我不知道它的答案
请查看并提供相关答案
thanx
问候
txtid,txtname,txtsalary are textboxes on form
Q 8 ) write the code that would loop through employee list and output their salary
I don''t know its answer
Q 9 ) what function should be called first if overloading a form''s constructor?
I don''t know its answer
please see to it and provide me relevant answers
thanx
regards
推荐答案
尝试一下:
try this:
Private emp As Employee
emp = New Employee( txtname.Text, txtsalary.Text)
您没有获得3个论点的承包商!
您还可以添加到承包商中:
you dont have a constractor that get 3 arguments!
you can also add to your constractor:
Sub New(ByVal id1 as Decimal, ByVal name1 As String, ByVal salary1 As Decimal)
name = name1
salary = salary1
id=id1
End Sub
非常简单.尝试以下代码:
It''s Very Simple.Try this code :
'If Employee is the Class
Dim Employee1 As Employee = New Employee("Name","Salary")
Dim EmployeeList As List(Of Employee)
EmployeeList.Add(Employee1)
希望对您有帮助. :)
I hope it will help you. :)
这篇关于如何使用构造函数添加员工列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!