如何显示继承的成员 [英] how to show inherited member

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

问题描述

namespace sir_shahzad_paper_OOP_2013
{
    class baseClass
    {
        protected int i = 13;
    }
    class Program : baseClass
    {
         int i=9;
         int b = 9;
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.Write(p.i);
 
            
        }
    }
}





查看以上程序

现在控制台显示输出9.我也想显示13。我怎么能这样做。



look above program
now console showing me the output 9. i want to show 13 as well. how can i do this.

推荐答案

Console.WriteLine(i);
Console.WriteLine(base.i);

结果:



9

13



编辑:回应Karthik和Muhamad的反馈:



是的,我不是在这里目标,不考虑这个方法是静态的,因为它是一个特殊的'控制台应用程序的主要方法启动板:你可以从不在静态方法中使用关键字base来访问祖先类字段或方法,无论该静态方法是 in 动态(非静态)类还是静态类!



当然,使用静态类,你根本没有继承的其他类:所有静态类都继承自'Object。



静态类中有一种方法可以调用动态类的方法,在该方法中,您可以访问它继承的基类。让我在代码中说明不是控制台应用程序,而是在WinForms中完成:

Result:

9
13

in response to feedback from Karthik and Muhamad:

Yes, I'm not on-target here, not thinking about the fact the method is static because it's the special 'Main method launch-pad for a Console App: you can never use the keyword 'base to access an ancestor class' fields, or methods, in a static method whether that static method is within a dynamic (non-static) class, or a static class !

And, of course, using static Classes you do not have inheritance from other classes at all: all static classes inherit from 'Object.

There is a way in a static class to call a method of a dynamic class, and in that method, you can access the base class it inherits from. Let me illustrate in code that's not a Console app, but done in WinForms:

public class baseClass
{
    protected int i = 13;
}

public class inheritsFromBaseClass : baseClass
{
    private int i = 9;
    private int b = 9;

    public void showValues()
    {
        Console.WriteLine(i.ToString());
        Console.WriteLine(base.i.ToString());
    }
}

// here's the static class
public static class xProgram
{
    public static inheritsFromBaseClass childOfBase;

    // static class requires parameterless ctor
    static xProgram()
    {
        childOfBase = new inheritsFromBaseClass();
    }
}

如果你调用静态类'xProgram,就像这样:

If you invoke the static class 'xProgram, like this:

xProgram.childOfBase.showValues();

您创建动态类的静态实例,'inheritsFromBase命名为'childOfBase',当您调用该实例的'showValues方法时,该方法可以并确实使用'base'来访问其祖先类'价值'i。



摘要:



1.你在特殊区域时编写控制台应用程序,如果你尝试使用继承里面静态'主要方法。



2.有办法访问动态类'ancestor类来自静态方法(如此处的代码所示)。但是,想想发生了什么:使用动态类的令人信服的理由是创建多个实例的工具。



在这种情况下,将会有一个且只有一个'inheritsFromBaseClass实例封装在xProgram静态类中!我怀疑不是是一件好事。



建议:考虑使用 only 动态类在OOP中对您的实验进行原型设计。



对不起,如果我的回复在这里混淆了水域!我几乎没有使用控制台应用程序的经验。

You create a static instance of the dynamic class, 'inheritsFromBase named 'childOfBase, and when you invoke that instance's 'showValues method, that method can, and does, use the word 'base to access its ancestor class' value for 'i.

Summary:

1. you are in a special 'zone when writing a Console Application if you try and use inheritance inside the static 'Main method.

2. there are ways to get access to a dynamic class' ancestor class from a static method (as shown in the code here). But, think about what's happened: the compelling reason to use dynamic classes is the facility to create multiple instances of them.

In this case there will be one, and only one, instance of the 'inheritsFromBaseClass encapsulated within the xProgram static class ! I suspect that's not a good thing.

Suggestion: consider prototyping your experiments in OOP using only dynamic classes.

Sorry if my response has "muddied the waters" here ! I have virtually no experience with Console applications.


试试这个: -

try this:-
namespace ConsoleApplication1
{
    class baseClass
    {
        protected int i = 13;
    }
    class prog : baseClass
    {
        int i = 9;
        int b = 9;
        public void printMethod()
        {
            Console.WriteLine(i); //this will give 9
            Console.WriteLine(base.i); //this will give 13
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            prog p = new prog();
            p.printMethod();
        }
    }
}





你试图添加

Console.WriteLine(base.I出现);

这行成为静态主方法。

--------------------------- ---------

您看到错误详细信息base关键字在静态方法中是不可用的。



为什么我们可以静态方法中不使用base关键字? : -

因为base关键字指的是当前类实例的基类。但是你没有静态方法中的当前实例 - 它们是静态的而不是实例。



you trying to add
Console.WriteLine(base.i);
this line into static main method.
------------------------------------
you seen the error detail base keyword is not avialable in static method.

why we can not use base keyword in static method? :-
Because base keyword is referring to the base class of the current class instance. But you do not have a current instance in static methods - they are static not instance.


你好b $ b试试这样





Hi Try like this


using System;
namespace sir_shahzad_paper_OOP_2013
{

    class Program
    {

        int i = 9;

        static void Main(string[] args)
        {
            DerivedClass obj = new DerivedClass();
            obj.somemethod();
        }


    }

    public class BaseClass
    {
        protected int i = 13;
    }
    public class DerivedClass : BaseClass
    {
        int i = 9;
        int b = 9;

      public  void somemethod()
        {
            DerivedClass p = new DerivedClass();
            Console.WriteLine(i); //9
            Console.WriteLine(base.i); // 13
            Console.ReadLine();
        }

    }



}









你不能在静态中使用 base 关键字方法,所以我创建了一个新的并完成了它。

i希望你明白。





you cannot use the base keyword inside a static method, so i have created a new class and done it.
i hope you understood.


这篇关于如何显示继承的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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