- Beginning C# 7 Hands-On:Advanced Language Features
- Tom Owsiak
- 669字
- 2021-07-02 15:29:19
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:
- Rename this block to Rectangle. This is also derived from Quad, so that's fine.
- Change where it says sideLength; because a rectangle has two different side lengths, so change that to say sideOne and sideTwo.
- Change public Square to public Rectangle as the name of the constructor. It calls the base class constructor with the name.
- Then, initialize the other two, so now you'll say double s1 and double s2.
- Next, you have to initialize the fields, so say sideOne = s1; and sideTwo = s2;. That's it: they've been initialized.
- 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);
}
}
- 國際大學生程序設計競賽中山大學內部選拔真題解(二)
- 測試驅動開發:入門、實戰與進階
- Getting Started with ResearchKit
- Python for Secret Agents:Volume II
- Visual Basic編程:從基礎到實踐(第2版)
- Learning AWS Lumberyard Game Development
- 3D少兒游戲編程(原書第2版)
- 程序員修煉之道:通向務實的最高境界(第2版)
- 信息技術應用基礎
- Android項目實戰:手機安全衛士開發案例解析
- PrimeFaces Blueprints
- Windows Phone 8 Game Development
- Microsoft HoloLens By Example
- 深入淺出Rust
- 跟著迪哥學Python數據分析與機器學習實戰