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

Creating a generics interface class

Next, go to Solution Explorer. Right-click on the name of the website, select Add, and then click on Class. Name the class GenInterface and then click on OK. When the Visual Studio message comes up, click on Yes. Remember, this is just an example.

The code for the GenInterface class is really complex. I'll create it now line-by-line, explaining what I'm doing and why I'm doing it.

First, delete everything except using System; at the very top. Next, you'll make the class called Quad for a four-sided shape of some kind. Enter the following after using System:

public class Quad : IComparable<Quad>

This needs System so that we can use IComparable. If you right-click on it and select Go To Definition in the drop-down menu (F12), you can see the definition of this thing. You will see namespace System near the top, and the public intCompareTo (T other); function after the Returns definition, as shown in Figure 3.3.2:

Figure 3.3.2: The definition of IComparable

Notice that it returns an integer. So, when we implement this interface, we have to keep that in mind. Now, close the definition window.

In our particular case, enter the following text below the open curly brace under the line beginning with public class Quad...:

private string name;
public Quad(string na)

Now, to set the value, enter the following between a set of curly braces beneath the preceding lines:

name = na;

After all, every quadrilateral shape, which is a square, rectangle, or rhombus, all have a name, don't they? So, it's a good idea to centralize the name feature in the Quad class.

主站蜘蛛池模板: 甘洛县| 纳雍县| 昭觉县| 延川县| 仙居县| 威信县| 中方县| 吴桥县| 桃源县| 永仁县| 新建县| 甘洛县| 申扎县| 垣曲县| 绥棱县| 松溪县| 韶山市| 凤凰县| 华坪县| 宣汉县| 于田县| 淄博市| 涟源市| 遂溪县| 安福县| 广德县| 稷山县| 白玉县| 平顶山市| 沅陵县| 凉城县| 泽库县| 甘洛县| 博白县| 浦东新区| 涿州市| 乡宁县| 马山县| 盐山县| 永州市| 泽州县|