我正在嘗試爲類創建一個設置,因此創建時可以爲其設置管理器類,並且可能需要通過接口調用該類中的函數在C中使用接口的問題#
這些調用並不總是必需的,並且管理器可能並不總是要調用此類的類,所以簡單的返回值並將其用於管理器類並不符合要求。
我想要做的是以下代碼。 (試圖剝離儘可能多的不必要的)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
interface Itester
{
void LoadTest();
}
public partial class Form1 : Form, Itester
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
World testWorld = new World();
testWorld.SetManager(this);
testWorld.InitializeWorld();
}
void Itester.LoadTest()
{
//random action I want run
label1.Text = "Ran LoadTest()";
}
}
public class World
{
Itester worldManager;
public World()
{
InitializeWorld();
}
private void InitializeWorld()
{
worldManager.LoadTest();
}
public void SetManager(Itester test)
{
worldManager = test;
}
}
}
我得到這個錯誤的基礎上。錯誤指的是「世界級的公共空間」
錯誤1不一致的輔助功能:參數類型'WindowsFormsApplication1.Itester'比方法'WindowsFormsApplication1.World.SetManager(WindowsFormsApplication1.Itester)'XYZ位置Form1更難以訪問。 cs 316 21封建世界
我本來預料會發生的是這樣的。然後它將自己設置爲它的管理器(理論上可以是實現Itester接口的另一個類),最後它調用World Class(World Class)再請求它初始化這個世界,我希望這個世界類可以調用Form1實例並讓它更新按鈕上的標籤。
LVL1:Form1的 - > LVL2:世界 - > LVL3:Form1的(或其他經理) - >返回空隙,以LVL2:世界 - >返回空隙,以LVL1:Form1中
我缺少什麼,爲什麼會出現這種不行?
+1只是打敗了我:-) – 2012-08-13 21:06:49
+1 - 基本上,如果有人在他們的項目中包含這個DLL,他們會看到一個'World'類,它有一個'SetManager(??? test)'方法,因爲「ITester」不公開。 – Tejs 2012-08-13 21:06:53
所以這不是我在做什麼的邏輯,而是因爲我選擇了一位公共經理,所以界面也必須是公開的?你們中的任何一個人都有我可以閱讀的鏈接,或者至少我應該在谷歌上閱讀這個鏈接? ...是的,這工作謝謝,現在我只需要弄清楚爲什麼它的工作:) – Taoh 2012-08-13 21:10:19