继承 - 初学者的问题 [英] Inheritance - beginners question

查看:65
本文介绍了继承 - 初学者的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





根据以下类别,有什么区别

 ClassBase ObjectBase =  new  ClassDerived(); 
ObjectBase.Show(); // 输出---这是派生类。





 ClassDerived ObjectDerived =  new  ClassDerived(); 
ObjectDerived.Show(); // 输出---这是派生类。





  public   class  ClassBase 
{
public virtual void Show()
{
MessageBox.Show( 这是基类);
}
}

public class ClassDerived: ClassBase
{
public override 密封 void 显示()
{
MessageBox.Show( 这是派生类);
}
}

解决方案

从功能的角度来看都没有,因为你的两个类都有相同的方法。不同之处在于,如果派生类具有基类没有的方法,那么在第一种情况下你将无法在没有强制转换的情况下访问它们。



< pre lang =c#>((DerivedClass)ObjectBase)。SomeDerivedMethod()


//如果你想知道以下片段后面的区别



 使用系统; 

命名空间 ConsoleApplication1
{
public class ClassBase
{
public virtual void 显示()
{
Console.WriteLine( < span class =code-string>这是基类);
}
}

public class ClassDerived: ClassBase
{
public override 密封 void 显示()
{
Console.WriteLine( 这是派生类);
}
}
class 计划
{
/ / 如果您想了解以下代码段后面的差异
static void Main()
{
ClassBase baseClass = new ClassBase();
ClassBase derviedClass = new ClassDerived(); // 请注意,我也在派生类中使用基类引用。

Console.WriteLine( Case1:子类重写基类方法。);
// 案例1:子类覆盖基类的虚方法。
Console.WriteLine( 访问基类对象:);
baseClass.Show(); // 这是基类
Console.WriteLine( 访问派生类对象:);
derviedClass.Show(); // 这是派生类


// Case2:子类不会覆盖基类方法。从ClassDerived的'Show'方法中删除'override'和'sealed'限定符,然后运行此部分。
Console.WriteLine( < span class =code-string> Case2:子类没有覆盖基类方法。);
Console.WriteLine( 访问基类对象:);
baseClass.Show(); // 这是基类
Console.WriteLine( 访问派生类对象:);
derviedClass.Show(); // 这是基类

// 在case2中,如果你想在使用基类对子类对象的引用时从子类调用show方法,那么你必须
// 执行如下所示的操作。
Console.WriteLine( 访问子类对象(子类没有重写基类方法) 。));
((ClassDerived)derviedClass)。Show(); // 这是派生类

Console.WriteLine( 使用子类方法中使用的'new'限定符来访问基类和子类对象以隐藏基类方法。:);
// 如果你想检查真正的差异,那么可以使用new运算符。
// 在上面的例子中使用'new'代替'override'并删除'sealed'关键字。
// 现在如果你运行相同的两行输出如下
Console.WriteLine( 访问基类对象:);
baseClass.Show(); // 这是基类
Console.WriteLine( 访问子类对象类对象:);
derviedClass.Show(); // 这是基类

Console.WriteLine( 使用类型转换为基类和子类访问子类对象(当子类没有重写基类方法时));
// 此外,如果您执行此类操作(假设子类未覆盖该方法)
((ClassBase) new ClassDerived())。Show(); // 这是基类
// 但是如果你在子类中有overriden方法,那么输出将如下所示。
((ClassDerived) new ClassDerived())。Show(); // 这是派生类

// 总结一下,我们有以下内容。
// 1。当一个虚拟方法(不是由子类覆盖)但只是使用带有或不带有新关键字的子类实现的情况发生时
// i。如果使用基类引用引用子类对象,则将调用基类的方法。例如。输出 - 这是基类
// ii。如果您使用的是子类对象子类引用然后将调用子类方法。例如。输出 - 这是派生类
// iii。您正在使用子类引用访问子类对象,但将其类型转换为基类引用,然后它将调用基类的方法。例如。输出 - 这是基类
// 2。当在子类中覆盖虚方法时,在所有情况下输出
// '这是派生类'。因此,当您重写子类中的方法时,无论您如何尝试在子类对象中访问它(使用基类引用或子类引用),只会调用子类的方法,而不是从基类调用它。它是位的长期解释,但我希望这有助于通过入住了解运行时多态性。
}




// 我希望这可以解决这个混乱......
}
}


Hi,

Based on the classes below, what's the difference between

ClassBase ObjectBase = new ClassDerived();
ObjectBase.Show();  // Output --- This is Derived Class.


and

ClassDerived ObjectDerived = new ClassDerived();
ObjectDerived.Show();  // Output --- This is Derived Class.



public class ClassBase
   {
     public virtual void Show()
        {
            MessageBox.Show("This is Base Class");
        }
    }

public class ClassDerived : ClassBase
   {
     public override sealed void Show()
        {
           MessageBox.Show("This is Derived Class");
        }
   }

解决方案

Nothing from the standpoint of functionality since both your classes have the same methods. The difference would be if the derived class had methods that the base class did not, you would not be able to access them in the first case without casting it.

((DerivedClass)ObjectBase).SomeDerivedMethod()


// If you want to know the difference run following snippet

using System;

namespace ConsoleApplication1
{
    public class ClassBase
    {
        public virtual void Show()
        {
            Console.WriteLine("This is Base Class");
        }
    }

    public class ClassDerived : ClassBase
    {
        public override sealed void Show()
        {
            Console.WriteLine("This is Derived Class");
        }
    }
    class Program
    {
        // If you want to know the difference run following snippet
        static void Main()
        {
            ClassBase baseClass = new ClassBase();
            ClassBase derviedClass = new ClassDerived();//Note that i am using base class reference in derived class as well.

            Console.WriteLine("Case1: Child class is overriding the base class method.");
            //Case 1: child class is overriding virtual method from base class. 
            Console.WriteLine("Accessing base class object:");
            baseClass.Show();//"This is Base Class"
            Console.WriteLine("Accessing derived class object:");
            derviedClass.Show(); //"This is Derived Class"


            //Case2: Child class is not overriding base class method. Remove 'override' and 'sealed' qualifiers from 'Show' method of ClassDerived, then run this part.
            Console.WriteLine("Case2: Child class is not overriding the base class method.");
            Console.WriteLine("Accessing base class object:");
            baseClass.Show();//"This is Base Class"
            Console.WriteLine("Accessing derived class object:");
            derviedClass.Show(); //"This is Base Class"

            //In case2 if you want call show method from child class when you are using a reference of base class to child class object then you have to
            //do something like shown below.         
            Console.WriteLine("Accessing child class object (child class has not overridden base class method.)");
            ((ClassDerived)derviedClass).Show(); //"This is Derived Class"            

            Console.WriteLine("Accessing base and child class objects with 'new' qualifier used in child class method to hide base class method.:");
            //If you want to check real difference then one can use new operator.
            //Use 'new' instead of 'override' in above example and remove 'sealed' keyword.
            // Now if you run same two lines output is as follows
            Console.WriteLine("Accessing base class object:");
            baseClass.Show();//"This is Base Class"
            Console.WriteLine("Accessing child class object class object:");
            derviedClass.Show(); //"This is Base Class"

            Console.WriteLine("Accessing the child class object using typecasting to base and child class (when child class has not overridden the base class method)");
            //Also if you do something like this (assuming that child class is not overriding the method)
            ((ClassBase)new ClassDerived()).Show(); //"This is Base Class"
            //But if you have overriden method in child class then output would be as follows.
            ((ClassDerived)new ClassDerived()).Show(); //"This is Derived Class" 

            //To summarise we have following.
            //1. When a virtual method which is (not overriden by child class) but just implemented using child class with or without new keyword following scenarios happen
            //     i. If you are referring child class object using base class reference then base class' method will be invoked. eg. output - This is Base Class
            //     ii.If you are referring child class object using child class reference then child class method will be invoked.  eg. output - This is Derived Class
            //     iii. You are accessing child class object using child class reference but typecasting it to base class reference then it will invoke base class' method. eg. output - This is Base Class
            //2. When a virtual method is overriden in child class then in all the cases output is 
            //'This is Derived Class'. So when you override a method in child class then no matter how you try to access it in child class object(using base class reference or child class reference) only child class' method will be invoked and not from the base class.It is bit long explaination but i hope this helps in understanding runtime polymorphism through inhertance.
        }




        //I hope this clears the confusion...
    }
}


这篇关于继承 - 初学者的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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