如何在子类的父类中调用函数? [英] how call function in class parent in child class?

查看:87
本文介绍了如何在子类的父类中调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在子类的父类中调用函数(方法)?

how call function (method) in class parent in child class?

推荐答案

您当然需要对父类的引用.没有更好的描述,我们将无济于事.
You need a reference to the parent class of course. Without a MUCH better description, we can''t help much beyond that.




根据您的要求(虽然不是很清楚,但我会给它加一个提要),您将看到这样的情况:
Hi,

Based on what you are asking (it wasn''t very clear but I will give it a bash), you will be looking at a scenario like this:
public class MyParentClass
{
    public virtual void SomeMethod()
    {
        /* do parent class stuff here */
    }
}

public class MyChildClass : MyParentClass
{
    public override void SomeMethod()
    {
        /* do child class stuff here */
        base.SomeMethod(); // <--- This will call the parent class method
    }
}



在子类中重写方法的任何时候,您都可以调用" base.SomeMethod()".

希望能对您有所帮助:)...



At any point in the overridden method in the child class you can call "base.SomeMethod()".

Hope that helps :)...


如果要在子类中调用父级的非静态方法,则该方法必须为publicprotected:

If you want to call a none static method of the parent in a child class, the method must be public or protected:

base.MethodName();



如果要在父类中调用子级的none静态方法:
您必须具有子类的实例,并且该方法必须是public方法:



If you want to call a none static method of the child in a parent class:
you have to have an instance of the child class and the method must be a public method:

ChildClass instance = new ChildClass();
instance.MethodName();







Or

(new ChildClass()).MethodName();



最好解释一下为什么要这么做.



It''s better If you explain why do you want to do so.


这篇关于如何在子类的父类中调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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