开发者

Instantiation of class

I am newbie in C#.I have one confusion.

There are two classes A and B.

using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            A objA = new A();

            A objB = new B();
        }
    }

    class A
    {
        public void MethodA()
        {
            Console.WriteLine("method of A class");
        }
    }

    class B : A
    {
        public void MethodB()
        {
            Console.WriteLine("method of B class");
        }
    }
}
开发者_如何学编程

Now the confusion which I have is that what is the meaning of:

A objB = new B();

I have seen and instantiated class like thi:

A objB = new A();

Can anyone tell me why we used:

A objB = new B();

Thanks in advance.


Since class B inherits from A, instances of B are also instances of A, and polymorphism guarantees than any instance of B can be assigned to an A.

Your code's only purpose seems to be demonstrating this concept.


What you're doing here is creating a new B, but you're treating it as an A. After this, you can do e.g. this:

A objB = new B();
B realObjB = (B)objB;

You don't use this in code directly, but you can use this for methods that only accept an A, but where you really want it to be a B.

A concrete example:

class Program
{
    static void Main(string[] args)
    {
        Truck truck = new Truck();

        TellMeHowManyWheelsThisHas(truck);
    }

    private static void TellMeHowManyWheelsThisHas(Vehicle vehicle)
    {
        Console.WriteLine("This vehicle has {0} wheels", vehicle.HowManyWheelsDoIHave());
    }
}

abstract class Vehicle
{
    public abstract int HowManyWheelsDoIHave();
}

class Car : Vehicle
{
    public override int HowManyWheelsDoIHave()
    {
        return 4;
    }
}

class Truck : Vehicle
{
    public override int HowManyWheelsDoIHave()
    {
        return 8;
    }
}


A derived class object can be represented a its base class object.

i.e in your case class B is derived from class A. hense A objB (which is of Base class type) can represent an object of type B. But in this case objB can only see base class A's Member functions and properties.

google for Upcasting/DownCasting for more information


Normally this would be used in the case where you only care that objB is an A and the fact that it is really a B doesn't matter.

An example is better served by a more descriptive example of polymorphism.

something akin to this: (method bodies missing for brevity)

class Vehicle { public void Drive(); }
class Boat : Vehicle { public void LowerAnchor(); }
class Car : Vehicle { public void SoundHorn(); }

Vehicle boat = new Boat();
boat.Drive();
Vehicle car = new Car();
car.Drive();

The fact that boat and car are actually Boat and Car is irrelevant, all you want to do is drive. The implementation of Drive() may also differ, but again you're only interested in being able to drive.


using System;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            A objA = new A();
            A objB = new B();
            Console.ReadLine(); // only used to keep the output on-screen
        }
    }

    class A
    {
        public A()
        {
            Console.WriteLine("method of A class");
        }
    }

    class B : A
    {
        public B()
        {
            Console.WriteLine("method of B class");
        }
    }
}

I believe this code example does a better job of providing an output that reflects what is happening. I moved the methods within each class to the constructor to illustrate what really happens when you inherit from an object. This is the output:

method of A class
method of A class
method of B class

The original code example was probably designed to show a simple example of inheritance but was poorly written.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜