重构重复代码 - 唯一的区别是<标志

时间:2016-06-17 18:05:47

标签: c# polymorphism

班级考试是abstract。它包含一个基本的testname和一个分数。该分数(双倍)可通过名为TestScore的属性访问。

三种类继承自Test。它们是如此不同,以至于每种测试都有不同的评分方式。

目标:根据分数之间的一系列值对6个评分中的一个评分。

class ScoreRangeAscendingTest : Test
{
    public ScoreRangeAscendingTest(double score, Enums.TestType typeName) : base(score, typeName) { }
    public double[] ranges;
    public Enums.Rating calculateScore(double[] range)
    {
        if (scoreInVeryWeakRange())
            return Enums.Rating.VeryWeak;
        else if (scoreInWeakRange())
            return Enums.Rating.Weak;
        else if (scoreInAveragelyWeakRange())
            return Enums.Rating.AveragelyWeak;
        else if (scoreInAveragelyGoodRange())
            return Enums.Rating.AveragelyGood;
        else if (scoreInGoodRange())
            return Enums.Rating.Good;
        else 
            return Enums.Rating.VeryGood;
    }

    private bool scoreInVeryWeakRange()
    {
        return this.TestScore < ranges[(int)Enums.Border.P10];
    }
    private bool scoreInWeakRange()
    {
        return TestScore >= ranges[(int)Enums.Border.P10] && TestScore < ranges[(int)Enums.Border.P25];
    }
    private bool scoreInAveragelyWeakRange()
    {
        return TestScore >= ranges[(int)Enums.Border.P25] && TestScore < ranges[(int)Enums.Border.P50];
    }
    private bool scoreInAveragelyGoodRange()
    {
        return TestScore >= ranges[(int)Enums.Border.P50] && TestScore < ranges[(int)Enums.Border.P75];
    }
    private bool scoreInGoodRange()
    {
        return TestScore >= ranges[(int)Enums.Border.P75] && TestScore < ranges[(int)Enums.Border.P90];
    }
    private bool scoreInVeryGoodRange()
    {
        return TestScore > ranges[(int)Enums.Border.P90];
    }

}

}

问题:我有相应范围上升的测试以及相应范围下降的测试。所以我需要为第二种类型的测试编写完全相同的代码,但这一次,所有'&lt;'标志需要'&gt;'标志,反之亦然。

问题:如何避免重复此代码?是否有可能将其添加到基类?

2 个答案:

答案 0 :(得分:2)

我不是100%确定我得到了这个问题,但您可以使用委托来执行比较。

以下内容应与您当前的代码相同:

/*
 *** MADE BY MRPONYCAPTAIN'S .SCHEMATIC TO .JAVA CONVERTING TOOL v0 ***
*/

package com.crazyhoorsecore.WorldGen;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.world.World;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.gen.feature.WorldGenerator;
import net.minecraftforge.fml.common.IWorldGenerator;

public abstract class Trap extends WorldGenerator implements IWorldGenerator
{
    protected Block[] getValidSpawnBlocks() {
        return new Block[] {
            Blocks.grass
        };
    }

    public boolean locationIsValidSpawn(World world, int i, int j, int k){
        int distanceToAir = 0;
        Block check = world.getBlock(i, j, k);

        while (check != Blocks.air){
            if (distanceToAir > 3){
                return false;
            }

            distanceToAir++;
            check = world.getBlock(i, j + distanceToAir, k);
        }

        j += distanceToAir - 1;

        Block block = (world).getBlock(i, j, k);
        Block blockAbove = world.getBlock(i, j+1, k);
        Block blockBelow = world.getBlock
                (i, j-1, k);

        for (Block x : getValidSpawnBlocks()){
            if (blockAbove != Blocks.air){
                return false;
            }
            if (block == x){
                return true;
            }else if (block == Blocks.snow && blockBelow == x){
                return true;
            }
        }

        return false;
    }

    public Trap() { }

    @Override
    public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) { }

    public void setBlock(World world, int x, int y, int z, Block block, int metadata)
    {
        Block b1 = world.getBlock(x, y, z);

        if(bisAir(world, x, y, z) || bisLeaves(world, x, y, z))
        {
            (world).setBlockState(x, y, z, block, metadata, 2);
        }
    }





    public boolean generate(World world, Random rand, int i, int j, int k) {
        //check that each corner is one of the valid spawn blocks
        if(!locationIsValidSpawn(world, i, j, k) || !locationIsValidSpawn(world, i + 6, j, k) || !locationIsValidSpawn(world, i + 6, j, k + 5) || !locationIsValidSpawn(world, i, j, k + 5))
        {
            return false;
        }

        k = k - 10;
        i = i - 10;

        this.setBlock(world, i + 0, j + 0, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 0, k + 1, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 0, k + 2, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 0, k + 3, Blocks.grass, 0);
        this.setBlock(world, i + 0, j + 0, k + 4, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 0, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 1, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 1, k + 1, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 1, k + 2, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 1, k + 3, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 1, k + 4, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 1, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 2, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 2, k + 1, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 2, k + 2, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 2, k + 3, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 2, k + 4, Blocks.dirt, 0);
        this.setBlock(world, i + 0, j + 2, k + 5, Blocks.grass, 0);
        this.setBlock(world, i + 0, j + 3, k + 0, Blocks.stonebrick, 0);
        this.setBlock(world, i + 0, j + 3, k + 1, Blocks.stonebrick, 0);
        this.setBlock(world, i + 0, j + 3, k + 2, Blocks.stone, 0);
        this.setBlock(world, i + 0, j + 3, k + 3, Blocks.stone, 0);
        this.setBlock(world, i + 0, j + 3, k + 4, Blocks.stonebrick, 0);
        this.setBlock(world, i + 0, j + 3, k + 5, Blocks.stonebrick, 0);
        this.setBlock(world, i + 0, j + 4, k + 1, Blocks.stone_slab, 5);
        this.setBlock(world, i + 0, j + 4, k + 4, Blocks.stone_slab, 5);
        this.setBlock(world, i + 1, j + 0, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 1, j + 0, k + 1, Blocks.dirt, 0);
        this.setBlock(world, i + 1, j + 0, k + 2, Blocks.grass, 0);
        this.setBlock(world, i + 1, j + 0, k + 3, Blocks.grass, 0);
        this.setBlock(world, i + 1, j + 0, k + 4, Blocks.grass, 0);
        this.setBlock(world, i + 1, j + 0, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 1, j + 1, k + 0, Blocks.grass, 0);
        this.setBlock(world, i + 1, j + 1, k + 1, Blocks.grass, 0);
        this.setBlock(world, i + 1, j + 1, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 1, j + 2, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 1, j + 2, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 1, j + 3, k + 0, Blocks.stonebrick, 0);
        this.setBlock(world, i + 1, j + 3, k + 1, Blocks.dirt, 0);
        this.setBlock(world, i + 1, j + 3, k + 2, Blocks.stone, 0);
        this.setBlock(world, i + 1, j + 3, k + 3, Blocks.stone, 0);
        this.setBlock(world, i + 1, j + 3, k + 4, Blocks.dirt, 0);
        this.setBlock(world, i + 1, j + 3, k + 5, Blocks.stonebrick, 0);
        this.setBlock(world, i + 1, j + 4, k + 0, Blocks.stone_slab, 5);
        this.setBlock(world, i + 1, j + 4, k + 1, Blocks.stonebrick, 3);
        this.setBlock(world, i + 1, j + 4, k + 4, Blocks.stonebrick, 1);
        this.setBlock(world, i + 1, j + 4, k + 5, Blocks.stone_slab, 5);
        this.setBlock(world, i + 1, j + 5, k + 1, Blocks.stonebrick, 1);
        this.setBlock(world, i + 1, j + 5, k + 4, Blocks.stonebrick, 3);
        this.setBlock(world, i + 1, j + 6, k + 1, Blocks.stonebrick, 3);
        this.setBlock(world, i + 1, j + 6, k + 4, Blocks.stonebrick, 1);
        this.setBlock(world, i + 1, j + 7, k + 1, Blocks.stone_slab, 0);
        this.setBlock(world, i + 1, j + 7, k + 2, Blocks.stone_slab, 0);
        this.setBlock(world, i + 1, j + 7, k + 3, Blocks.stone_slab, 0);
        this.setBlock(world, i + 1, j + 7, k + 4, Blocks.stone_slab, 0);
        this.setBlock(world, i + 2, j + 0, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 2, j + 0, k + 1, Blocks.dirt, 0);
        this.setBlock(world, i + 2, j + 0, k + 2, Blocks.grass, 0);
        this.setBlock(world, i + 2, j + 0, k + 3, Blocks.dirt, 0);
        this.setBlock(world, i + 2, j + 0, k + 4, Blocks.grass, 0);
        this.setBlock(world, i + 2, j + 0, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 2, j + 1, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 2, j + 1, k + 1, Blocks.grass, 0);
        this.setBlock(world, i + 2, j + 1, k + 2, Blocks.stone, 0);
        this.setBlock(world, i + 2, j + 1, k + 3, Blocks.stone, 0);
        this.setBlock(world, i + 2, j + 1, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 2, j + 2, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 2, j + 2, k + 2, Blocks.tnt, 0);
        this.setBlock(world, i + 2, j + 2, k + 3, Blocks.tnt, 0);
        this.setBlock(world, i + 2, j + 2, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 2, j + 3, k + 0, Blocks.stone, 0);
        this.setBlock(world, i + 2, j + 3, k + 1, Blocks.stone, 0);
        this.setBlock(world, i + 2, j + 3, k + 2, Blocks.stone, 0);
        this.setBlock(world, i + 2, j + 3, k + 3, Blocks.stone, 0);
        this.setBlock(world, i + 2, j + 3, k + 4, Blocks.stone, 0);
        this.setBlock(world, i + 2, j + 3, k + 5, Blocks.stone, 0);
        this.setBlock(world, i + 2, j + 7, k + 1, Blocks.stone_slab, 0);
        this.setBlock(world, i + 2, j + 7, k + 2, Blocks.stonebrick, 5);
        this.setBlock(world, i + 2, j + 7, k + 3, Blocks.double_stone_slab, 5);
        this.setBlock(world, i + 2, j + 7, k + 4, Blocks.stone_slab, 0);
        this.setBlock(world, i + 3, j + 0, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 3, j + 0, k + 1, Blocks.grass, 0);
        this.setBlock(world, i + 3, j + 0, k + 2, Blocks.grass, 0);
        this.setBlock(world, i + 3, j + 0, k + 3, Blocks.dirt, 0);
        this.setBlock(world, i + 3, j + 0, k + 4, Blocks.grass, 0);
        this.setBlock(world, i + 3, j + 0, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 3, j + 1, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 3, j + 1, k + 2, Blocks.dirt, 0);
        this.setBlock(world, i + 3, j + 1, k + 3, Blocks.dirt, 0);
        this.setBlock(world, i + 3, j + 1, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 3, j + 2, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 3, j + 2, k + 1, Blocks.dirt, 0);
        this.setBlock(world, i + 3, j + 2, k + 2, Blocks.tnt, 0);
        this.setBlock(world, i + 3, j + 2, k + 3, Blocks.tnt, 0);
        this.setBlock(world, i + 3, j + 2, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 3, j + 3, k + 0, Blocks.stone, 0);
        this.setBlock(world, i + 3, j + 3, k + 1, Blocks.stone, 0);
        this.setBlock(world, i + 3, j + 3, k + 2, Blocks.grass, 0);
        this.setBlock(world, i + 3, j + 3, k + 3, Blocks.grass, 0);
        this.setBlock(world, i + 3, j + 3, k + 4, Blocks.stone, 0);
        this.setBlock(world, i + 3, j + 3, k + 5, Blocks.stone, 0);
        this.setBlock(world, i + 3, j + 4, k + 2, Blocks.air, 5);
        this.setBlock(world, i + 3, j + 4, k + 3, Blocks.air, 5);
        this.setBlock(world, i + 3, j + 7, k + 1, Blocks.stone_slab, 0);
        this.setBlock(world, i + 3, j + 7, k + 2, Blocks.double_stone_slab, 5);
        this.setBlock(world, i + 3, j + 7, k + 3, Blocks.double_stone_slab, 5);
        this.setBlock(world, i + 3, j + 7, k + 4, Blocks.stone_slab, 0);
        this.setBlock(world, i + 4, j + 0, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 4, j + 0, k + 1, Blocks.grass, 0);
        this.setBlock(world, i + 4, j + 0, k + 2, Blocks.dirt, 0);
        this.setBlock(world, i + 4, j + 0, k + 3, Blocks.dirt, 0);
        this.setBlock(world, i + 4, j + 0, k + 4, Blocks.grass, 0);
        this.setBlock(world, i + 4, j + 0, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 4, j + 1, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 4, j + 1, k + 2, Blocks.stone, 0);
        this.setBlock(world, i + 4, j + 1, k + 3, Blocks.stone, 0);
        this.setBlock(world, i + 4, j + 1, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 4, j + 2, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 4, j + 2, k + 2, Blocks.tnt, 0);
        this.setBlock(world, i + 4, j + 2, k + 3, Blocks.tnt, 0);
        this.setBlock(world, i + 4, j + 2, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 4, j + 3, k + 0, Blocks.stone, 0);
        this.setBlock(world, i + 4, j + 3, k + 1, Blocks.stone, 0);
        this.setBlock(world, i + 4, j + 3, k + 2, Blocks.stone, 0);
        this.setBlock(world, i + 4, j + 3, k + 3, Blocks.stone, 0);
        this.setBlock(world, i + 4, j + 3, k + 4, Blocks.stone, 0);
        this.setBlock(world, i + 4, j + 3, k + 5, Blocks.stone, 0);
        this.setBlock(world, i + 4, j + 7, k + 1, Blocks.stone_slab, 0);
        this.setBlock(world, i + 4, j + 7, k + 2, Blocks.double_stone_slab, 5);
        this.setBlock(world, i + 4, j + 7, k + 3, Blocks.double_stone_slab, 5);
        this.setBlock(world, i + 4, j + 7, k + 4, Blocks.stone_slab, 0);
        this.setBlock(world, i + 5, j + 0, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 5, j + 0, k + 1, Blocks.grass, 0);
        this.setBlock(world, i + 5, j + 0, k + 2, Blocks.grass, 0);
        this.setBlock(world, i + 5, j + 0, k + 3, Blocks.grass, 0);
        this.setBlock(world, i + 5, j + 0, k + 4, Blocks.grass, 0);
        this.setBlock(world, i + 5, j + 0, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 5, j + 1, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 5, j + 1, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 5, j + 2, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 5, j + 2, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 5, j + 3, k + 0, Blocks.stonebrick, 0);
        this.setBlock(world, i + 5, j + 3, k + 1, Blocks.dirt, 0);
        this.setBlock(world, i + 5, j + 3, k + 2, Blocks.stone, 0);
        this.setBlock(world, i + 5, j + 3, k + 3, Blocks.stone, 0);
        this.setBlock(world, i + 5, j + 3, k + 4, Blocks.dirt, 0);
        this.setBlock(world, i + 5, j + 3, k + 5, Blocks.stonebrick, 0);
        this.setBlock(world, i + 5, j + 4, k + 0, Blocks.stone_slab, 5);
        this.setBlock(world, i + 5, j + 4, k + 1, Blocks.stonebrick, 3);
        this.setBlock(world, i + 5, j + 4, k + 4, Blocks.stonebrick, 1);
        this.setBlock(world, i + 5, j + 4, k + 5, Blocks.stone_slab, 5);
        this.setBlock(world, i + 5, j + 5, k + 1, Blocks.stonebrick, 1);
        this.setBlock(world, i + 5, j + 5, k + 4, Blocks.stonebrick, 3);
        this.setBlock(world, i + 5, j + 6, k + 1, Blocks.stonebrick, 3);
        this.setBlock(world, i + 5, j + 6, k + 4, Blocks.stonebrick, 1);
        this.setBlock(world, i + 5, j + 7, k + 1, Blocks.stone_slab, 0);
        this.setBlock(world, i + 5, j + 7, k + 2, Blocks.stone_slab, 0);
        this.setBlock(world, i + 5, j + 7, k + 3, Blocks.stone_slab, 0);
        this.setBlock(world, i + 5, j + 7, k + 4, Blocks.stone_slab, 0);
        this.setBlock(world, i + 6, j + 0, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 0, k + 1, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 0, k + 2, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 0, k + 3, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 0, k + 4, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 0, k + 5, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 1, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 1, k + 1, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 1, k + 2, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 1, k + 3, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 1, k + 4, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 1, k + 5, Blocks.grass, 0);
        this.setBlock(world, i + 6, j + 2, k + 0, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 2, k + 1, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 2, k + 2, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 2, k + 3, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 2, k + 4, Blocks.dirt, 0);
        this.setBlock(world, i + 6, j + 2, k + 5, Blocks.grass, 0);
        this.setBlock(world, i + 6, j + 3, k + 0, Blocks.stonebrick, 0);
        this.setBlock(world, i + 6, j + 3, k + 1, Blocks.stonebrick, 0);
        this.setBlock(world, i + 6, j + 3, k + 2, Blocks.stone, 0);
        this.setBlock(world, i + 6, j + 3, k + 3, Blocks.stone, 0);
        this.setBlock(world, i + 6, j + 3, k + 4, Blocks.stonebrick, 0);
        this.setBlock(world, i + 6, j + 3, k + 5, Blocks.stonebrick, 0);
        this.setBlock(world, i + 6, j + 4, k + 1, Blocks.stone_slab, 5);
        this.setBlock(world, i + 6, j + 4, k + 4, Blocks.stone_slab, 5);
        this.setBlock(world, i + 2, j + 4, k + 2, Blocks.stone_pressure_plate, 0);
        this.setBlock(world, i + 2, j + 4, k + 3, Blocks.stone_pressure_plate, 0);
        this.setBlock(world, i + 3, j + 4, k + 1, Blocks.stone_pressure_plate, 0);
        this.setBlock(world, i + 3, j + 4, k + 4, Blocks.stone_pressure_plate, 0);
        this.setBlock(world, i + 4, j + 4, k + 2, Blocks.stone_pressure_plate, 0);
        this.setBlock(world, i + 4, j + 4, k + 3, Blocks.stone_pressure_plate, 0);

        return true;
    }
}

但是,如添加的评论中所述,您现在可以使用其他比较功能,即将class ScoreRangeAscendingTest : Test { public ScoreRangeAscendingTest(double score, Enums.TestType typeName) : base(score, typeName) { } public double[] ranges; public Enums.Rating calculateScore(double[] range) { Func<double, double, bool> cmp = (x, y) => x < y; // you can swap the comparison here, e.g. (x, y) => x > y if (scoreInVeryWeakRange(cmp)) return Enums.Rating.VeryWeak; else if (scoreInWeakRange(cmp)) return Enums.Rating.Weak; else if (scoreInAveragelyWeakRange(cmp)) return Enums.Rating.AveragelyWeak; else if (scoreInAveragelyGoodRange(cmp)) return Enums.Rating.AveragelyGood; else if (scoreInGoodRange(cmp)) return Enums.Rating.Good; else return Enums.Rating.VeryGood; } private bool scoreInVeryWeakRange(Func<double, double, bool> cmp) { return cmp(this.TestScore, ranges[(int)Enums.Border.P10]); } private bool scoreInWeakRange(Func<double, double, bool> cmp) { return !cmp(TestScore, ranges[(int)Enums.Border.P10]) && cmp(TestScore, ranges[(int)Enums.Border.P25]); } private bool scoreInAveragelyWeakRange(Func<double, double, bool> cmp) { return !cmp(TestScore, ranges[(int)Enums.Border.P25]) && cmp(TestScore, ranges[(int)Enums.Border.P50]); } private bool scoreInAveragelyGoodRange(Func<double, double, bool> cmp) { return !cmp(TestScore, ranges[(int)Enums.Border.P50]) && cmp(TestScore, ranges[(int)Enums.Border.P75]); } private bool scoreInGoodRange(Func<double, double, bool> cmp) { return !cmp(TestScore, ranges[(int)Enums.Border.P75]) && cmp(TestScore, ranges[(int)Enums.Border.P90]); } private bool scoreInVeryGoodRange(Func<double, double, bool> cmp) { return (TestScore == ranges[(int)Enums.Border.P90]) || !cmp(TestScore, ranges[(int)Enums.Border.P90]); } } 换成<

答案 1 :(得分:1)

System.Double实施IComparable<double>。所以,如果你有几行代码,只有比较运算符,你可以这样写:

abstract class ComparisonBase
{
    protected bool IsSatisfiesCondition(double left, double right, params int[] conditions)
    {
        return conditions.Contains(left.CompareTo(right));
    }

    public abstract bool Compare(double left, double right);
}

class GreaterThan : ComparisonBase
{
    public override bool Compare(double left, double right)
    {
        // instead of left > right
        return IsSatisfiesCondition(left, right, 1);
    }
}

class LessThan : ComparisonBase
{
    public override bool Compare(double left, double right)
    {
        // instead of left < right
        return IsSatisfiesCondition(left, right, -1);
    }
}

class GreaterThanOrEqual : ComparisonBase
{
    public override bool Compare(double left, double right)
    {
        // instead of left >= right
        return IsSatisfiesCondition(left, right, 1, 0);
    }
}

这只是示例,但它应该让您了解如何在真实代码中应用它。