简体   繁体   English

如何使用OOP或SOLID原理避免代码重复?

[英]How can I avoid code duplication by using OOP or SOLID principles?

I have the following class and interface for him: 我为他准备了以下课程和接口:

public class A // int wrapper
{
    private int _a;

    public A(int a)
    {
        _a = a;
    }
}

interface IProgram
{
    int a();

    A b();
}

public class Program : IProgram
{
    public int a()
    {
       int b = 0;
       b++;
       return b;
    }

    public A b()
    {
        int b = 0;
        b++;
        return new A(b);
    }
}

Two methods do the same thing: just increase b . 两种方法做同样的事情:增加b

How can I avoid code duplication and change my interface? 如何避免代码重复并更改界面?

创建一个私有方法,该方法迭代int b并将其返回,然后从a()和b()调用该方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM