开发者

Cant reference from any objects?

I was brushing up myself in Inheritance and overriding but I came across this stu开发者_Go百科pid problem. I am creating 3 classes and making the objects of first two class in the third one. But my problem is that I cannot access the Function inside them :( eg. A() in Testing_Class and same function in Testing Class.

What I am doing wrong here?

public class Testing_Class
{
    public virtual string A()
    {
        string a = "John";
        return a;
    }
}

public class Testing : Testing_Class
{
    public override string  A()
    {
        string a = "John";
        return a;
    }

    Testing_Class t1 = new Testing_Class();
}

public class Test
{
    Testing MyTesting = new Testing();
    Testing_Class MyTestingClass = new Testing_Class();
    MyTesting.A(); //MyTesting is not even showing up in the popup options menu...  
}


It's because you're trying to call A() outside of a method.

public class Test
{
    public void Foo()
    {
        Testing MyTesting = new Testing();
        Testing_Class MyTestingClass = new Testing_Class();
        MyTesting.A(); 
    }
}

You can declare things at the class-level, but to simply execute an expression it needs to be in a method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜