Class 01
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lab06_ex02
{
public class Employee01
{
public string EmployeeName01 { get; set; }
public Employee01(string employeeName01)
{
EmployeeName01 = employeeName01;
}
public void InputEmployeeNames01()
{
Console.WriteLine("Please enter a name of an employee:");
string employeeName01 = Console.ReadLine();
}
public void DisplayEmployeeNames01()
{
Console.WriteLine("The name of the first employee entered was: \n{0}\n", EmployeeName01);
}
}
}
02级
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lab06_ex02
{
public class Employee02
{
public string EmployeeName02 { get; set; }
public Employee02(string employeeName02)
{
EmployeeName02 = employeeName02;
}
public void InputEmployeeNames02()
{
Console.WriteLine("Please enter a name of another employee:");
string employeeName02 = Console.ReadLine();
}
public void DisplayEmployeeNames02()
{
Console.WriteLine("The name of the second employee entered was: \n{0}\n", EmployeeName02);
}
}
}
和03级
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lab06_ex02
{
public class EmployeeTest
{
public static void Main(string[] args)
{
//create Employee object myEmployee and pass string to constructor
Employee01 myEmployee01 = new Employee01("");
Employee02 myEmployee02 = new Employee02("");
myEmployee01.InputEmployeeNames01();
myEmployee02.InputEmployeeNames02();
myEmployee01.DisplayEmployeeNames01();
myEmployee02.DisplayEmployeeNames02();
//Console.WriteLine("The name of the first employee was: ", myEmployee01.EmployeeName01);
//Console.WriteLine();
//Console.WriteLine("The name of the second employee was: ", myEmployee02.EmployeeName02);
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
Class 03显示object01和object02创建的输入参数值。如何让class03显示class01和class02中每个名称的用户输入?谢谢!
答案 0 :(得分:1)
不是将输入中的值分配给新变量,而是将其分配给属性。而不是
public void InputEmployeeNames01()
{
Console.WriteLine("Please enter a name of an employee:");
string employeeName01 = Console.ReadLine();
}
这样的事情可能有所帮助:
public void InputEmployeeNames01()
{
Console.WriteLine("Please enter a name of an employee:");
EmployeeName01 = Console.ReadLine();
}
显然,Employee02也是如此。
但为什么要为员工创建两个班级?为什么不是单个员工类然后创建该类的两个实例?
public class Employee
{
public string Name { get; set; }
public Employee(string name)
{
Name = name;
}
public void SetName()
{
Console.WriteLine("Please enter a name of an employee:");
Name = Console.ReadLine();
}
public void PrintName()
{
Console.WriteLine("The name of the employee entered was:");
Console.WriteLine(Name);
}
}
然后就是这样:
public class EmployeeTest
{
public static void Main(string[] args)
{
//create Employee object myEmployee and pass string to constructor
var myEmployee01 = new Employee("Bob");
var myEmployee02 = new Employee("Alice");
myEmployee01.SetName();
myEmployee02.SetName();
myEmployee01.PrintName();
myEmployee02.PrintName();
//Console.WriteLine("The name of the first employee was: ", myEmployee01.Name);
//Console.WriteLine();
//Console.WriteLine("The name of the second employee was: ", myEmployee02.Name);
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}