У меня возникли проблемы с хранением атрибутов из моего абстрактного класса, конструктор, похоже, работает нормально. Однако я не могу хранить базовые атрибуты в таблице базы данных подкласса.
public abstract class Vehicle : IComparable< Vehicle >, IComparable {
public Int16 VehicleID;
public DateTime ProductionDate;
public Vehicle(Int16 _ Vehicle ID,DateTime _ProductionDate)
{
this.AccidentID = _ AccidentID;
this.ProductionDate = _ProductionDate;
}
int IComparable.CompareTo(object other) {
return CompareTo((Vehicle)other);
}
public int CompareTo(Vehicle other){
return this.ProductionDate.CompareTo(other.ProductionDate);
}
public Vehicle()
{}
}
public class Car : Vehicle
{
public Car ()
{
}
public Car (Int16 _VehicleID,DateTime _ProductionDate, Int16 _CarAttribute1, Int16 _CarAttribute2):base(_Vehicle ID,_ProductionDate)
{
this.AccidentID = _ AccidentID;
this.ProductionDate = _ProductionDate;
this.CarAttribute1 = _CarAttribute1
this.CarAttribute2 = _CarAttribute2
}
[PrimaryKey, AutoIncrement, Column("Attribute1")]
public Int16 CarAttribute1{ get; set;}
[Column("Attribute2")]
public Int16 CarAttribute2{ get; set;}
}
Я совершенно новичок в С#, поэтому некоторые рекомендации оценены :) Что я пропустил?
В базовом классе вы должны использовать свойства вместо полей, поэтому настройте базовый класс следующим образом:
public abstract class Vehicle : IComparable<Vehicle>, IComparable {
public Int16 AccidentID { get; set; }
public DateTime ProductionDate { get; set;}
public Vehicle(Int16 _ Vehicle ID,DateTime _ProductionDate)
{
this.AccidentID = _ AccidentID;
this.ProductionDate = _ProductionDate;
}
int IComparable.CompareTo(object other) {
return CompareTo((Vehicle)other);
}
public int CompareTo(Vehicle other){
return this.ProductionDate.CompareTo(other.ProductionDate);
}
public Vehicle()
{}
}
поэтому я изменил:
public Int16 VehicleID;
public DateTime ProductionDate;
чтобы:
public Int16 AccidentID { get; set; }
public DateTime ProductionDate { get; set;}
BTW: У вас есть поле VehicleID в базовом классе, но в конструкторе вы устанавливаете значение для AccidentID вместо VehicleID. Я думаю, что это просто опечатка в описании, не так ли? Поэтому я использовал свойство AccidentID как имя свойства, поэтому, пожалуйста, проверьте, правильно ли оно.