2.面向对象(类、对象、属性)

3 阅读3分钟

1.构造函数与方法的参数/返回值(机器视觉)

场景1 :你现在在做 VisionPro 的斑点检测(Blob Tool),你需要写一个方法来判断算出来的斑点面积是不是合格的。

题目 :

  1. 创建一个类叫 BlobTool 。
  2. 里面有一个属性叫 Area (面积,双精度浮点数 double )。
  3. 写一个 构造函数 :在 new 这个对象的时候,必须传入一个面积值,并赋值给 Area 。
  4. 写一个方法叫 CheckPass 。它接收两个参数: minArea (最小合格面积)和 maxArea (最大合格面积)。
  5. 这个方法要返回一个 bool 值。如果当前的 Area 在这个范围内,返回 true (PASS),否则返回 false (NG)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace mediunm
{
    class BlobTool
    {
        public double Area{get;set;};
        public BlobTool(double inputArea)
        {
            Area = inputArea;
        }

        public bool checkPass(double minArea,double maxArea) 
        {
            if (Area >= minArea && Area <= maxArea)
            {
                Console.WriteLine("OK");
                return true;
                
            }
            else 
            {
                Console.WriteLine("NG");
                return false;
            }
        }
    }


    internal class Program
    {
        static void Main(string[] args)
        {
            BlobTool blobTool = new BlobTool(150);
            bool blobToolnNum = blobTool.checkPass(100, 200);
        }
    }
}

2.上位机坐标系(多个参数的传递与返回值)

场景2 :VisionPro 里的 PMAlign(模板匹配)工具算出了产品的 X 和 Y 坐标,但它是以像素为单位的。你需要写一个方法,把像素坐标转换成机械手能懂的毫米(mm)坐标。

题目 :

  1. 创建一个类 CoordinateMath (坐标计算)。
  2. 在类里面写一个方法 PixelToMm 。
  3. 这个方法需要接收 3 个参数 : pixelX (X像素坐标,double类型), pixelY (Y像素坐标,double类型), ratio (像素精度,也就是1像素等于多少毫米,double类型)。
  4. 这个方法必须 返回一个字符串 (string),格式像这样:"机械手坐标:X=10.5mm, Y=20.5mm"。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace medium3
{
    class CoordinateMath 
    {
        public string PixelToMm(double pixelX, double pixelY, double ratio) 
        {
            double mmx = pixelX * ratio;
            double mmy = pixelY * ratio;

            return $"机械手坐标: X = {mmx}mm, Y = {mmy}mm, ratio = {ratio}";
        }
    }


    internal class Program
    {
        static void Main(string[] args)
        {
            CoordinateMath coordinatemath = new CoordinateMath();
            string math = coordinatemath.PixelToMm(1.5, 2, 3);
            Console.WriteLine(math);
        }
    }
}

3.PLC 气缸控制(带状态判断的复杂方法)

场景 :你要通过串口或者网线给 PLC 发指令,让一个气缸去夹产品。但夹之前,必须先判断气缸有没有气,以及门关没关好。

题目 :

  1. 创建一个类 CylinderController (气缸控制器)。
  2. 添加两个属性: HasAir (是否有气,bool)和 IsDoorClosed (安全门是否关闭,bool)。
  3. 编写一个 构造函数 :在创建控制器时,必须初始化 HasAir 和 IsDoorClosed 。
  4. 编写一个方法 Clamp (夹紧),它 不需要传入参数 (因为直接读取自身的属性),但要 返回一个 bool (是否夹紧成功)。
  5. 逻辑:只有当 HasAir 为 true 且 IsDoorClosed 为 true 时,才返回 true 并打印“气缸已夹紧”;否则返回 false 并打印“报警:条件不满足”。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace medium4
{
    class CylinderController
    {
        public bool HasAir{get;set;};
        public bool IsDoorClosed{get;set;};

        public CylinderController(bool air,bool closed)
        {
            HasAir = air;
            IsDoorClosed = closed;
        }

        public bool Clamp() 
        {
            if (HasAir == true && IsDoorClosed == true)
            {

                Console.WriteLine("气缸已夹紧");
                return true;
            }
            else 
            {
                Console.WriteLine("条件不满足");
                return false;
            }
            
        }
}

internal class Program
    {
        static void Main(string[] args)
        {
            CylinderController cylinderController = new CylinderController(true,true);
            cylinderController.Clamp();

            CylinderController cylinderController1 = new CylinderController(false, true);
            cylinderController1.Clamp();

        }
    }
}

4.上位机权限管理(结合字符串与类的综合运用)

场景 :在你们公司的机台上,通常有“操作员(Operator)”和“管理员(Admin)”两个权限。你需要写一个登录模块。

题目 :

  1. 创建一个类 UserLogin 。
  2. 里面有一个私有字段(或者属性)保存着正确的管理员密码,比如叫 correctPassword = "888" 。
  3. 写一个方法叫 Login 。它接收两个参数: userName (用户名,string)和 password (密码,string)。
  4. 这个方法返回一个 int (代表权限等级):
    • 如果用户名是 "Admin",不需要管密码,直接返回 1 (操作员权限)。
    • 如果用户名是 "Operator" 且 密码等于 correctPassword ,返回 99 (最高权限)。
    • 其他情况(比如密码错了),返回 0 (登录失败)。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace medium5
{
    class UserLogin
    {
        private string correctPassword = "123";
        public int Login(string userName, string password)
        {
            if (userName == "Operator" && password == correctPassword)
            {
                Console.WriteLine("操作员登录成功");
                return 1;
            }
            else if (userName == "Admin")
            {
                Console.WriteLine("管理员登录成功");
                return 1;
            }
            else
            {
                Console.WriteLine("登录失败");
                return 0;
            }

        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            UserLogin userLogin = new UserLogin();
            userLogin.Login("Operator", "123");
            userLogin.Login("Admin", "");
        }
    }
}