C#访问堆栈对象属性
在这种情况下,如何访问对象属性?
Araba araba = new Araba();
araba.Renk = "mavi";
araba.fiyat = 12345;
// I created this class and it working normally
ArrayTypedStack asd = new ArrayTypedStack(10);
asd.Push(araba);
object araba2 = asd.Pop();
araba2. //cant access
How can I access the objects property in this situation?
Araba araba = new Araba();
araba.Renk = "mavi";
araba.fiyat = 12345;
// I created this class and it working normally
ArrayTypedStack asd = new ArrayTypedStack(10);
asd.Push(araba);
object araba2 = asd.Pop();
araba2. //cant access
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里,您将
asd.pop()
的值分配给类型对象
的变量。对象
是所有对象的根(所有对象都从中继承并可以施加到其上),因此没有关于其是什么的真实信息。就像现实生活中的任何对象都是thing
一样。这里的解决方案是将
araba2
声明为类型araba
,这将使您访问下一行上的所有属性。我不知道
arraytypedstack
的实现以及pop()
方法的样子(它是返回类型),因此这可能会给您带来错误,说它不能将对象转换为阿拉伯型。这是.NET中实现的类型安全性。您必须说服.NET是类型Araba
,如果对象从
pop()
isn''''对象返回,则可以通过施放此操作来在运行时出现错误。 t类型Araba
,在这种情况下,您可以要求.net尝试施放它,因此有服务器选项:Here you are assigning the value of
asd.Pop()
to a variable of the typeobject
.object
is the root of all objects (all objects inherit from it and can be casted to it) and as such has no real information about what it is. It's just like any object in real life is athing
.The solution here is to declare the
araba2
as the typeAraba
, that will give you access to all the properties on the next line.I don't know the implementation of the
ArrayTypedStack
and what thePop()
method looks like (it's return type) so it's possible that this will give you an error, saying that it can't convert an object to the type Araba. This is the type safety implemented in .NET. You have to convince .NET that it's of the typeAraba
, this can be by castingthis can still give an error on runtime if the object returned from
Pop()
isn't of the typeAraba
, in this case you can ask .NET to try to cast it, there are serveral options for this:好吧,您的 araba2 变量是类型
对象
。因此,无论其包含的实例的实际类型如何,都可以通过对象
actibal araba2 您只能访问由类型object 。
要访问
Araba
类型的访问成员(并假设 araba2 变量中的实例是类型araba
的实例), Araba2 变量本身应为类型Araba
,或 araba2 的值需要以araba
的形式施加。因此,
或
使用上面的第一个示例代码行,要求 pop 方法的返回类型为
abara
(或从abala
)派生的类型。无论 pop pop 是实际的 pop ,后一个代码行示例都将起作用,无论 pop 的返回类型如何是可转换为Araba
实例的东西)。Well, your araba2 variable is of type
object
. Thus, regardless of the actual type of the instance it contains, through theobject
variable araba2 you can only access members that are provided by the typeobject
.To access members provided by the
Araba
type (and assuming the instance in the araba2 variable is an instance of typeAraba
), the araba2 variable itself should be of typeAraba
, or the value of araba2 needs to be cast asAraba
.Thus,
or
with the first example code line above requiring that the return type of the Pop method is
Araba
(or a type derived fromAraba
). The latter code line example will work regardless of the return type of Pop as long as the value returned by Pop is an actualAraba
instance (or is something that is convertible to anAraba
instance).