클래스 1에서 원하는 데이터 필드를 공개로 표시하십시오. 그런 다음 class2에서 class1을 기반으로 개체를 인스턴스화하면 해당 데이터 필드에 대한 액세스 권한이 부여됩니다. 내장 된 .ToString() 메서드를 적용하여 부동 소수점과 Point를 변환합니다. 목록을 반복하고 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
클래스 2의 변수를 공개하면 해당 변수에 액세스 할 수 있습니다. – Dilshod
하지만 float, Point 및 List를 문자열로 변환 할 때 ??? – user2308516