6
Answers

Override method

albert albert

albert albert

13y
1.2k
1
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

Answers (6)