C# 继承和方法

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

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

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;

非常感谢,

阿米尔

最佳答案

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

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 - 值(value)改变行动
可能有更好的方法来实现这一点,但我喜欢能够向 setter 添加简单的逻辑,以确保在值更改时需要运行的任何逻辑都这样做。例如,我可以使用以下内容:

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
}

关于C# 继承和方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54400705/

相关文章:

python - 从作为 python 脚本的 Linux 进程调用类方法

c# - IMutableEntityType.GetProperties() 不包含我想要的属性

c# - 为什么结构不能包含可为空的循环引用?

java - 私有(private)成员和继承

c++ - 在 Linux 上编译 C++ 代码。需要使用intel/icpc编译器。错误: "multiple definitions" related to inerited class

java - JFrame 更改边框颜色

c# - 正则表达式值更新

c# - .Net 相当于 JCEKS keystore

java - 如何达到扩展类的功能?

c# - 是否可以更改预定义方法可以在 C# 中采用的参数?