![](/img/trans.png)
[英]How to access properties that are in an Inherited UserControl in C#?
[英]How to have access to an inherited method in C#?
我是否缺少有关 inheritance 的任何概念? 我正在尝试调用子 class 中的方法,但它不起作用。
这就是我所拥有的,非常简单:
威路士
public class Vuelo
{
private Aeronave _aeronave { get; set; }
public Vuelo(string numero, Aeronave aeronave)
{
ValidarNumero(numero); // validates numero
_aeronave = aeronave;
}
public string modelo_aeronave()
{
return _aeronave.model(); // This is where the error goes, .model()
}
public string RegistroAvion()
{
return _aeronave.Registration(); // This worked perfectly
}
}
Aeronave.cs
public class Aeronave
{
private string _registration { get; set; }
public Aeronave(string registration)
{
_registration = registration;
}
public string Registration()
{
return _registration;
}
}
Airbus319.cs (子类):
public class AirbusA319 : Aeronave
{
private string _model { get; set; }
public AirbusA319(string model, string registro) : base(registro)
{
_model = model;
}
public string model()
{
return _model;
}
}
我想像这样显示model()
中的空客model :
Vuelo vuelo = new Vuelo("AB345", new AirbusA319("G-EUPT", "GG235"));
Console.WriteLine(vuelo.modelo_aeronave());
我无法在 inte.net 中找到解决方案,即使在有关 inheritance 的 Microsoft 文档中也是如此。
您需要修改您的类,如下所示。 Aeronave 应包含 model(虚拟或抽象)以在空中客车中覆盖。
public abstract class Aeronave
{
private string _registration { get; set; }
public Aeronave(string registration)
{
_registration = registration;
}
public string Registration()
{
return _registration;
}
public abstract string model();
}
public class AirbusA319 : Aeronave
{
private string _model { get; set; }
public AirbusA319(string model, string registro) : base(registro)
{
_model = model;
}
public override string model()
{
return _model;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.