官术网_书友最值得收藏!

Adding refinements

Now we add class-specific refinements; for this, enter the following between a set of curly braces beneath the preceding line:

private double sideLength;
public Square(string n, double s) : base(n) {sideLength = s;}

Here, string n is the name and doubles is the side. Then, call the base class constructor with the name (n) and then enter sideLength = s. Remember, when you are calling the base class constructor, you're reusing code.

I've chosen to express this as a single line just to save space. Remember that, normally, it would look like this:

private double sideLength;
public Square(string n, double s) : base(n) { sideLength = s; }

Next, we have to implement an override version of Perimeter. So, enter the following beneath the preceding closed curly brace:

public override string Perimeter()

Now, we want to keep return base.Perimeter(), which appears automatically, because it provides the useful default functionality, $"The perimeter of {name} is ";, from the preceding return line: we don't want to keep typing that. What we do want to do is add a small refinement. So, add the following refinement to return base.Perimeter():

return base.Perimeter() + 4 * sideLength;

This means four times the sideLength variable, because to find the perimeter of a square, you take four multiplied by the length of one side.

Refinement comes from the derived class generic functionality, which is equally applicable to all the classes you stick into a virtual method in the base class: you don't have to keep writing it.

Next, we can repeat this for our rectangle. So, copy the public class Square : Quad block (Ctrl + C) and then paste (Ctrl +& V) down below:

public class Rectangle : Quad
{ private double sideOne, sideTwo; public Rectangle(string n, double s1, double s2) : base(n) { sideOne = s1; sideTwo = s2; } public override string Perimeter() { return base.Perimeter() + (2 * sideOne + 2 * sideTwo); }
}

Now, make the following changes:

  1. Rename this block to Rectangle. This is also derived from Quad, so that's fine.
  2. Change where it says sideLength; because a rectangle has two different side lengths, so change that to say sideOne and sideTwo.
  3. Change public Square to public Rectangle as the name of the constructor. It calls the base class constructor with the name.
  4. Then, initialize the other two, so now you'll say double s1 and double s2.
  1. Next, you have to initialize the fields, so say sideOne = s1; and sideTwo = s2;. That's it: they've been initialized.
  2. Now again, override Perimeter inside the Rectangle class as shown earlier. Here, you specify the bit that applies to rectangles, so (2 * sideOne + 2 * sideTwo). Make sure that you enclose this within parentheses, so that the calculation is done first and then it's combined together with base.Perimeter with the rest of it.

So, that's that class. For reference, the complete version of the GenInterface class, including comments, is shown in the following code block:

using System;
public class Quad:IComparable<Quad>//implement IComparable
{
private string name;//instance field
public Quad(string na)
{
name = na;//set value of instance field
}
//implement CompareTo to make list sortable
//in this case, the items are sorted by name
public int CompareTo(Quad other)
{
if(this.name.CompareTo(other.name) < 0)
{
return -1;
}
else
{
return 1;
}
}//put default code inside Perimeter
public virtual string Perimeter()
{
return $"The perimeter of {name} is ";
}
}
public class Square: Quad
{
private double sideLength;
public Square(string n, double s):base(n)
{
sideLength = s;
}
//override Perimeter, calling the base portion
//and then adding refinement with 4*sideLength
public override string Perimeter()
{
return base.Perimeter() + 4 * sideLength;
}
}
public class Rectangle: Quad
{
private double sideOne, sideTwo;
public Rectangle(string n, double s1, double s2) : base(n)
{
sideOne = s1; sideTwo = s2;
}
//override Perimeter, calling the base portion
//and then adding refinement with 2sideOne+2sideTwo
public override string Perimeter()
{
return base.Perimeter() + (2 * sideOne + 2 * sideTwo);
}
}
主站蜘蛛池模板: 即墨市| 时尚| 平舆县| 浙江省| 阿坝县| 慈利县| 科技| 平顺县| 黑龙江省| 兴宁市| 马尔康县| 若羌县| 呼图壁县| 密云县| 枣强县| 武功县| 绥阳县| 台前县| 阳城县| 海口市| 大姚县| 静宁县| 成都市| 三门峡市| 寿宁县| 东光县| 拉萨市| 寻乌县| 甘谷县| 明光市| 托里县| 江陵县| 朔州市| 西峡县| 平和县| 华池县| 常德市| 温宿县| 永丰县| 炉霍县| 格尔木市|