c#不理解调用方法.面向对象编程

本文关键字:面向对象编程 方法 调用 不理解 | 更新日期: 2023-09-27 18:16:23

我刚刚开始面向对象编程,我不太明白如何调用另一个方法。我想计算增加的百分比,然后给出最终的总价格。这将通过调用方法来完成计算,然后将值返回给另一个方法来完成。然而,我不确定如何在方法之间交叉。

谁能解释一下我该怎么做?最好不要只是给出答案这样我就能完全理解发生了什么。

注。这段代码位于一个叫做RetailPricing的类中。由于我已经复制并粘贴了它,它看起来并没有完全格式化(我知道如何将这个类调用到主程序)

namespace week7exercise2
{
    class RetailPricing
    {
        public void CalculateRetailPrice()
        {
            double inputcost;
            double inputpercent;
            string inputitemcost;
            string inputmarkup;
            Console.Write("Please Input The Cost Of The Item: ");
            inputitemcost = Console.ReadLine();
            inputcost = double.Parse(inputitemcost);
            Console.Write("Please Input The Markup Percentage: ");
            inputmarkup = Console.ReadLine();
            inputpercent = double.Parse(inputmarkup);
            Console.Write("Your Retail Price Is: " + newprice);
        }
        public double sum(double MarkUpPercentage, double overallprice, double newprice)
        {            
            MarkUpPercentage = inputpercent + 100;
            overallprice = MarkUpPercentage / 100;
            newprice = inputcost * overallprice;
            return newprice;
        }
    }   
}

c#不理解调用方法.面向对象编程

我的理解是你想调用一个函数来执行一些计算。

最后一行前:

Console.Write("Your Retail Price Is: " + newprice);

你需要做的是调用计算价格的函数,它会在计算后返回价格。所以只要这样做:

double newprice = sum(inputpercent,inputcost)

并将sum函数更改为:

public double sum(double MarkUpPercentage, double overallprice)
    {            
        MarkUpPercentage = inputpercent + 100;
        overallprice = MarkUpPercentage / 100;
        double newprice = inputcost * overallprice;
        return newprice;
    }