【发布时间】:2016-11-22 14:20:10
【问题描述】:
感谢 NHMountainGoat 的解答! 实现接口看起来是一个不错的选择,所以我们只实例化了“需要”的方法。 现在看起来像这样: 编辑
class Machine
{
//REM: MachineConnexion is a link to the main server where asking the data
internal linkToPLC LinkToPLC;
public IlinkToPLC ILinkPLC;
public interface IlinkToPLC//Interface to linkPLC
{
Int16 MachineNumIS { get; set; }
}
internal class linkToPLC : IlinkToPLC
{
private Int16 Act_MachineNum;
private List<string> genlnkPLCCanvas;
private List<string> genlnkPLCworkingwith;
static private List<string> ListSymbolNoExist;
private string[] ListToPLClnk = {
"GlobalFolder.PMachine[{0}].",
"GlobalFolder.PMachine[{0}].STATE.",
"GlobalFolder.Machine[{0}].",
"GlobalFolder.Machine[{0}].STATE.",
};
public linkToPLC()//ctor
{
genlnkPLCCanvas = new List<string>(ListToPLClnk);
genlnkPLCworkingwith = new List<string>(ListToPLClnk);
ListSymbolNoExist = new List<string>();
Act_MachineNum = MachineNumIS;
}
public Int16 MachineNumIS { get { return (Int16)ReadWriteMachine("data"); } set { ReadWriteMachine("data", value); } }
public string ValueExist(string ValueToreach, bool WorkingDATA = false)
{
if (!WorkingDATA)
{
for (int inc = 0; inc < genlnkPLCworkingwith.Count; inc++)
{
string StrValueToReach = genlnkPLCworkingwith[inc] + ValueToreach;
if (MachineConnexion.SymbolExists(StrValueToReach))
{
ListSymbolNoExist.Clear();
return StrValueToReach;
}
else ListSymbolNoExist.Add(genlnkPLCworkingwith[inc] + ValueToreach);
}
}
else if (WorkingDATA)
{
string StrValueToReach = genlnkPLCworkingwith[10] + ValueToreach;
if (MachineConnexion.SymbolExists(StrValueToReach))
{
ListSymbolNoExist.Clear();
return StrValueToReach;
}
else ListSymbolNoExist.Add(genlnkPLCworkingwith[10] + ValueToreach);
}
if (ListSymbolNoExist.Count != 0)
{
string ErrorList = "";
for (int inc = 0; inc < ListSymbolNoExist.Count; inc++)
{
ErrorList = string.Concat(ErrorList + "Num: " + inc.ToString() + " " + ListSymbolNoExist[inc].ToString() + "\n");
}
Console.WriteLine("Error" + ErrorList);
}
return null;
}
public object ReadWriteMachine(string VariableName, object DataToWrite = null, bool WorkingDATA = false)
{
string valueToFind = "";
if (ValueExist(VariableName) != "FALSE")
{
if (DataToWrite != null) { MachineConnexion.WriteSymbol(valueToFind, DataToWrite); }
return MachineConnexion.ReadSymbol(valueToFind);
}
return VariableName;
}
}
public Machine() //constructor
{
LinkToPLC = new linkToPLC();
}
}
并且告诉我引用对象没有定义到对象的实例.....在以下行中不起作用: Machine() LinkToPLC = new linkToPLC();//REM 我发现了这个错误,是我;o)) 24112016
//REM 24112016
这两个概念之间的主要区别是什么:静态实例和接口?
例子:
class Program
{
static void Main(string[] args)
{
ITestInterface InterInstance = new TestInterface();
//test Interface
bool value1 = true;
value1 = InterInstance.invert(value1);
InterInstance.print(value1);
//test Instance static
TestStaticInstance staticInstance = new TestStaticInstance();
staticInstance.Instance.invert(value1);
staticInstance.Instance.print(value1);
Console.ReadKey();
}
}
class TestInterface : ITestInterface
{
public bool invert(bool value)
{
return !value;
}
public void print(bool value)
{
Console.WriteLine(value.ToString()+"\n");
}
private void methodX()
{ }
}
interface ITestInterface
{
bool invert(bool value);
void print(bool value);
}
public class TestStaticInstance
{
public TestStaticInstance Instance;
public TestStaticInstance()
{
Instance = this;
}
internal bool invert(bool value)
{
return !value;
}
internal void print(bool value)
{
Console.WriteLine(value.ToString());
}
}
谢谢
【问题讨论】:
-
不要将继承视为代码重用的工具。有很多方法可以做到这一点 - 组合,实用程序类,静态方法等。继承意味着“is-a”关系 - 如果
otherstuffis alinktoPLC那么继承可能是合适的。如果它只是包含一个或需要一个 PLC,那么组合可能是正确的选择。 -
没有副本 - 每个
linktoPLC实例,包括needanotherone,都有自己的genlnkPLCworkingwith集合。考虑一个带有List<Flea> fleas集合的基类Animal- 每个Dog和每个Cat都有自己的跳蚤集合,而myDog.Scratch()只能从他的集合中删除跳蚤。将其设为静态 会 导致该集合仅存在一次 - 但我不确切知道您要实现的目标。 -
再次阅读您的问题我很困惑-您是在问
needanotherone和LinkToPLC是否共享genlnkPLCworkingwith引用的列表?答案是否定的。由于您询问virtual和override,我不确定您要做什么-virtual和override适用于方法,而不适用于数据 i>. -
请注意,这是合法的,您将通过 weach 变量引用相同的列表:
linktoPLC LinkToPLC = new linktoPLC(); otherstuff needanotherone = LinktoPLC; -
如果您不想在副本中进行更改,而是想更改主变量(列表),只需将该变量设为静态,这样就不允许为任何实例创建新副本。
标签: c# inheritance static-methods