自学内容网 自学内容网

C#里,Class(类)&方法的建立,实例化&应用,以及class里方法的不同调用说明

C# 类(Class)

当你定义一个类时,你定义了一个数据类型的蓝图。这实际上并没有定义任何的数据,但它定义了类的名称意味着什么,也就是说,类的对象由什么组成及在这个对象上可执行什么操作。对象是类的实例。构成类的方法和变量称为类的成员。

C# 方法

一个方法是把一些相关的语句组织在一起,用来执行一个任务的语句块。要使用一个方法,您需要:

  • 定义方法
  • 调用方法

 C# 编程实例

界面: 

 

代码: 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApp1
{
    public partial class Form6 : Form
    {

        NumberOpertor numberOpertor = new NumberOpertor();//实例化
        private void button5_Click(object sender, EventArgs e)
        {       
            //Max值计算
            numericUpDown4.Value = numberOpertor.FindMax(Convert.ToInt32(numericUpDown2.Value), Convert.ToInt32(numericUpDown3.Value));
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //Min值计算
            numericUpDown5.Value = numberOpertor.FindMin(Convert.ToInt32(numericUpDown2.Value), Convert.ToInt32(numericUpDown3.Value));
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //平均值计算
            numericUpDown6.Value = numberOpertor.FindAverage(Convert.ToInt32(numericUpDown2.Value), Convert.ToInt32(numericUpDown3.Value));
        }
        class NumberOpertor//数据操作的类
        {
            public int FindMax(int num1, int num2)
            {
                /* 局部变量声明 */
                int result;

                if (num1 > num2)
                    result = num1;
                else
                    result = num2;

                return result;
            }
            public int FindMin(int num1, int num2)
            {
                /* 局部变量声明 */
                int result;

                if (num1 > num2)
                    result = num2;
                else
                    result = num1;

                return result;
            }
            public int FindAverage(int num1, int num2)
            {
                /* 局部变量声明 */
                int result;


                result = (num1 + num2) / 2;


                return result;
            }

        }
    }

}


原文地址:https://blog.csdn.net/weixin_53520014/article/details/142410264

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!