Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 初始化基类:两次声明变量?_C#_Class_Inheritance_Declare - Fatal编程技术网

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;

我目前正在读一本C#教程。现在我遇到了这样一个问题:

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)用于