简体   繁体   English

在C#中使用对象

[英]Working with objects in c#

In the following example: 在以下示例中:

class Base
{
    public void TestMethod()
    {
        Console.WriteLine("Base Class");
    }
}

class Derived : Base
{
    public void DerivedTestMethod()
    {
        Console.WriteLine("Derived Class");
    }
}

class Demo 
{
    public static void Main()
    {
        Base Obj = new Derived();
        Obj.DerivedTestMethod(); //Error Line
    }
}

Why can't I access the method DerivedTestMethod() in Derived class when I have created the object for Derived class using new ? 使用new为Derived类创建对象时,为什么不能访问Derived类中的DerivedTestMethod()方法?

Your object is of type Derived , meaning that it does indeed have the method DerivedTestMethod . 您的对象是Derived类型的,这意味着它确实具有DerivedTestMethod方法。

Your problem is that the reference to your object is of type Base . 您的问题是对您的对象的引用Base类型的。 Base doesn't have this method and thus the compiler cannot guarantee that calling DerivedTestMethod will make sense as it's not certain that the reference is of type Derived . Base没有此方法,因此编译器无法保证调用DerivedTestMethod会有意义,因为不确定引用是否为Derived类型。


Here's a code example, try using the var keyword, and then newing up the Derived class: 这是一个代码示例,尝试使用var关键字,然后更新Derived类:

class Demo 
{
public static void Main()
    {
        var Obj = new Derived();
        Obj.DerivedTestMethod();
    }
}

If however you still want to explicitely declare it first to the type Base , or when you recieve an object that is in this Base type then you can test whether the variable Obj is in fact of the class Derived by using the C# is keyword 然而,如果你仍然想明确地首先声明它的类型Base ,或当您收到一个对象,它是在这个Base类型,那么你可以测试变量是否Obj是班上的事实Derived使用C# is关键字

if(Obj is Derived) // testing with the "is" keyword
{
    Obj = (Derived)obj; // here we cast it
} 
else
{
    // Other code, but now we know that the "Obj" variable isn't of type "Derived".
}

For more information on testing with the "is" keyword, see the MSDN documentation http://msdn.microsoft.com/en-us/library/scekt9xw.aspx or check this answer: https://stackoverflow.com/a/10416231/1155847 有关使用“ is”关键字进行测试的更多信息,请参见MSDN文档http://msdn.microsoft.com/en-us/library/scekt9xw.aspx或检查以下答案: https : //stackoverflow.com/a/一百一十五万五千八百四十七分之一千零四十一万六千二百三十一

Because you're reference is of type Base. 因为您引用的是Base类型。 Although you're instantiating an object of type Derived, the reference to your object is of type Base and Base does not contain DerivedTestMethod. 尽管您要实例化Derived类型的对象,但是对该对象的引用是Base类型的,并且Base不包含DerivedTestMethod。

So, althogh in memory you have an object of type Derived and you could cast to it and use it as Derived, the CLR will let you call only methods available for the type of reference (Base, in your case). 因此,尽管在内存中您有一个Derived类型的对象,并且可以将其强制转换为Derived并使用它,所以CLR仅允许您调用可用于引用类型的方法(在您的情况下为Base)。

The other answers all contain significant dangers and omissions (eg just trying to cast without testing might be dangerous - unless you really really know that you can cast to the given type). 其他答案都包含重大的危险和遗漏(例如,仅在未经测试的情况下进行转换可能很危险-除非您真的真的知道可以转换为给定的类型)。

Read through this answer in full to get a feel and understanding/insight as to why things weren't happening as you'd wanted it to be. 仔细阅读此答案,以了解和理解/了解为什么事情没有按照您希望的那样进行。

Because you declared it specifcally to the "base" class, try using the "var" keyword, and then newing up the "Derived" class. 因为您是专门针对“基”类声明的,所以请尝试使用“ var”关键字,然后更新“派生”类。

class Demo 
{
public static void Main()
    {
        var Obj = new Derived();
        Obj.DerivedTestMethod();
    }
}

If however you still want to explicitely declare it first to "Base" 但是,如果您仍然想先明确声明为“基础”

Base Obj = new Derived();

then you can test whether the variable "Obj" is in fact of the class "Derived" by using the C# "is" keyword 那么您可以使用C#“ is”关键字来测试变量“ Obj”是否实际上是类“ Derived”

if(Obj is Derived)
{
    Obj = (Derived)obj; // here we cast it
} 
else
{
    // Other code, but now we know that the "Obj" variable isn't of type "Derived".
}

For more information on testing with the "is" keyword, see the MSDN documentation http://msdn.microsoft.com/en-us/library/scekt9xw.aspx or check this answer: https://stackoverflow.com/a/10416231/1155847 有关使用“ is”关键字进行测试的更多信息,请参见MSDN文档http://msdn.microsoft.com/en-us/library/scekt9xw.aspx或检查以下答案: https : //stackoverflow.com/a/一百一十五万五千八百四十七分之一千零四十一万六千二百三十一

With Base Obj = new Derived(); 使用Base Obj = new Derived(); you are creating a new Derived-Object and then assign it to an Object of type Base. 您正在创建一个新的派生对象,然后将其分配给类型为Base的对象。 The Base-Class doesn't have the method. 基本类没有该方法。

If you want to call the method on the Base-Object you can do: 如果要在基础对象上调用方法,则可以执行以下操作:

((Derived)Obj).DerivedTestMethod();

The following should work for you: 以下应该为您工作:

Base Obj = new Derived();
((Dervived)Obj).DerivedTestMethod(); //Error Line

Basically, you are instantiating the base and calling a method from Derived, but you should be able to cast easily enough. 基本上,您正在实例化基础并从Derived调用方法,但是您应该能够足够容易地进行强制转换。

You can achieve it as follows: 您可以通过以下方式实现它:

  Derived Obj = new Derived();
  Obj.DerivedTestMethod(); 

OR 要么

  Base Obj = new Derived();
  ((Derived)Obj).DerivedTestMethod();  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM