C#关于比较两个对象参数的编写方法的帮助

C#关于比较两个对象参数的编写方法的帮助,c#,C#,我试图写一个简单的代码,将有汽车规格输出文本,并告诉哪辆车有更多的惠普在程序结束 public class Engine { private int cubage; private double power; public int Cubage { get { return cubage; } set { cubage = value; }

我试图写一个简单的代码,将有汽车规格输出文本,并告诉哪辆车有更多的惠普在程序结束

 public class Engine
    {
    private int cubage;
    private double power;

        public int Cubage
        {
            get { return cubage; }
            set { cubage = value; }                  
        }

        public double Power
        {
            get { return power; }
            set { power = value; }
        }

        public double Power_HP(double power)
        {
            return power * 1.36;
        }
        public Engine(int cubage, double power)
        {
            this.cubage = cubage;
            this.power = power;
        }                           
        }
    }
    public class Body
    {
        private string manufacturer;
        private string model;
        private string color;

        public string Manufacturer
        {
            get { return manufacturer; }
            set { manufacturer = value; }
        }

        public string Model
        {
            get { return model; }
            set { model = value; }
        }

        public string Color
        {
            get { return color; }
            set { color = value; }
        }
        public Body (string manufacturer, string model, string color)
        {
            this.manufacturer = manufacturer;
            this.model = model;
            this.color = color;
        }
    public class Car
    {
        private Body body;
        private Engine engine;

        public Car(string manufacturer, string model, string bolor, int cubage, double power)
        {
            body = new Body(manufacturer, model, color);
            motor = new Motor(cubage, power);
        }
        public void Printo()
        {
            Console.WriteLine("Marka:" + sasija.Manufacturer);
            Console.WriteLine("Model:" + sasija.Model);
            Console.WriteLine("Boja:" + sasija.Color);
            Console.WriteLine("Zapremina:" + motor.Cubage);
            Console.WriteLine("Snaga u kW:" + motor.Power);
            /*Console.WriteLine("Snaga u HP" + Motor.Power_HP());*/

        }

        public void Difference() /*This is the method I want to use to show which car has more hp but I dont know how to succesfully compare their Snaga parameters*/
        {
            if (engine.Power > engine.Power)
                Console.WriteLine("Motor prvog auta je snažniji od motora drugog auta");
            else
                Console.WriteLine("Motor drugog auta je snažniji od motora prvog auta");

        }
    class Program
    {        
        static void Main(string[] args)
        {
            Console.WriteLine("Car 1:");
            Auto A = new Auto("Audi", "A6", "Black", 2500, 130);
            /* Console.WriteLine("Snaga u HP:" + A.Power_HP(130));*/
            A.Printo();
            Console.WriteLine("Car 2:");
            Auto B=new Auto("BMW", "320D", "Blue",2000,119);
            B.Printo();
            Difference();
            if (130 > 119)
                Console.WriteLine("Motor prvog auta je snažniji od motora drugog auta");
            else
                Console.WriteLine("Motor drugog auta je snažniji od motora prvog auta");

            /*Auto.Razlika(130,150);*/                 
           /* Console.WriteLine("Snaga u HP:" + B.Snaga_HP(119));*/
            Console.ReadLine();
        }
    }
    }
为了更好地理解此代码,我将编写我的文字翻译: 发动机 发动机容积 汽车制造商 汽车模型 波加色 Ispis输出文本方法 汽车车架 Razlika=区分哪辆车的马力更大的差异方法
我添加注释只是为了阻止一些代码行,以便在需要时保存以备将来使用。我希望你能理解我的问题是什么

基本要求是:

  • 将马达作为Auto类的公共属性公开
  • 创建一个接受两个自动并输出比较结果的静态方法和/或一个接受一个自动并与实例一起输出比较结果的实例方法

  • 如果您了解字符串插值和三值运算符,请跳过此操作

    应该注意的是,在我的代码中,我使用了一些C#6.0特性,比如“字符串插值”。当您看到这样的字符串:
    $“{first}的电机比{second}的电机更强大”
    ,这与:
    string.Format(“{0}的电机比{1}的电机更强大”,第一,第二)

    另外,如果您不熟悉三元运算符,当您看到以下内容时:

    var firstIsGreater = (first == null)
        ? second == null
        : first.Motor == null
            ? second.Motor == null
            : first.Motor.Power > second.Motor.Power;
    
    这与:

    bool firstIsGreater;
    if (first == null)
        firstIsGreater = (second == null);
    else if (first.Motor == null)
        firstIsGreater = (second.Motor == null);
    else
        firstIsGreater = (first.Motor.Power > second.Motor.Power);
    

    以下是我要做的一些更改

    仅由专用字段支持的属性可以设置为自动属性:

    public class Frame
    {
        public string Make { get; set; }
        public string Model { get; set; }
        public string Color { get; set; }
    
        public Frame(string make, string model, string color)
        {
            Make = make;
            Model = model;
            Color = color;
        }
    }
    
    此外,马力可以是基于功率的计算字段:

    public class Motor
    {
        public int Volume { get; set; }
        public double Power { get; set; }
        public double Horsepower { get { return Power * 1.36; } }
    
        public Motor(int volume, double power)
        {
            Volume = volume;
            Power = power;
        }
    }
    
    然后在
    Auto
    类上设置
    Motor
    属性
    public
    ,以便访问其属性。注意,我还重写了
    ToString
    方法,因此我们有一种友好的方式来显示自动名称

    不过,这里的主要变化是添加了两种
    showcomparation
    方法。其中一个是
    static
    ,这意味着您不需要使用自动实例来比较两辆车,另一个是实例方法,它将当前实例与其他一些自动实例进行比较:

    public class Auto
    {
        public Frame Frame { get; set; }
        public Motor Motor { get; set; }
    
        public Auto(string make, string model, string color, int volume, double power)
        {
            Frame = new Frame(make, model, color);
            Motor = new Motor(volume, power);
        }
    
        public void DisplayDetails()
        {
            Console.WriteLine("Make:" + Frame.Make);
            Console.WriteLine("Model:" + Frame.Model);
            Console.WriteLine("Color:" + Frame.Color);
            Console.WriteLine("Volume:" + Motor.Volume);
            Console.WriteLine("Power (kW):" + Motor.Power);
            Console.WriteLine("Horsepower:" + Motor.Horsepower);
        }
    
        public void ShowComparison(Auto other)
        {
            ShowComparison(this, other);
        }
    
        public static void ShowComparison(Auto first, Auto second)
        {
            var firstIsGreater = (first == null)
                ? second == null
                : first.Motor == null
                    ? second.Motor == null
                    : first.Motor.Power > second.Motor.Power;
    
            Console.WriteLine((firstIsGreater)
                ? $"The motor of {first} is more powerful than {second}"
                : $"The motor of {second} is more powerful than {first}");
        }
    
        public override string ToString()
        {
            return $"{Frame.Make} {Frame.Model}";
        }
    }
    
    现在,您可以通过以下三种方式显示差异:

    private static void Main()
    {
        Console.WriteLine("Auto 1:");
        Auto A = new Auto("Audi", "A6", "Crna", 2500, 130);
        A.DisplayDetails();
        Console.WriteLine();
    
        Console.WriteLine("Auto 2:");
        Auto B = new Auto("BMW", "320D", "Plava", 2000, 119);
        B.DisplayDetails();
        Console.WriteLine();
    
        // These all output the same text
        Auto.ShowComparison(A, B);
        A.ShowComparison(B);
        B.ShowComparison(A);
    
        Console.WriteLine("\nDone!\nPress any key to exit...");
        Console.ReadKey();
    }
    
    输出


    对不起,我不明白你想要什么。你需要一种方法,将其与汽车进行比较,并打印出有关HP更高的汽车的一些信息?是的,我想打印消息哪辆汽车的HP更高。但我需要一种方法。我不知道如何为此编写方法。在这种类模型中,您将
    Razlika
    作为一个函数,传入要比较的
    Auto
    的第二个实例(当前您正在与自身进行比较),并返回一个布尔值:
    public bool Razlika(Auto second_car){返回this.Snaga>second_car.Snaga;}
    ,然后调用like
    var result=A.Razlika(B)。这就是你的意思吗?需要将
    Snaga
    作为
    Auto
    类的属性公开,因为
    motor
    在您当前的设计中是私有的。@dlatikay:我认为您的解决方案是正确的。将此作为答案发布,Pavlony可以将其标记为已接受。@RufusL的问题是,我不知道如何对两辆汽车使用该方法并输出比较结果。我把它翻译成了英语。这太有帮助了!非常感谢你的好心人!我有一个愚蠢的问题,我刚学会C#编程。我用什么替换第一个和第二个?我只是键入A而不是第一个还是什么?
    first
    second
    static
    showcomparation
    方法的输入参数。第一个示例是直接调用此方法的示例(
    Auto.showcomparation(A,B);
    )。该方法也可以从
    实例
    版本间接调用,如第二个和第三个示例中所示。在第二个例子中:
    A.ShowComparison(B)
    ,如果遵循该代码,则该方法在内部调用静态方法,方法为:
    showcomparation(this,other)
    ,其中
    this
    A
    实例,
    other
    B
    实例。希望对您有所帮助?简而言之,您可以使用两个
    Auto
    实例调用
    Auto.showcomparation
    <代码>第一个
    将是您的
    汽车之一
    第二个
    将是另一个。是的,我按照您的指示修改了代码。非常感谢你!你有什么想法可以让我从这个节目中得到更多的东西吗?