在 C# 中声明一个对象数组
本文将介绍如何声明或初始化对象数组。使用对象数组允许我们访问每个对象的类方法。
C#
中的对象数组
对象数组可以以多种方式使用;它们将多种组件放在一个集合中。对象引用可以引用任何派生类型实例。
以下是 C# 中对象数组声明的一些示例。
在 C#
中声明具有字符串数据类型的对象数组
语法:
employee[] e = new employee[3];
e[0] = new employee();
e[1] = new employee();
e[2] = new employee();
例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1 {
class employee {
private string empName;
private int empId;
private string empDesig;
public void EmpInfo(string empName, int empId, string empDesig) {
this.empId = empId;
this.empDesig = empDesig;
this.empName = empName;
}
public void showEmployeeDetails() {
Console.WriteLine("\nEmployee Record: ");
Console.WriteLine("\t Emp Name: " + empName);
Console.WriteLine("\t Id : " + empId);
Console.WriteLine("\tDesignation : " + empDesig);
}
}
class EmployeeTest {
public static void Main() {
employee[] e = new employee[3];
e[0] = new employee();
e[1] = new employee();
e[2] = new employee();
e[0].EmpInfo("Shan", 132, "Manager");
e[0].showEmployeeDetails();
e[1].EmpInfo("Casper", 131, "CEO");
e[1].showEmployeeDetails();
e[2].EmpInfo("Olga", 139, "Team Leader");
e[2].showEmployeeDetails();
}
}
}
输出:
Employee Record:
Emp Name: Shan
Id : 132
Designation : Manager
Employee Record:
Emp Name: Casper
Id : 131
Designation : CEO
Employee Record:
Emp Name: Olga
Id : 139
Designation : Team Leader
按照这些步骤,我们可以声明一个对象数组。
- 首先,我们必须导入以下库,它们是必需的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
-
现在,创建一个名为
employee
的类,在该类中,我们将声明employee
的 3 个私有数据成员。
class employee {
private string empName;
private int empId;
private string empDesig;
}
-
在
employee
类中创建一个名为EmpInfo()
的构造函数,并分别传递名为empName
、empId
、empDesig
的 3 个参数。EmpInfo()
是设置employee
对象的私有变量值的 setter 方法,在employee
类之外无法访问这些值。
public void EmpInfo(string empName, int empId, string empDesig) {
this.empId = empId;
this.empDesig = empDesig;
this.empName = empName;
}
-
我们在
employee
类中构建了showEmployeeDetails()
函数来显示’员工’的详细信息。我们构建了这个方法来在控制台上显示employee
数据,因为employee
变量在类外不可用。
由于此函数的可用性,我们可以直接在我们的 employee
对象上调用 showEmployeeDetails()
以在控制台中显示 employee
数据。
public void showEmployeeDetails() {
Console.WriteLine("\nEmployee Record: ");
Console.WriteLine("\t Emp Name: " + empName);
Console.WriteLine("\t Id : " + empId);
Console.WriteLine("\tDesignation : " + empDesig);
}
-
我们将在
Main
类中创建一个对象数组。employee
类的所有对象都将存储在我们构建的数组中。然后我们将创建并初始化两个对象。
之后,我们将调用我们之前在 employee
类中创建的构造函数,并为我们的两个对象的每个变量分配一个值。
然后,在我们的 employee
类中,我们将调用 showEmployeeDetails()
来打印我们在前面的代码块中分配的每个 employee
的值。
class EmployeeTest {
public static void Main() {
employee[] e = new employee[3];
e[0] = new employee();
e[1] = new employee();
e[2] = new employee();
e[0].EmpInfo("Shan", 132, "Manager");
e[0].showEmployeeDetails();
e[1].EmpInfo("Casper", 131, "CEO");
e[1].showEmployeeDetails();
e[2].EmpInfo("Olga", 139, "Team Leader");
e[2].showEmployeeDetails();
}
}
在 C#
中声明具有浮点和整数数据类型的对象数组
语法:
object[] num = new object[5];
num[0] = 2.15;
num[1] = 'S';
num[2] = 27;
例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class DeclareObjectArray {
static public void Main() {
object[] num = new object[5];
// float value
num[0] = 2.15;
// Character
num[1] = 'S';
// integer value
num[2] = 27;
// null value
num[3] = null;
// system object
num[4] = new object();
foreach (var Items in num) {
Console.WriteLine(Items);
}
}
}
输出:
2.15
S
27
System.Object
在 Main
类中,我们将为此示例创建一个对象数组。
我们创建的数组将保存 num
类的所有对象。然后我们将制作 5 个对象并设置它们。
之后,我们将执行我们之前创建的构造函数,并为所有实例的每个变量设置一个值。
相关文章
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:198 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:171 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:187 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中发出 HTTP POST Web 请求
发布时间:2024/02/04 浏览次数:131 分类:编程语言
-
在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求
在 C# 中运行命令提示符命令
发布时间:2024/02/04 浏览次数:130 分类:编程语言
-
Process 类可用于在 C# 中运行命令提示符命令。在 C# 中使用 Process.Start() 函数运行命令提示符命令
在 C# 中调整图像大小
发布时间:2024/02/04 浏览次数:203 分类:编程语言
-
有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保
在 C# 中下载图片
发布时间:2024/02/04 浏览次数:138 分类:编程语言
-
有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据
在 C# 中使用秒表
发布时间:2024/02/04 浏览次数:139 分类:编程语言
-
我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。
在 C# 中获取可执行路径
发布时间:2024/02/04 浏览次数:200 分类:编程语言
-
有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径