为什么这是不好的做法 [英] Why is this bad practice

查看:56
本文介绍了为什么这是不好的做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在阅读.net框架sdk时,它说下面的内容很糟糕

练习,然后继续给出一个例子这种不良做法的实例非常相同

。有人可以对此发表评论吗,这是好的还是坏的?


// msdn

public int Number

{

get

{

返回数字++; //不要这样做

}

}

然后继续.....


示例2

在此示例中,两个类Cube和Square实现了一个抽象的

类,Shape,并覆盖其抽象的Area属性。注意在属性上使用

覆盖修饰符。程序接受一边

作为输入并计算方形和立方体的面积。它还接受该区域作为输入,并计算方块和立方体的对应边。


// overridding_properties .cs

//覆盖属性

使用系统;

抽象类形状

{

公共抽象双区域

{

get;

set;

}

}


类广场:形状

{

公共双面;


//构造函数:

公共广场(双s)

{

side = s;

}


//区域属性

公共覆盖双区域

{

get

{

返回方*方;

}

设定

{

//给定区域,计算边

边= Math.Sqrt(值);

}

} < br $>
}


类立方体:形状

{

公共双面;


//缺点tructor:

public Cube(双s)

{

side = s;

}


//区域物业

公共覆盖双区域

{

get

{

返回6 *方*方;

}

设定

{

//给定区域,计算边

边= Math.Sqrt(值/ 6);

}

}

}


公共类MainClass

{

public static void Main()

{

//输入一边:

Console.Write("输入边:");

string sideString = Console.ReadLine();

double side = double.Parse(sideString);


//计算区域:

Square s = new Square(side);

Cube c = new Cube(side);


//显示结果:

Console.WriteLine(" square of a square = {0:F2}",s.Area);

Console.WriteLine(" cube of a cube = {0:F2}" ;,c.Area);


//输入区域:

Console.Write("输入区域:");

string areaString = Console .ReadLine();

double area = double.Parse(areaString);


//计算方:

s。面积=面积;

c.Area = area;


//显示结果:

Console.WriteLine(" Side) of square = {0:F2}",s.side);

Console.WriteLine(多维数据集的一边= {0:F2},c.side); < br $>
}

}

输入

4

24

样品输出

输入方:4

方块面积= 16.00

方块面积= 96.00

输入面积:24

平方的边= 4.90

方块的边= 2.00

解决方案

ph*****@vistatec.ie < ph ***** @vistatec .IE>写道:

在阅读.net框架sdk时,它说以下是不好的练习,然后继续给出一个例子与此相同的
实例'糟糕的做法''。有人可以对此发表评论,是好还是坏?

// msdn
public int Number
{
get
{
返回数字++; //不要这样做
}
}




以下示例中究竟发生了什么?注意

这里的坏习惯是获取数字也*改变*它 -

我在你给出的例子中看不到的东西。 />

-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件


> // msdn

public int Number
{
get
{
返回数字++; //不要这样做
}
}


每次读到Number属性时,你都不会得到相同的答案...

这很糟糕:我希望x.Number + x.Number == 2 * x.Number,但它不是

的情况。你不应该修改

属性的get成员中的任何变量......

// Area属性
public override double Area
{
获得
{
返回方*边;
}




那没关系:我会的每次我阅读Area属性时都会得到相同的答案

(提供''side''没有变化)...


Im不确定它是如何在.NET框架中实现的,但++编号是

通常比数字++更受欢迎。


为什么?我已经看到的一些实现使用这个来表示数字++


{

int temp = number;

number = number + 1 ;

返回临时;

}

注意,数字会增加另一个对象被分配。但是在

++数字,


{

number = number + 1;

return数字;

}


无额外步骤或分配。不确定.NET是这样做的,但它是

使用的好习惯,尽你所能。


= J


< ph ***** @ vistatec.ie>在消息中写道

新闻:a3 ************************* @ posting.google.co m ... < blockquote class =post_quotes>

在阅读.net框架sdk时,它说以下是不好的实践,然后继续给出一个相同的例子<这种坏习惯的实例。有人可以对此发表评论,是好还是坏?

// msdn


然后再进一步......
示例2
在此示例中,两个类(Cube和Square)实现了一个抽象的类,Shape,并覆盖其抽象的Area属性。注意在属性上使用
覆盖修饰符。程序接受作为输入的一面
并计算方形和立方体的面积。它还接受该区域作为输入,并计算方形和立方体的相应边。

// overridding_properties.cs
//覆盖属性
使用系统;
抽象类形状
公共抽象双区域
{
获取;
设置;
} >}

班级广场:形状
{
公共双方;

//构造函数:
公共广场(双s)
{
side = s;
}
//区域属性
公共覆盖双区域
{
获取
{
返回方*边;
}
设置
{
//给定区域,计算方
方= Math.Sqrt(价值);
}
}

类立方体:形状
{
公共双面;
//构造函数:
公共立方体(双s)
{
side = s;
}
//区域属性
公共覆盖双区域
{
获取
{
返回6 *边*边;
}
设置
{给定区域,计算边
边= Math.Sqrt(值/ 6);
}
}

公共类MainClass
{
public static void Main()
{
//输入一边:
Console.Write(输入边:);
字符串sideString = Console.ReadLine();
双边=双。 Parse(sideString);

//计算区域:
Square s = new Square(side);
Cube c = new Cube(side);

//显示结果:
Console.WriteLine(" square of a square = {0:F2}",s.Area);
Console.WriteLine(" cube of a cube of = {0:F2}",c.Area);

//输入区域:
Console.Write("输入区域:");
字符串areaString = Console.ReadLine();
double area = double.Parse(areaString) ;

//计算方:
s.Area = area;
c.Area = area;

//显示结果:
Console.WriteLine(" square of a square = {0:F2}",s .side);
Console.WriteLine(" Multi side of side = {0:F2}",c。一边);
}
}
输入
4
24
样品输出
输入侧面:4
面积square = 16.00
方块面积= 96.00
输入面积:24
方块的边= 4.90
方块的边= 2.00



hi,

while reading .net framework sdk, it says that the following is bad
practice, then it goes on to give an example with the very same
instance of this ''bad practice''. Can someone comment on this, is it
good or bad?

//msdn
public int Number
{
get
{
return number++; // Don''t do this
}
}
then further on.....

Example 2
In this example, two classes, Cube and Square, implement an abstract
class, Shape, and override its abstract Area property. Note the use of
the override modifier on the properties. The program accepts the side
as an input and calculates the areas for the square and cube. It also
accepts the area as an input and calculates the corresponding side for
the square and cube.

// overridding_properties.cs
// Overriding properties
using System;
abstract class Shape
{
public abstract double Area
{
get;
set;
}
}

class Square: Shape
{
public double side;

// Constructor:
public Square(double s)
{
side = s;
}

// The Area property
public override double Area
{
get
{
return side*side ;
}
set
{
// Given the area, compute the side
side = Math.Sqrt(value);
}
}
}

class Cube: Shape
{
public double side;

// Constructor:
public Cube(double s)
{
side = s;
}

// The Area property
public override double Area
{
get
{
return 6*side*side;
}
set
{
// Given the area, compute the side
side = Math.Sqrt(value/6);
}
}
}

public class MainClass
{
public static void Main()
{
// Input the side:
Console.Write("Enter the side: ");
string sideString = Console.ReadLine();
double side = double.Parse(sideString);

// Compute areas:
Square s = new Square(side);
Cube c = new Cube(side);

// Display results:
Console.WriteLine("Area of a square = {0:F2}",s.Area);
Console.WriteLine("Area of a cube = {0:F2}", c.Area);

// Input the area:
Console.Write("Enter the area: ");
string areaString = Console.ReadLine();
double area = double.Parse(areaString);

// Compute sides:
s.Area = area;
c.Area = area;

// Display results:
Console.WriteLine("Side of a square = {0:F2}", s.side);
Console.WriteLine("Side of a cube = {0:F2}", c.side);
}
}
Input
4
24
Sample Output
Enter the side: 4
Area of a square = 16.00
Area of a cube = 96.00
Enter the area: 24
Side of a square = 4.90
Side of a cube = 2.00

解决方案

ph*****@vistatec.ie <ph*****@vistatec.ie> wrote:

while reading .net framework sdk, it says that the following is bad
practice, then it goes on to give an example with the very same
instance of this ''bad practice''. Can someone comment on this, is it
good or bad?

//msdn
public int Number
{
get
{
return number++; // Don''t do this
}
}



Where exactly does that occur in the example that follows? Note that
the bad practice here is that fetching the Number also *changes* it -
something that I can''t see happening in the example you gave.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


> //msdn

public int Number
{
get
{
return number++; // Don''t do this
}
}
Here you wont get the same answer every time you read the Number property...
It''s bad : I expect that x.Number + x.Number == 2*x.Number, but it''s not the
case. You should not modify any variable within the get member of a
property...
// The Area property
public override double Area
{
get
{
return side*side ;
}



That''s ok : I''ll get the same answer every time I read the Area property
(provided ''side'' doesnt change)...


I"m not sure how it''s implemented in the .NET framework, but ++number is
usually preferred to number++.

Why? Some implementations I"ve seen use this to mean number++

{
int temp = number;
number = number + 1;
return temp;
}
Note, the number is incremented an another object is allocated. But in
++number,

{
number = number + 1;
return number;
}

No extra step or allocation. Not sure of .NET does it this way, but it''s
good practice to use, where you can.

=J

<ph*****@vistatec.ie> wrote in message
news:a3*************************@posting.google.co m...

hi,

while reading .net framework sdk, it says that the following is bad
practice, then it goes on to give an example with the very same
instance of this ''bad practice''. Can someone comment on this, is it
good or bad?

//msdn

then further on.....

Example 2
In this example, two classes, Cube and Square, implement an abstract
class, Shape, and override its abstract Area property. Note the use of
the override modifier on the properties. The program accepts the side
as an input and calculates the areas for the square and cube. It also
accepts the area as an input and calculates the corresponding side for
the square and cube.

// overridding_properties.cs
// Overriding properties
using System;
abstract class Shape
{
public abstract double Area
{
get;
set;
}
}

class Square: Shape
{
public double side;

// Constructor:
public Square(double s)
{
side = s;
}

// The Area property
public override double Area
{
get
{
return side*side ;
}
set
{
// Given the area, compute the side
side = Math.Sqrt(value);
}
}
}

class Cube: Shape
{
public double side;

// Constructor:
public Cube(double s)
{
side = s;
}

// The Area property
public override double Area
{
get
{
return 6*side*side;
}
set
{
// Given the area, compute the side
side = Math.Sqrt(value/6);
}
}
}

public class MainClass
{
public static void Main()
{
// Input the side:
Console.Write("Enter the side: ");
string sideString = Console.ReadLine();
double side = double.Parse(sideString);

// Compute areas:
Square s = new Square(side);
Cube c = new Cube(side);

// Display results:
Console.WriteLine("Area of a square = {0:F2}",s.Area);
Console.WriteLine("Area of a cube = {0:F2}", c.Area);

// Input the area:
Console.Write("Enter the area: ");
string areaString = Console.ReadLine();
double area = double.Parse(areaString);

// Compute sides:
s.Area = area;
c.Area = area;

// Display results:
Console.WriteLine("Side of a square = {0:F2}", s.side);
Console.WriteLine("Side of a cube = {0:F2}", c.side);
}
}
Input
4
24
Sample Output
Enter the side: 4
Area of a square = 16.00
Area of a cube = 96.00
Enter the area: 24
Side of a square = 4.90
Side of a cube = 2.00



这篇关于为什么这是不好的做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆