简体   繁体   English

访问派生类中的基类变量

[英]access base class variable in derived class

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. 无论我输入什么,我想在子类中访问intF和intS ..但我总是得到两个变量的值0. 0是两个变量的默认值。

Can anyone tell me that how can i get the value??? 任何人都可以告诉我,我怎么能得到价值???

thx in adv.. thx in adv ..

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, 是的,Zero是您应该得到的,因为您创建了子类的单个实例并且没有为其继承的变量赋值,

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. 那么只有你才能得到理想的结果。

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. 通过中提到的对象的基类变量的值obj1 0,因为他们没有被设置为别的。 There's nothing to tie that object to the object referred to by obj . 将该对象与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. 您可以将obj作为参数传递给方法,或者将其作为属性。 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. 如果您只使用一个类,但是创建了该类的两个不同实例,则会获得相同的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM