C# 初始化基类:两次声明变量?
我目前正在读一本C#教程。现在我遇到了这样一个问题:C# 初始化基类:两次声明变量?,c#,class,inheritance,declare,C#,Class,Inheritance,Declare,我目前正在读一本C#教程。现在我遇到了这样一个问题: using System; namespace RectangleApplication { class Rectangle { //member variables protected double length; protected double width; public Rectangle(double l, double w) { length = l;
using System;
namespace RectangleApplication {
class Rectangle {
//member variables
protected double length;
protected double width;
public Rectangle(double l, double w) {
length = l;
width = w;
}
public double GetArea() {
return length * width;
}
public void Display() {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}//end class Rectangle
class Tabletop : Rectangle {
private double cost;
public Tabletop(double l, double w) : base(l, w) { }
public double GetCost() {
double cost;
cost = GetArea() * 70;
return cost;
}
public void Display() {
base.Display();
Console.WriteLine("Cost: {0}", GetCost());
}
}
class ExecuteRectangle {
static void Main(string[] args) {
Tabletop t = new Tabletop(4.5, 7.5);
t.Display();
Console.ReadLine();
}
}
}
在类桌面中
有成本
声明两次。曾经是私人双重成本代码>和4行之后的双倍成本代码>
为什么会这样
删除双倍成本时代码>代码仍然有效。当代码中有double cost
时,我可以将鼠标悬停在private double cost上代码>并阅读消息:字段Tabletop.cost
从未使用过”。我几乎可以删除任何一个成本,代码工作正常
他们是否忘记删除其中一项声明,或者背后有什么原因
还有,为什么我没有收到一条错误消息,比如“成本已经定义”
这是事实上,在类桌面
中,作用域成本
是重叠的,因为在方法GetCost
中还有一个名为cost
的局部变量
在GetCost
的范围内,当您引用cost
时,实际上是引用名为cost
的局部作用域对象,而不是外部作用域中的对象(类中的对象)。发生这种情况时,外部作用域中声明的cost
被内部作用域隐藏(在方法中).事实上,在您的类Tabletop
中,作用域cost
是重叠的,因为在方法GetCost
中还有一个名为cost
的局部变量
在GetCost
的范围内,当您引用cost
时,实际上是引用名为cost
的局部作用域对象,而不是外部作用域中的对象(类中的对象)。发生这种情况时,外部作用域中声明的cost
被内部作用域隐藏(在方法中).在成员范围(在方法中)中定义与现有成员同名的变量时,只需隐藏后者并引用前者
在你的例子中:
class Tabletop : Rectangle
{
private double cost;
public Tabletop(double l, double w) : base(l, w) { }
public double GetCost()
{
double cost; // this hides the field
cost = GetArea() * 70;
return cost; // this referts to the variable defined two lines above
}
public void Display()
{
Console.WriteLine("Cost: {0}", cost); // while this refers to the field
}
}
cost
from内的GetCost
将引用本地变量,而在显示中使用cost
将引用字段
这是绝对正确的。但是它会导致混乱,从而导致意外行为。这就是为什么一些开发人员倾向于使用This
-限定符:
public double GetCost()
{
double cost;
this.cost = GetArea() * 70;
return this.cost;
}
使用限定符引用当前实例,使其成为。
需要访问字段而不是变量。在成员范围(在方法中)中定义与现有成员同名的变量时,只需隐藏后者并引用前者
在你的例子中:
class Tabletop : Rectangle
{
private double cost;
public Tabletop(double l, double w) : base(l, w) { }
public double GetCost()
{
double cost; // this hides the field
cost = GetArea() * 70;
return cost; // this referts to the variable defined two lines above
}
public void Display()
{
Console.WriteLine("Cost: {0}", cost); // while this refers to the field
}
}
cost
from内的GetCost
将引用本地变量,而在显示中使用cost
将引用字段
这是绝对正确的。但是它会导致混乱,从而导致意外行为。这就是为什么一些开发人员倾向于使用This
-限定符:
public double GetCost()
{
double cost;
this.cost = GetArea() * 70;
return this.cost;
}
使用引用当前实例的限定符,使这个。
cost`可以访问字段而不是变量
在课堂桌面上,有两次申报成本,一次为私人成本
双倍成本;4行之后作为双倍成本
好的,private double cost;
是tableTop
类的成员字段,而其他声明是方法体的本地声明。为什么会出现混淆
在课堂桌面上,有两次申报成本,一次为私人成本
双倍成本;4行之后作为双倍成本
好的,private double cost;
是tableTop
类的成员字段,而其他声明是方法体的本地声明。为什么会出现混淆。private double cost;
未使用,可以删除
您不会得到错误,因为正如John在评论中所说,它在不同的范围内;一个被定义为类的字段,另一个是局部变量。当使用cost
时,会访问局部变量。要访问字段,可以使用this.cost
class A
{
private int a = 1;
void A()
{
int a = 2;
Console.WriteLine(a); // 2
Console.WriteLine(this.a); // 1
}
}
注意:即使在不同的作用域中,也不能有多个同名的局部变量:
void A()
{
int a = 1;
if(someCondition)
{
int b = 2; // Compiler error: A local variable named 'a' cannot be declared in this scope because it would give a different meaning to 'a', which is already used in a 'parent or current' scope to denote something else
}
}
私人双重成本;
未使用,可以删除
您不会得到错误,因为正如John在评论中所说,它在不同的范围内;一个被定义为类的字段,另一个是局部变量。当使用cost
时,会访问局部变量。要访问字段,可以使用this.cost
class A
{
private int a = 1;
void A()
{
int a = 2;
Console.WriteLine(a); // 2
Console.WriteLine(this.a); // 1
}
}
注意:即使在不同的作用域中,也不能有多个同名的局部变量:
void A()
{
int a = 1;
if(someCondition)
{
int b = 2; // Compiler error: A local variable named 'a' cannot be declared in this scope because it would give a different meaning to 'a', which is already used in a 'parent or current' scope to denote something else
}
}
我想他们确实忘了把它拿走
至于为什么没有得到“cost is ready defined”错误,这是因为GetCost()
中的double cost
是本地的(只能在GetCost()
方法中访问,并且将在GetCost()
方法完成后从内存中销毁),而私有双重成本
可供整个桌面
类访问,并且只要桌面
实例处于活动状态,就会一直保存在内存中。我想他们确实忘了删除它
至于为什么没有得到“cost is ready defined”错误,这是因为GetCost()
中的double cost
是本地的(只能在GetCost()
方法中访问,并且将在GetCost()
方法完成后从内存中销毁),而私有双重成本
可用于要访问的整个桌面
类,并且只要桌面
实例处于活动状态,就会保存在内存中。Voteup(+1)用于