将您想要从 class1 中获取的数据字段标记为公开。然后,在 class2 中,基于 class1 实例化一个对象,您将可以访问这些数据字段。您可以通过应用内置的 .ToString() 方法来转换浮点数和点数。您需要遍历列表,然后调用 IntPoint.ToString() 方法。
class class1
{
public AForge.Point center;
public float radius;
public List<IntPoint> corners;
private void ProcessImage( Bitmap bitmap )
{
...
}
}
class class2
{
class1 myClass1 = new class1();
private void setTextLabel()
{
label1.Text = myClass1.center.ToString();
label1.Text += myClass1.radius.ToString();
foreach (IntPoint ip in myClass1.corners)
{
label1.Text += ip.X.ToString();
label1.Text += ", ";
label1.Text += ip.Y.ToString();
}
}
}
下面找到了一个完整的工作实现。我将其作为一个网络项目进行。
Class1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AForge;
namespace stack_question
{
public class Class1
{
public AForge.Point center;
public float radius;
public List<IntPoint> corners;
public Class1()
{
center = new AForge.Point(3.3F, 4.4F);
radius = 5.5F;
corners = new List<IntPoint>();
corners.Add(new IntPoint(6, 7));
corners.Add(new IntPoint(8, 9));
}
}
}
Class2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AForge;
namespace stack_question
{
public partial class Class2 : System.Web.UI.Page
{
Class1 myClass1 = new Class1();
protected void Page_Load(object sender, EventArgs e)
{
setTextLabel();
}
private void setTextLabel()
{
label1.Text += "Center: " + myClass1.center.ToString() + "<br/>";
label1.Text += "Radius: " + myClass1.radius.ToString() + "<br/>";
foreach (IntPoint ip in myClass1.corners)
{
label1.Text += "IntPoint: " + ip.X.ToString();
label1.Text += ", ";
label1.Text += ip.Y.ToString() + "<br/>";
}
}
}
}
最后,Class2.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Class2.aspx.cs" Inherits="stack_question.Class2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="label1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
网页上的输出如下所示:
Center: 3.3, 4.4
Radius: 5.5
IntPoint: 6, 7
IntPoint: 8, 9