在派生类中访问基类变量

发布于 2024-10-21 23:01:35 字数 800 浏览 5 评论 0原文

class Program
{
    static void Main(string[] args)
    {
        baseClass obj = new baseClass();
        obj.intF = 5;
        obj.intS = 4;
        child obj1 = new child();
        Console.WriteLine(Convert.ToString(obj.addNo()));
        Console.WriteLine(Convert.ToString(obj1.add()));
        Console.ReadLine();
    }
}
public class baseClass
{
    public int intF = 0, intS = 0;
    public int addNo()
    {
        int intReturn = 0;
        intReturn = intF + intS;
        return intReturn;
    }
}
class child : baseClass
{
    public int add()
    {
        int intReturn = 0;
        intReturn = base.intF * base.intS;
        return intReturn;
    }
}

无论我输入什么,我都想访问子类中的 intF 和 intS 。但我总是得到两个变量的值 0。0 是两个变量的默认值。

谁能告诉我怎样才能得到这个值???

非常感谢..

class Program
{
    static void Main(string[] args)
    {
        baseClass obj = new baseClass();
        obj.intF = 5;
        obj.intS = 4;
        child obj1 = new child();
        Console.WriteLine(Convert.ToString(obj.addNo()));
        Console.WriteLine(Convert.ToString(obj1.add()));
        Console.ReadLine();
    }
}
public class baseClass
{
    public int intF = 0, intS = 0;
    public int addNo()
    {
        int intReturn = 0;
        intReturn = intF + intS;
        return intReturn;
    }
}
class child : baseClass
{
    public int add()
    {
        int intReturn = 0;
        intReturn = base.intF * base.intS;
        return intReturn;
    }
}

I want to access intF and intS in child class whatever i input.. but i always get the values of both variables 0. 0 is default value of both variables.

Can anyone tell me that how can i get the value???

thx in adv..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原野 2024-10-28 23:01:35

是的,零是您应该得到的,因为您创建了子类的单个实例并且没有为其继承的变量分配任何值,

child obj1 = new child();

而是您单独实例化了基类的另一个实例并为其成员分配了值,

baseClass obj = new baseClass();

两者都运行时都是基类类实例和子实例是完全不同的对象,因此您应该单独分配子值,

obj1.intF = 5;
obj1.intS = 4;

这样您才能获得所需的结果。

Yes, Zero is what you should get, since you made single instance of child class and did not assign any value to its inherited variables,

child obj1 = new child();

rather you have instantiated another instance of base class separately and assign value to its members,

baseClass obj = new baseClass();

both runtime both the base class instance and child instance are totally different objects, so you should assign the child values separately like

obj1.intF = 5;
obj1.intS = 4;

then only you shall get the desired result.

撩心不撩汉 2024-10-28 23:01:35

你有两个独立的对象 - 这就是问题所在。 obj1 引用的对象中的基类变量的值为 0,因为它们尚未设置为其他值。没有任何东西可以将该对象与 obj 引用的对象联系起来。

如果您需要访问另一个对象的变量,则必须使该对象可供尝试访问数据的人使用。您可以将 obj 作为参数传递给方法,或者将其作为属性。这里有很多不同的方法,但我们不知道更大的图景是什么——你真正想要做什么。一旦您了解了为什么它没有按您期望的方式工作,您就可以开始思考您真正想要做什么。

需要明确的是,这与继承无关。它与您创建两个不同的对象有关一切。如果您只使用一个类,但创建该类的两个不同实例,您将获得相同的效果。

You've got two separate objects - that's the problem. The values of the base class variables in the object referred to by obj1 are 0, because they haven't been set to anything else. There's nothing to tie that object to the object referred to by obj.

If you need to access the variables of another object, you'd have to make that object available to the one trying to access the data. You could pass obj as an argument to a method, or perhaps make it a property. There are lots of different approaches here, but we don't know what the bigger picture is - what you're really trying to do. Once you understand why it's not working as you expect it to, you can start thinking about what you're really trying to do.

Just to be clear, this has nothing to do with inheritance. It has everything to do with you creating two distinct objects. You'd get the same effect if you just used a single class, but created two different instances of that class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文