assign value from parent class. easy
How do something like that:
Class Car: string Name, int year
Class SUV:Car;
SUV bmw=ToCar(car);
Something like that ??
开发者_如何学JAVApublic 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
{
}
精彩评论