第六章 生产线上的功能

第六章 生产线上的功能

类的方法

C#是OOP语言,一切都是在类中。
在不使用顶级语句的控制台程序模板中,代码是这样的。

namespace ConsoleApp3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

观察第3行中,是不是也有类(class)标识符,之后才定义了静态方法Main()。这充分说明了一切都是在类中,函数也在类中。
只不过在类中的函数改了个名字,叫方法。在面向过程编程语言(如:C语言)中,是没有类和对象的,所以叫函数。

是时候造一台自己的削皮机器了

public class PareMachine { }

添加上属性

public class PareMachine
{
    public int Pares { get; set; }
}

定义类的方法

加上削皮功能

public class PareMachine
{
    public int Pares { get; set; }

    public void Pare()
    {
        for (int i = 1; i <= Pares; i++)
        {
            Console.WriteLine($"砍下第{i}刀,还剩下{Pares - i}刀");
            if (i == Pares)
            {
                Console.WriteLine($"已经砍下{Pares}刀,削完苹果皮,交给下一个机器处理");
            }
        }
    }
}

把前面定义的苹果类代码整合在一起

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 流水线

            // 放入一个需要砍80刀才能削完皮的苹果
            // 其他属性可设定也可以不设定,不设定的属性值默认为0或者空
            Apple a = new Apple { Weight = 180, Pares = 80 };

            // 装上一台削皮机
            PareMachine pm = new PareMachine();

            // 把要削的苹果皮数量传给削皮机的属性
            pm.Pares = a.Pares;

            pm.Pare();

            Console.WriteLine("继续下一步操作");
        }
    }

    public class Apple
    {
        public string Color { get; set; }
        public int Weight { get; set; }
        public int Pares { get; set; }
    }

    public class PareMachine
    {
        public int Pares { get; set; }

        public void Pare()
        {
            for (int i = 1; i <= Pares; i++)
            {
                Console.WriteLine($"砍下第{i}刀,还剩下{Pares - i}刀");
                if (i == Pares)
                {
                    Console.WriteLine($"已经砍下{Pares}刀,削完苹果皮,交给下一个机器处理");
                }
            }
        }
    }
}

定义构造方法

上面两个类Apple和PareMachine都定义了削片数量,有的螺栓,能不能根据苹果的重量自动判断削片数量呢。可以简化一下代码,同时在实例化PareMachine削皮机类时直接判断削片数量。
假设180克的需要80刀,190克的需要90刀,200克的需要100刀。可以在传入苹果时加一个if逻辑判断一下

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 流水线

            Apple a = new Apple { Weight = 180 };

            // 直接传入一个苹果的实例对象
            PareMachine pm = new PareMachine(a);

            pm.Pare();

            Console.WriteLine("继续下一步操作");
        }
    }
    public class Apple
    {
        public string Color { get; set; } = null;
        public int Weight { get; set; }
    }

    public class PareMachine
    {
        public int Pares { get; set; }

        // 定义一个构造函数
        // C#中构造函数是在创建给定类型的对象时执行的类方法
        // 构造函数的命名必须和类名相同
        // 构造函数没有返回值(void也不需要)
        public PareMachine(Apple apple)
        {
            if (apple.Weight == 180) Pares = 80;
            if (apple.Weight == 190) Pares = 90;
            if (apple.Weight == 200) Pares = 100;
        }
        public void Pare()
        {
            for (int i = 1; i <= Pares; i++)
            {
                if (i == Pares)
                {
                    Console.WriteLine($"已经砍下{Pares}刀,削完苹果皮,交给下一个机器处理");
                }
            }
        }
    }

}

更新了代码,给PareMachine增加了一个构造函数,构造函数是类在实例化对象时执行的方法,也有类的对象被销毁是执行的方法,叫析构函数。很少用到,一般都是C#自动处理的。

PareMachine构造函数接受一个Applel类型的对象,对传入对象的Weight属性值进行了判断,并分析出了这对象需要砍多少刀才能削完皮,设定了PareMachine的Pares值。

修改了Apple类的color属性,让color属性在未赋值是默认为空值。

方法重载

观察一下表格,每个学生都有三科成绩。

学号 姓名 语文 英语 数学
101 张三 80 97 88
102 李四 96 92 93
103 王五 60 93 70

尝试对这个表的每一行定义一个类

public class StudentScore
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Chinese { get; set; }
    public int English { get; set; }
    public int Math { get; set; }
}

然后呢?
总分是不是其他三科相加,可以定义一个方法,计算一下成绩。

public class StudentScore
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Chinese { get; set; }
    public int English { get; set; }
    public int Math { get; set; }

    public int Sum()
    {
        return Chinese + English + Math;
    }
}

完善一下代码,看看效果。

using System.Transactions;

namespace ConsoleApp3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            StudentScore studentScore = new StudentScore { Id = 101, Name = "张三", Chinese = 80, English = 97, Math = 88 };

            Console.WriteLine($"{studentScore.Name}的总分是{studentScore.Sum()}");
        }
    }
    public class StudentScore
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Chinese { get; set; }
        public int English { get; set; }
        public int Math { get; set; }

        public int Sum()
        {
            return Chinese + English + Math;
        }
    }
}

假如想在类里面实现更多的功能,比如语文、英语的分数按一个比例计算后再计入总分,是不是需要再加一个方法,再修改输出代码?没那么复杂,方法重载即可。

方法重载(Method Overloading)是指在同一个类中,可以有多个同名的方法,但这些方法的参数列表(参数的类型、数量或顺序)必须不同。可以通过方法调用时传入的参数类型和数量来决定实际调用哪个方法。

namespace ConsoleApp3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            StudentScore studentScore = new StudentScore { Id = 101, Name = "张三", Chinese = 80, English = 97, Math = 88 };

            Console.WriteLine($"{studentScore.Name}的总分是{studentScore.Sum()}");
            Console.WriteLine($"按照语文和英语除以2后,{studentScore.Name}的总分是{studentScore.Sum(0.5)}");
        }
    }
    public class StudentScore
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Chinese { get; set; }
        public int English { get; set; }
        public int Math { get; set; }

        public int Sum()
        {
            return Chinese + English + Math;
        }

        public int Sum(double proportion)
        {
            // 传入的是double类型,计算结束后,需要将结果再转回int类型
            return Convert.ToInt32(Chinese * proportion + English * proportion + Math);
        }
    }
}
posted @ 2025-05-26 18:16  冲浪的奶糖  阅读(11)  评论(0)    收藏  举报