【问题标题】:C# Inheritance and MethodsC# 继承和方法
【发布时间】:2019-06-21 09:06:43
【问题描述】:

我正在学习继承,我理解下面的代码。

namespace InheritanceApplication {
   class Shape {
      public void setWidth(int w) {
         width = w;
      }
      public void setHeight(int h) {
         height = h;
      }
      protected int width;
      protected int height;
   }

   // Base class PaintCost
   public interface PaintCost {
      int getCost(int area);
   }

   // Derived class
   class Rectangle : Shape, PaintCost {
      public int getArea() {
         return (width * height);
      }
      public int getCost(int area) {
         return area * 70;
      }
   }
   class RectangleTester {
      static void Main(string[] args) {
         Rectangle Rect = new Rectangle();
         int area;

         Rect.setWidth(5);
         Rect.setHeight(7);
         area = Rect.getArea();

         // Print the area of the object.
         Console.WriteLine("Total area: {0}",  Rect.getArea());
         Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
         Console.ReadKey();
      }
   }
}

但是,他们为什么要创建设置高度和设置宽度功能。简单地这样做不是更好的做法吗:

public int width {get;set;}
public int height {get;set;}

然后在主类中执行如下操作:

rect.width = 5;
rect.height = 7;

非常感谢,

阿米尔

【问题讨论】:

  • 更好的做法是简单地这样做是什么让你认为它“更好”?只是不一样
  • 根据这些二传手的工作量,使用其中一个可能是一个更好的主意。一般来说,您应该为某种数据使用属性,并使用一种方法来实际执行一个动作。但没有任何规则强迫你这样做。
  • 看起来“他们”的灵感来自于 Java。 IMO,99.99% 的 C# 开发人员不会在这里使用 set 方法。
  • 说了我所说的,这里有 nio “正确”或“错误”的答案。您喜欢哪种风格非常基于意见。
  • 这段代码看起来像是java开发者在尝试学习C#

标签: c# inheritance methods multiple-inheritance access-modifiers


【解决方案1】:

我相信其他人会提供不同的观点,但这里是我使用gets/sets 的两个主要原因。如果这些不适用于给定的属性,我很可能不会使用 getter/setter。

1 - 调试
如果您可以调试您关心的 setter,那么调试数据传播(数据如何传递)会变得非常容易。如果您担心它被传递了错误的值,您可以轻松地调用Debug.Print 并调试正在设置的值。或者您可以放置​​断点并通过堆栈跟踪进行实际调试。例如:

   class Shape {
       public void setWidth(int w) {
           if(w < 0)
               Debug.Print("width is less than 0!");

           width = w;
       }
       public void setHeight(int h) {
           height = h;
       }
       protected int width;
       protected int height;
    }

2 - 值更改操作
可能有更好的方法来实现这一点,但我喜欢能够向设置器添加简单的逻辑,以确保在值更改时需要运行的任何逻辑都这样做。例如,我可以使用以下内容:

public void SetWindowHeight(int newHeight)
{
    if(WindowHeight == newHeight)
        return;

    WindowHeight = newHeight;

    UpdateWindowDisplay();
}
public int GetWindowHeight()
{
    return WindowHeight;
}

private int WindowHeight;


public void UpdateWindowDisplay()
{
    Window.UpdateHeight(WindowHeight);
    // Other window display logic
}

虽然我个人更喜欢使用属性获取/设置,但这只是我的偏好。

public int WindowHeight
{
    get
    {
        return windowHeight;
    }
    set
    {
        if(windowHeight == value)
            return;

        windowHeight = value;

        UpdateWindowDisplay();
    }
}
private int windowHeight;

public void UpdateWindowDisplay()
{
    Window.UpdateHeight(WindowHeight);
    // Other window display logic
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 2012-04-25
    • 1970-01-01
    • 2015-03-20
    • 2023-03-28
    • 2021-08-22
    • 2014-10-04
    相关资源
    最近更新 更多