你将如何从一个类继承并具有其示例
中的单个属性的倍数这是实际的代码(So Far)
class NetWorkInterface
{
public double Vlan;
public string Ip;
}
class Port : NetWorkInterface
{
public double PortNumber;
}
public void test ()
{
Port newport = new Port();
newport.PortNumber = 7;
newport.Vlan = 100
}
我希望能够将多个接口添加到一个端口
答案 0 :(得分:3)
我不认为你应该在这里使用继承。
如果您希望Port
包含NetworkInterfaces
的集合,那么Port
的更好定义可能是:
class Port
{
public double PortNumber;
public NetworkInterface[] networkInterfaces;
}
答案 1 :(得分:0)
关闭的事情
public class Port
{
public double PortNumber { get; set; }
public IEnumerable<NetworkInterface> NetworkInterfaceList { get; set; }
}
答案 2 :(得分:0)
您可以为每个类继承一个类(在C#中没有多重继承),或者实现多个接口,或者同时执行这两个类
public interface NetWorkInterface1
{
double Vlan {get; set;}
}
public interface NetWorkInterface2
{
string Ip {get; set;}
}
class Port : A /* (inherit class before interface) port inherits A & A inherits B*/, NetWorkInterface1, NetWorkInterface2 /* implements interface 1 & 2*/
{
public double PortNumber;
public double Vlan {get; set;}
public string Ip {get; set;}
}
public class A : B /* A inherits B */
{
public string AStuff;
}
public class B
{
public string BStuff;
}
public void test ()
{
Port newport = new Port();
newport.PortNumber = 7;
newport.Vlan = 100;
newport.AStuff = "A";
newport.BStuff = "B";
}