简体   繁体   中英

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 .

How can I avoid code duplication and change my interface?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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