开发者

assign value from parent class. easy

开发者 https://www.devze.com 2022-12-22 10:17 出处:网络
How do something like that: Class Car: string Name, int year Class SUV:Car; SUV bmw=ToCar(car); Something like that ??

How do something like that:

Class Car: string Name, int year
Class SUV:Car;

SUV bmw=ToCar(car);

Something like that ??

开发者_如何学JAVA
public object ToObject(Type type, Car car)
{
// here i just wanna take string and int pool
}

I did something like:

List<Car> cars=new List<Car>

SUV suv=new SUV();

cars.Add(suv).

And next I want:

SUV suv=(Suv)cars[0];

But I get an error that this casting is wrong. :/

I want to just recover the SUV.


It looks like you've got the basic idea, there were some naming errors in your example that I'm assuming were just typos and not what the actual code looks like...something like this works fine which is what it looks like you are getting at:

static void Main(string[] args)
        {
            List<Car> cars = new List<Car>();
            SUV suv = new SUV { name = "mySuv", year = 2010 };
            cars.Add(suv);

            SUV anotherSuvReference = (SUV)cars[0];
            Console.WriteLine(anotherSuvReference.name);
        }

        class Car
        {
            public string name;
            public int year;
        }
        class SUV : Car
        {
        }
0

精彩评论

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

关注公众号