无法使用基类实例调用派生类中的方法 [英] cannot call method in derived class with base class instance

查看:97
本文介绍了无法使用基类实例调用派生类中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用基类实例调用派生类中的方法。

以下代码给出了编译时错误。

I am not able to call method in derived class with base class instance.
Following code giving me compile time error.

class Parent
    {
        public void Display()
        {

        }
    }

    class Child : Parent
    {
        public void Display1()
        {

        }
    }

    class MyDemo
    {
        public static void Main()
        {
            Child c1 = new Child();
            c1.Display();

            Parent p1 = new Parent();
            p1.Display1();

        }
    }

推荐答案

继承意味着子类可以访问它的父级的功能类但反向不是全部(父类无法访问其子类的功能)



以下代码将重新出错:因为您想要访问子类通过父类对象的功能,这是不允许的。



Inheritance Means "Child Class Can Access The Functionlity Of It's Parent Class" But Reverse Is Not Alllowed (Parent Class Can Not Access The Functionlity Of It's Child Class)

The following Code Will Reise Error: Because you want to access child class function through parent class object, which is not allowed.

Parent p1 = new Parent();
p1.Display1();





您只能这样访问:





You can access only like this :

Child c1 = new Child();
c1.Display1();


我认为你通过望远镜的错误端来看待类的继承。 br />


是的,派生类有立即访问它们派生自的基类中的对象和方法... if ... Base Class 通过使用修饰符来公开这些对象和方法,例如'public。



还有另外两种方式派生类可以使用其基类的对象和方法......初学程序员通常无法清楚地理解这些方法:



1.派生类的实例可以调用方法,并设置/获取其基类的公共变量/字段,即使基类的实例已经从未创建。



2.特殊限定符base后跟一个点和基类中公开暴露的变量/字段或方法的名称:是派生类可以使用其Base的另一种方式CLAS s'资源。



base.SomePublicNameInBaseClass不能级联:如果:



Class3派生自Class1派生自Class1:并且,在Class3中编写:base.base.SomePublicNameInBaseClass这是一个错误。



Base.SomePublicNameInBaseClass在Class3中将使用第一个SomePublicNameInBaseClass它在其继承链中找到。



这里有一个小代码示例供您试用,看看它是否有助于您了解继承:
I think you are kind of looking at inheritance of Classes through the "wrong end of the telescope."

Yes, derived classes have immediate access to objects and methods in the Base Classes they derive from ... if ... the Base Class exposes those objects and methods by the use of modifiers, like 'public.

There are two other ways a derived Class can use its Base Class' objects and methods ... these ways are often not clearly understood by beginning programmers:

1. an instance of a derived Class can call methods, and set/get public variables/fields, of its Base Class even if an instance of the Base Class has never been created.

2. the special qualifier "base" followed by a dot and the name of publicly exposed variable/field, or method, in the Base Class: is another way the derived Class can use its Base Class' resources.

"base.SomePublicNameInBaseClass" cannot be "cascaded:" if:

Class3 derives from Class2 which derives from Class1: and, in Class3 you write: base.base.SomePublicNameInBaseClass that's an error.

base.SomePublicNameInBaseClass in Class3 will use the first SomePublicNameInBaseClass that it finds in its inheritance chain.

Here's a little code sample for you to try out and see if it helps you understand inheritance:
public class Class1
{
    public Class1() { }

    private string mySecret = "shhhhh";

    public string notASecret = "no secret here";

    public void AllDerivedClassesCanUseMe()
    {
        Console.WriteLine("in Class1: all derived Classes can use me !");
    }

    private void NoDerivedClassCanUseMe()
    {
        Console.WriteLine(mySecret + " " + "derived Classes cannot use me !");
    }

    public void hello()
    {
        NoDerivedClassCanUseMe();
        Console.WriteLine("hello from Class1");
    }
}

public class Class2 : Class1
{
    public Class2() { }

    public void hello()
    {
        AllDerivedClassesCanUseMe();

        base.hello();

        Console.WriteLine("hello from Class2");
    }
}

public class Class3 : Class2
{
    public Class3() { }

    public void hello()
    {
        base.notASecret = "Class 3 knows a secret in Class1";

        AllDerivedClassesCanUseMe();

        base.hello();

        Console.WriteLine("hello from Class3");
    }
}

public class Class4
{
    public Class4() { }

    public void hello()
    {
        // make sure you understand why the next two lines cause error
        // AllDerivedClassesCanUseMe();
        // base.hello();

        Console.WriteLine("hello from Class4");
    }
}

我建议您创建一个VS项目,创建一个这个代码所在的类,然后在启动Project的代码中,执行以下操作:

I suggest you create a VS project, create a Class into which this code goes, and then in your code that launches the Project, do something like this:

//Class1 c1 = new Class1();
//Class2 c2 = new Class2();
Class3 c3 = new Class3();

c3.hello();

尝试将断点放在代码创建Class3实例的位置,然后单步执行代码。



运行项目,并在控制台窗口中检查输出。尝试取消注释不同的调用以创建Class1,Class2,Class3,并查看会发生什么。更改创建类的顺序,看看会发生什么。



最后,基类可能会调用方法或影响其派生类中的对象 if 它对派生类的实例有一个有效的引用,派生的Classes暴露了它们的内部方法或公共使用的对象。这可能是不寻常的,但它是可能的

Try putting a breakpoint right where the code is going to create the instance of Class3, and then single-stepping through the code.

Run the project, and examine the output in the Console Window. Try un-commenting the different calls to create Class1, Class2, Class3, and see what happens. Change the order of creating the Classes and see what happens.

Finally, it would be possible for a Base Class to call methods or affect objects in its derived Classes if it had a valid reference to the instances of the derived Classes, and the derived Classes exposed their internal methods, or objects for public use . That might be unusual, but it is possible.


这篇关于无法使用基类实例调用派生类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆