C# 需要帮助理解多态性吗
以下课程包括 父亲班 B-儿童班 持有者-包含a的列表 我想从FatherObject列表中访问子属性。为什么我不能这样做?或者更好的问题,我该怎么做C# 需要帮助理解多态性吗,c#,polymorphism,C#,Polymorphism,以下课程包括 父亲班 B-儿童班 持有者-包含a的列表 我想从FatherObject列表中访问子属性。为什么我不能这样做?或者更好的问题,我该怎么做 public class A { public int var = 0; } public class B : A { public int Property1 { get; set; } public int Property2 { get; set; } public B() { }
public class A
{
public int var = 0;
}
public class B : A
{
public int Property1 { get; set; }
public int Property2 { get; set; }
public B()
{
}
public B(B p_B)
{
Property1 = p_B.Property1;
Property2 = p_B.Property2;
}
}
class Holder
{
private List<A> m_Objects = new List<A>();
public void AddObject(A p_Object)
{
m_Objects.Add(p_Object);
}
public void AddObjectProperty1(B p_B)
{
// At this point, m_Objects holds a B-object. And I want to add the value from Property1
// but there is no Property1 in the A-class so I cant do this. How do I use the base.values from
// a statement like the one below?
int index = m_Objects.FindIndex(item => item.Property1 == p_B.Property1);
if (index > -1)
m_Objects.ElementAt(index).Property1 += p_B.Property1;
}
}
class Program
{
static void Main(string[] args)
{
// Class to hold the objects
Holder h = new Holder();
// Create a B object
B b = new B();
b.Property1 = 1;
b.Property2 = 2;
// Place a new instance of the B-object in a list of A's
h.AddObject(new B(b));
// Add the value from Property1 to the value in the b-object in the a-list. :P
h.AddObjectProperty1(b);
Console.WriteLine(++b.var);
Console.ReadLine();
}
}
公共A类
{
公共int var=0;
}
B级:A级
{
公共int属性1{get;set;}
公共int属性2{get;set;}
公共图书馆B()
{
}
公共B(B pÈB)
{
属性1=p_B.属性1;
Property2=p_B.Property2;
}
}
阶级持有者
{
私有列表m_对象=新列表();
公共void AddObject(一个p_对象)
{
m_Objects.Add(p_Object);
}
public void AddObjectProperty1(B p_B)
{
//此时,m_对象持有一个B对象,我想添加Property1中的值
//但是A类中没有属性1,所以我不能这样做。如何使用中的base.values
//像下面这样的陈述?
int index=m_Objects.FindIndex(item=>item.Property1==p_B.Property1);
如果(索引>-1)
m_Objects.ElementAt(index).Property1+=p_B.Property1;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
//类来保存对象
保持架h=新保持架();
//创建一个B对象
B=新的B();
b、 房地产1=1;
b、 房地产2=2;
//将B对象的新实例放置在a对象的列表中
h、 AddObject(新的B(B));
//将Property1中的值添加到a列表中b对象中的值。:P
h、 AddObjectProperty1(b);
Console.WriteLine(++b.var);
Console.ReadLine();
}
}
您可以使用类型转换:
(m_Objects[i] as B).Property1
或
在编译时,编译器不可能知道,您只需要将
B
s添加到A
s列表中。因此,无法保证以下查询中的每个项
都是B
的实例,因此具有属性1
int index = m_Objects.FindIndex(item => item.Property1 == p_B.Property1);
第一种可能性是投法,如Artyom的回答所示。但如果列表中并非所有元素都是B
s,则此操作将失败。因此,如果您依赖m_对象中的所有元素作为B
的实例,为什么不直接使用列出m_对象
如果需要混合列表,则必须在查询中进行类型检查,以确保在强制转换之前处理的是B
的实例
int index = m_Objects.FindIndex(item => (item is B) && (item as B).Property1 == p_B.Property1);
参见此不要命名变量var
。首先,它并不意味着什么,其次,var
也是C#中的一个关键字-您的代码将变得凌乱且难以维护。是的,谢谢您的建议!)我通常不给变量命名为var,我只是想在这个特殊情况下尽可能地创建一些通用的变量!但是谢谢你注意到了!为什么这个问题被否决了(这正是我一直在寻找的。我之所以想将B存储在A中,而不是相反,是因为我想使用多态性以我认为智能的方式存储对象(可能不是这样)。例如,如果holderclass是一个钱包。我想存储现金(读作A),可以是硬币或钞票的形式(B或C,如果存在C)。听起来不错,因为我可以有一张现金清单。我尝试了你的解决方案,语法正确,但是在铸造物品时找不到Property1:(有什么线索吗?我是不是想错了?从这个角度来看,使用列表。似乎工作正常。Ty为您解答!我尝试了此解决方案,但属性1未被识别,即使类型转换是正确的。:(什么意思:未被识别。它不会编译或因错误而崩溃,或者得到空值?
int index = m_Objects.FindIndex(item => (item is B) && (item as B).Property1 == p_B.Property1);