我有一个名为Chicken的类,其中包含一些方法,在另一个类中,我实例化了Chicken并调用了其中的方法,可能会像这样:
Chicken chicken = new Chicken("Name","Description")
public void UpdateChicken(Chicken chicken)
{
chicken.Update(chicken);
}
上述代码是否正确,如果存在问题,是否更好创建另一个类,例如ChickenCalculations,并做出如下处理:
public void UpdateChick(Chicken chicken)
{
ChickenCalculations.Update(chicken);
}
这里是一个实现:
Chicken chicken = new Chicken("Bob","Coolest Chicken", 4, 123, 5, 388, true, false, true);
Chicken anotherChicken = new Chicken()
anotherChicken.Update(chicken);
chicken.Update(chicken)
这里有一个更实际的例子,而不是使用一只鸡:
public class AirlineBooking
{
int BookingId {get;set;}
string Name {get;set;}
string Description {get;set;}
decimal Price {get;set;}
decimal Tax {get;set;}
string seat {get;set;}
bool IsActive {get;set;}
bool IsCanceld {get;set;}
public AirlineBooking(string name, string description, decimal price,
decimal tax, string seat, bool isActive, bool isCanceled)
{
Name = name;
Description = description;
Price = price;
Tax = tax;
Seat = seat;
IsActive = isActive;
IsCanceled = isCanceled;
}
public Update(AirlineBooking airlineBooking, int id)
{
//Call stored proc here to update booking by id
}
public class BookingSystem
{
//Create new booking
AirlineBooking booking = new AirlineBooking("ticket-1",
"desc",150.2,22.0,
"22A",true, false);
//Change properties and update.
booking.Name ="ticket-2";
booking.Description = "desc2";
booking.Price = 200.52;
booking.Tax = 38.50;
public void UpdateBooking(AirlineBooking booking, int id)
{
/* This is the meat of the question, should the passed in booking to
update itself or should I have a Service Class , such as
AirlineBookingOperations with an update method. */
booking.Update(booking,id);
}
}
}
chicken.Update()
不足以吗? - Alessandrochicken.Update()
有什么问题?顺便提一下,你可能想提供一个更有意义的例子。当你处于一个毫无意义的领域时,很难讨论设计决策。 - R. Martinho Fernandes