HI Everyone,
I have this:
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PackageInheritanceApp
{
public abstract class Package
{
private string firstName;
private string lastName;
private string zipCode;
private string address;
private string city;
private string state;
private double weight = 0;
private double cost = 0;
public Package(string FirstNameValue, string LastNameValue, string ZipCodeValue,string AddressValue,
string CityValue, string StateValue, double WeightValue, double CostValue )
{
this.firstName = FirstNameValue;
this.lastName = LastNameValue;
this.zipCode = ZipCodeValue;
this.address = AddressValue;
this.city = CityValue;
this.state = StateValue;
this.weight = WeightValue;
this.cost = CostValue;
}//end constructor
public virtual double CalculateCost()
{
double z;
z = weight * cost;
return z;
}
public abstract double CalculateCost2();
//{
//double z;
//z = weight * costPerOnce;
//return z;
// }
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}//end method
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}//end method
public string ZipCode
{
get
{
return zipCode;
}
set
{
zipCode = value;
}
}//end method
public string Address
{
get
{
return address;
}
set
{
address = value;
}
}//end method
public string City
{
get
{
return city;
}
set
{
city = value;
}
}//end method
public string State
{
get
{
return state;
}
set
{
state = value;
}
}//end method
public double Weight
{
get
{
return weight;
}
set
{
weight = value < 0 ? 0 : value;//Validation.
}
}//end method
public double Cost
{
get
{
return cost;
}
set
{
cost = value > 0 ? 0 : value;//Validation.
}
}
}
}
[/code]
AND:
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PackageInheritanceApp
{
public class TwoDayNightPackage:Package
{
private double flatFree;
public TwoDayNightPackage(string first, string last, string zi, string ad,
string ci, string st, double wei, double co, double flatFreeValue) : base(first,last,
zi,ad,ci,st,wei,co)
{
this.flatFree = flatFreeValue;
}
public double CalculateCostTwoDayNightPackage
{
get
{
return flatFree;
}
set
{
CalculateCost2() + flatFree = value;
}
}
public override double CalculateCost()
{
double z;
z = (weight * costPerOnce) + flatFree;
return z;
//throw new NotImplementedException();
}
public double FlatFree
{
get
{
return flatFree;
}
set
{
flatFree = value;
}
}//end method
}
}
[/code]
I just have a method in package: CalculateCost() that has to be: weight * costperOnce(For every concrete derived class is this).
And then in every concrete class the method CalculateCost of class Package has to override with some selfdependent variables. like double flatFree.
So that will be: CalculateCost+ flatfree of CalculateCost + additionCost. But I cant make the method: CalculateCost() in package abstract and I cant make an INterface for it. Because either doesnt allowed a body.
So how to do?
THX
Loading
VulpesPosted Nov 8, 2011, 10:01 AM
Just make the CalculateCost() method virtual as said before so it can contain a body - it's OK for an abstract class to contain a virtual method.
I'd also make Package's constructor protected to make it clear it can't be instantiated.
Then all you need to do is to define the CalculateCost() override in the derived class as follows:
}
albert albertPosted Nov 8, 2011, 10:05 AM
albert albertPosted Nov 8, 2011, 9:14 AM
[code]
public double CalculateCostNight(double weight, double costPerOnce,double additionalCost)
{
double z;
z = CalculateCost(weight, costPerOnce) + (additionalCost * weight);
return z;
}
[/code]
and:
[code]
public double calcShippingCost(double weight, double costPerOnce,double flatFree)
{
double z;
z = CalculateCost(weight, costPerOnce) + flatFree;
return z;
//throw new NotImplementedException();
}
[/code]
VulpesPosted Nov 8, 2011, 9:12 AM
albert albertPosted Nov 8, 2011, 9:08 AM
[code]
public double CalculateCostTwoDayNightPackage
{
get
{
return flatFree;
}
set
{
CalculateCost2() + flatFree = value;
}
}
[/code]
The class Package has to be abstract, because that is the general class and then class TwodayPackage is a concrete class with more detailed methods.
But I dont instantiate from Package. Ofcourse that is not possible. The system is the same as: Employee(abstract) en then u have an hourlyEmployee(Concreet)... etc
VulpesPosted Nov 8, 2011, 8:59 AM