如何在c#中使用子类的实例访问父类的方法

时间:2013-08-14 13:03:06

标签: c#

我有这段代码

class parent 
{
  public void sleep()
   {
     // Some Logic
   }
}

class Child : Parent
{
  public void sleep()
  {
    // some logic
  }
}

class Implement
{
  Child ch = new Child();
  ch.sleep();
}

但是现在我想通过使用已经创建的子类的实例来访问父类的sleep()方法。

6 个答案:

答案 0 :(得分:9)

您可以将对象转换为父类型。

Child ch = new Child();
var parent = (Parent)ch;
parent.sleep();

答案 1 :(得分:2)

您只需将创建的Child对象转换为Parent

((Parent)ch).sleep();

正如 @Thorsten 在下面做出评论,这是有效的,因为Parent.sleep是一个非虚拟方法,并且它不是重写 Child课程。Parent.sleep。如果它被覆盖,那么您将无法使用ch调用virtual实现。 (对于override方法,调用的方法是“派生程度最高的实现”,即使用{{1}}关键字在类层次结构中派生此方法的最多实现。)

答案 2 :(得分:1)

要访问父成员,您必须强制转换对象。

public class A
{
   public virtual void One();
   public void Two();
}

public class B : A
{
   public override void One();
   public new void Two();
}

B b = new B();
A a = b as A;

a.One(); // Calls implementation in B
a.Two(); // Calls implementation in A
b.One(); // Calls implementation in B
b.Two(); // Calls implementation in B

来源:new keyword in method signature

建议:你隐藏了一个继承的成员。您应该使用“new”关键字:

    class Parent
    {
        public void MethodA()
        {
            Console.WriteLine("Parent");
        }
    }

    class Child : Parent
    {
        public new void MethodA()
        {
            Console.WriteLine("Child");
        }
    }

答案 3 :(得分:0)

您可以在Child上创建一个名为ParentSleep的新方法,如下所示:

class Child : Parent
{
  public void sleep()
  {
    // some logic
  }

  public void ParentSleep()
  {
     base.sleep();
  }
}

然后这样称呼它:

Child ch = new Child();
ch.ParentSleep();

答案 4 :(得分:0)

如果你想用interface和Parent Class实现它,你可以这样做:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExplicitInterfaceImplementation
{
    class Program
    {
        static void Main(string[] args)
        {
            SampleClass sc = new SampleClass();
            var mc = (MainSampleClass)sc;
            IControl ctrl = (IControl)sc;
            ISurface srfc = (ISurface)sc;

            mc.Paint();
            sc.Paint();
            ctrl.Paint();
            srfc.Paint();
            Console.ReadKey();
        }
    }

    /// <summary>
    /// Interface 1
    /// </summary>
    interface IControl
    {
        void Paint();
    }

    /// <summary>
    /// Interface 2
    /// </summary>
    interface ISurface
    {
        void Paint();
    }

    /// <summary>
    /// Parent/Main Class 
    /// </summary>
    public class MainSampleClass
    {
        /// <summary>
        /// Parent class Paint Method.
        /// </summary>
        public void Paint()
        {
            Console.WriteLine("Paint method in - Parent MainSampleClass");
        }
    }

    /// <summary>
    /// SampleClass is the Child class inherited from Parent/Main Class and two interfaces
    /// Parent/Main class having a Paint() method and two interfaces having 
    /// Paint() method - each of them having same name but they are not same(different from each other).
    /// </summary>
    public class SampleClass : MainSampleClass,IControl, ISurface
    {
        /// <summary>
        /// new method(Paint()) for Child class, separate from parent class(Paint() method)
        /// </summary>
        public new void Paint()
        {
            Console.WriteLine("Paint method in - Child SampleClass");
        }

        /// <summary>
        /// Implementing IControl.Paint() method.
        /// </summary>
        void IControl.Paint()
        {
            System.Console.WriteLine("Paint method in - IControl Interface");
        }

        /// <summary>
        /// Implementing ISurface.Paint() method. 
        /// </summary>
        void ISurface.Paint()
        {
            System.Console.WriteLine("Paint method in - ISurface Interface");
        }
    }
}

答案 5 :(得分:0)

class Implement
{
    Parent parentChild = new Child();
    parentChild.sleep();
}