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

本文关键字:访问 父类 方法 实例 子类 何用 | 更新日期: 2023-09-27 18:27:50

我有这个代码

class parent 
{
  public void sleep()
   {
     // Some Logic
   }
}
class Child : Parent
{
  public void sleep()
  {
    // some logic
  }
}
class Implement
{
  Child ch = new Child();
  ch.sleep();
}

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

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

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

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

您只需要将创建的Child对象强制转换为Parent:

((Parent)ch).sleep();

正如@Thorsten在下面评论的那样,这之所以有效,是因为Parent.sleep是一个非虚拟方法,并且它在Child类中没有被覆盖。如果它被覆盖,那么就没有办法使用ch调用Parent.sleep实现。(对于virtual方法,调用的方法是"最派生的实现",即在具有override关键字的类层次结构中该方法的最派生的实施。)

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

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

来源:方法签名中的新关键字

建议:你隐藏了一个继承的成员。你应该使用这样的"新"关键字:

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

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

class Child : Parent
{
  public void sleep()
  {
    // some logic
  }
  public void ParentSleep()
  {
     base.sleep();
  }
}

然后这样称呼它:

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

如果你想用接口和父类实现这一点,你可以这样做:

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");
        }
    }
}
class Implement
{
    Parent parentChild = new Child();
    parentChild.sleep();
}