我可以不吃啊 发表于 2024-7-30 23:05:50

c#实际开发长到的知识

底子科普:

个人建议先把rotion的库导入进来再操纵,详细需要导入的库有,helper库包含了modbus通讯封装好的模块,而mvvm则可以用来做设计mvvm模块,你可以使用里面封装好的实现方法,用起来特殊简单更轻易实现其中的操纵,但是我担心那天被卡脖子了啊啊啊,要是我罗工把库下架了那不是死翘翘;
下面就是使用rotion的库把数据交互那块给发送到VMwindows这个cs文件去了,就不在这个视图底下的隐藏文件处理数据交互模块了,是不是很清新啊!
https://i-blog.csdnimg.cn/direct/742580faacf34555b5d3554a47389023.png
这下面的东西非rotion库中的,
https://i-blog.csdnimg.cn/direct/4ae8610d224342a7a87997890c48ac0e.png
这串才是,看注释,你get到了吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LS.WPF.MVVM;
using LS.WPF.MVVM.Command;
using System.Threading.Tasks;
using WpfApp2;

namespace WpfApp2
{
    publicclass wpf: BaseViewModel//继承BaseViewModel,这个接口是rotion的库里的,
    {
      public wpf(MainWindow win) : base(win) { } //构造函数
    }
}
再来科普一下构造函数:
public class Rectangle {
    private int width;
    private int height;

    // 默认构造函数
    public Rectangle() {
      this.width = 1;
      this.height = 1;
    }

    // 带参数的构造函数
    public Rectangle(int width, int height) {
      this.width = width;
      this.height = height;
    }

    public int getArea() {
      return width * height;
    }
}

// 使用构造函数
public class Main {
    public static void main(String[] args) {
      Rectangle rect1 = new Rectangle(); // 使用默认构造函数
      Rectangle rect2 = new Rectangle(5, 10); // 使用带参数的构造函数

      System.out.println("Area of rect1: " + rect1.getArea());//输出1
      System.out.println("Area of rect2: " + rect2.getArea());//输出50
    }
}
下面解释一下,这个cs文件,假设我们现在创建一个cs文件,这个文件用来处理某个页面转发过来的数据,那么刚被创建的时间它长下面这个样子,你大概会觉得欸,怎么长这个鸟样,有点不大熟悉,这个时间你就把他当作我们创建的一个类就行了,表面的namespace不用管滴,其实上他就是一个类嘛,还记得类是怎么界说的吗?想一想?当时我们为什么会用到类呢?不就是觉得主方法里要实现的方法许多,不简洁,于是我们新建的类来实现这些方法,而在我们的主类里则需要new一下这个类,那么我们就可以掉用我们创建的这个类里面的各种方法了,因此这里我们要写的什么七零八落的处理方法,都需要写在这个类的内部,还记得一个类长什么样吗?看下面复习一下,你看在这个里我们可以界说字段的get和set方法,还有界说一些void方法大概返回的int什么之类的诸如此类的一些列方法,这里再讲一下为什么需要get和set方法,不讲人话就是为了标准,讲人话呢?它其实很好理解,因为有些字段我想按照我的规矩来给它设置值,那么我就先把这个字段设为private私有的,然后我在通过设置公共的get和set方法来给外部访问,像我们现在这个例子emm,并没有过多约束,只是为了标准而已,这个时间就得再提一下全局变量和局部变量了。其实也很简单,可以看一下代码块,也列举了,在这c#其实是同等的,只是你太久没接触了给忘记了。
https://i-blog.csdnimg.cn/direct/3c3940148e964481b4b5f59e492d3e21.png
public class Person
{
    private int age;

    public int Age
    {
      get { return age; }
      set
      {
            if (value < 0)
            {
                throw new ArgumentException("Age cannot be negative");
            }
            age = value;
      }
    }
   
    private string name;

    public string Name
    {
      get { return name; }
      set
      {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentException("Name cannot be empty or whitespace");
            }
            name = value;
      }
    }

    public int test(int a,int b){
      
      return a+b;
    }
}

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: c#实际开发长到的知识