C#访问堆栈对象属性

发布于 2025-01-28 18:26:42 字数 282 浏览 2 评论 0原文

在这种情况下,如何访问对象属性?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花桑 2025-02-04 18:26:42

在这里,您将asd.pop()的值分配给类型对象的变量。

对象是所有对象的根(所有对象都从中继承并可以施加到其上),因此没有关于其是什么的真实信息。就像现实生活中的任何对象都是thing一样。

这里的解决方案是将araba2声明为类型araba,这将使您访问下一行上的所有属性。

我不知道arraytypedstack的实现以及pop()方法的样子(它是返回类型),因此这可能会给您带来错误,说它不能将对象转换为阿拉伯型。这是.NET中实现的类型安全性。您必须说服.NET是类型Araba

Araba araba2 = (Araba)asd.Pop();

如果对象从pop() isn''''对象返回,则可以通过施放此操作来在运行时出现错误。 t类型Araba,在这种情况下,您可以要求.net尝试施放它,因此有服务器选项:

object popResult = asd.Pop();
if (popResult is Araba) {
  Araba araba2 = (Araba)popResult;
}

// can be written as follows:
if (popResult is Araba araba3) {
  araba3.fiyat = 65432;
}

// can also be done as follows
Araba araba4 = asd.Pop() as Araba;
if (araba4 != null) {
  araba4.fiyat = 84368;
}

Here you are assigning the value of asd.Pop() to a variable of the type object.

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 a thing.

The solution here is to declare the araba2 as the type Araba, that will give you access to all the properties on the next line.

I don't know the implementation of the ArrayTypedStack and what the Pop() 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 type Araba, this can be by casting

Araba araba2 = (Araba)asd.Pop();

this can still give an error on runtime if the object returned from Pop() isn't of the type Araba, in this case you can ask .NET to try to cast it, there are serveral options for this:

object popResult = asd.Pop();
if (popResult is Araba) {
  Araba araba2 = (Araba)popResult;
}

// can be written as follows:
if (popResult is Araba araba3) {
  araba3.fiyat = 65432;
}

// can also be done as follows
Araba araba4 = asd.Pop() as Araba;
if (araba4 != null) {
  araba4.fiyat = 84368;
}
攒眉千度 2025-02-04 18:26:42

好吧,您的 araba2 变量是类型对象。因此,无论其包含的实例的实际类型如何,都可以通过对象 actibal araba2 您只能访问由类型object 。

要访问Araba类型的访问成员(并假设 araba2 变量中的实例是类型araba的实例), Araba2 变量本身应为类型Araba,或 araba2 的值需要以araba的形式施加。

因此,

var araba2 = asd.Pop();

var araba2 = (Araba) asd.Pop();

使用上面的第一个示例代码行,要求 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 the object variable araba2 you can only access members that are provided by the type object.

To access members provided by the Araba type (and assuming the instance in the araba2 variable is an instance of type Araba), the araba2 variable itself should be of type Araba, or the value of araba2 needs to be cast as Araba.

Thus,

var araba2 = asd.Pop();

or

var araba2 = (Araba) asd.Pop();

with the first example code line above requiring that the return type of the Pop method is Araba (or a type derived from Araba). The latter code line example will work regardless of the return type of Pop as long as the value returned by Pop is an actual Araba instance (or is something that is convertible to an Araba instance).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文