首页 / 资讯中心 / 文章详情

C#入门学习周记2

C#入门学习周记2 ★ FEATURED ARTICLE
本周学习了很多方法类等等方法方法概念: 一组代码的集合目的: 方便多次调用, 实现代码重用定义c# 里普通函数方法必须写在类或者结构体、记录 record里面不能单独写在类外面。访问修饰符 [其他修饰符] 返回值类型 方法名([参数列表]){ 方法体 }例子定义了一个静态方法返回两个整数之和internal class Program { static void Main(string[] args) { int sum Add(10, 10); Console.WriteLine(sum);//输出20 Console.WriteLine(Add(1,2));//输出3 } static int Add(int a,int b) { return a b; } }静态方法与非静态方法静态方法 staticstatic int Add()属于类本身不用 new 对象直接Add()调用实例方法不带 static属于实例对象必须new出类的对象才能调用。例子定义了一个非静态方法Sub在Main函数中调用发生了错误。这是因为static的修饰的函数只能调用静态方法需要实例对象调用方法才行。internal class Program { static void Main(string[] args) { int sum Add(10, 10); Console.WriteLine(sum); Console.WriteLine(Add(1,2)); Console.WriteLine(Sub(7,3));//这里报错 } static int Add(int a,int b) { return a b; } public int Sub(int a, int b) { return a - b; } }这样使用就行internal class Program { static void Main(string[] args) { Program program new Program(); //实例化一个对象 int sum Add(10, 10); Console.WriteLine(sum); Console.WriteLine(Add(1,2)); Console.WriteLine(program.Sub(7,3));//这样就行结果为4 } static int Add(int a,int b) { return a b; } public int Sub(int a, int b) { return a - b; } }当为静态方法时可以使用类名加.来调用静态方法不可以使用对象调用非静态方法还是得使用对象调用class Calculate { public static int Add(int a,int b) { return a b; } public int Sub(int a,int b) { return a - b; } } internal class Program { static void Main(string[] args) { Calculate calculate new Calculate(); //实例化一个对象 int sum Calculate.Add(10, 10); Console.WriteLine(sum); Console.WriteLine(Calculate.Add(1,2)); Console.WriteLine(calculate.Sub(7,3));//这样就行结果为4 Console.WriteLine(calculate.Add(44,2));//这样报错 Console.WriteLine(Calculate.Sub(7, 3));//这样也不行 } }总结就是静态函数可以使用类名.方法名在外边调用在内部调用则可以直接调用非静态方法可以在内部非静态函数调用要做静态函数调用得声明实例一个对象调用。类类就是一个模板、图纸用来描述一类事物有哪些特征、能做什么行为。定义class 类名 { // 属性/字段特征 // 方法行为 }例子class Student//定义一个学生类 { private int age;//字段 public int Age//属性 { get { return age; } set { age value; } } private string name;//字段 public string Name//属性 { get{ return name; } set{ name value; } } }构造函数创建对象时自动调用类中默认会生成一个无参构造函数using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 学习回顾 { class Student//定义一个学生类 { private int age;//字段 public int Age//属性 { get { return age; } set { age value; } } private string name;//字段 public string Name//属性 { get{ return name; } set{ name value; } } } internal class Program { static void Main(string[] args) { Student student new Student();//这里类会生成一个无参数的构造函数初始化实例 } } }也可以自己创建一个构造函数系统就只会调用自己写的构造函数class Student//定义一个学生类 { private int age;//字段 public int Age//属性 { get { return age; } set { age value; } } private string name;//字段 public string Name//属性 { get{ return name; } set{ name value; } } public Student(int age,string name) { this.age age; this.name name; } } internal class Program { static void Main(string[] args) { Student student new Student();//这里类会报错因为只会调用自己创建的构造函数 Student student1 new Student(12, 小米);//这样才行 } }访问修饰符public公开外面可以访问protected保护的只能类内部访问外面访问不到private私有只能类内部访问外面访问不到例子class Student { private string _id; // 私有字段外部不能直接读取修改 protected int age;//外部不能直接读取修改 public string Name { get; set; } //公开属性 }类的静态成员可以为静态变量静态方法静态方法上面讲了这里讲静态变量using System; namespace StaticVarApplication { class StaticVar { public static int num; public void count() { num; } public int getNum() { return num; } } class StaticTester { static void Main(string[] args) { StaticVar s1 new StaticVar(); StaticVar s2 new StaticVar(); s1.count(); s1.count(); s1.count(); s2.count(); s2.count(); s2.count(); Console.WriteLine(s1 的变量 num {0}, s1.getNum()); Console.WriteLine(s2 的变量 num {0}, s2.getNum()); Console.ReadKey(); } } }s1 的变量 num 6 s2 的变量 num 6我们可以使用static关键字把类成员定义为静态的。当我们声明一个类成员为静态时意味着无论有多少个类的对象被创建只会有一个该静态成员的副本。总之是静态成员是共有的。方法的重写与重载方法重载: 在同一个类, 方法名一样, 参数列表不一样的一组方法,称为方法重载重载overload同一个类里面同名、不同参数和继承没关系方法重写: 子类对父类某个方法实现不满意, 子类重写父类这个方法的实现重写override不能改方法签名签名必须完全一致方法签名方法名 参数列表参数个数、类型、顺序不含返回值、参数名特征:1. 父子类2. 方法名,参数列表,返回值都一样3. 方法体实现不一样C#的重写有两种方式: virtual override: 虚方法: 父类方法有方法实现的, 子类可以重写,也可以不重写class Calculate { public virtual int Add(int a,int b) { return a b; } } class AddNumber:Calculate { public override int Add(int a, int b) { return a-b; } } internal class Program { static void Main(string[] args) { AddNumber addNumber new AddNumber();//结果-1 Console.WriteLine(addNumber.Add(1,2)); } }abstract override: 抽象方法, 父类这个抽象方法没有方法体, 子类必须重写这个方法abstract class Calculate { public abstract int Add(int a, int b); } class AddNumber:Calculate { public override int Add(int a, int b) { return a-b; } } internal class Program { static void Main(string[] args) { AddNumber addNumber new AddNumber();//结果-1 Console.WriteLine(addNumber.Add(1,2)); } }异常 Exception异常: 程序在运行时出现的错误, 这个程序可以编译,没有语法错误编译错误: 代码有语法错误,编译不通过try-catchtry{ //试一试 //可能产生异常的代码 }catch(异常类型1 异常名){ //捕获 //处理产生异常类型1的 }catch(异常类型2 异常名){ //捕获 //处理产生异常类型2的 }升级版本try{ //试一试 //可能产生异常的代码 //出现异常代码 //之后代码 }catch(异常类型1 异常名){ //捕获 //处理产生异常类型1的 }catch(异常类型2 异常名){ //捕获 //处理产生异常类型2的 } ... finally{ } //不管是否产生异常,都会执行throw 抛出 语法: throw 异常对象; 可以作为一个返回值public static int Divide(int num1,int num2) { 返回 if (num2 0) { } //使用throw 抛出异常 throw new DivideByZeroException(除数为0); //throw: 结束方法, 异常作为结果 return num1 / num2; }字典 Dictionary存储的key/value对, 存储的key与value的映射关系, key唯一, value可以重复弓 ----- 23张----- 1021key,value数据类型可以任意的泛型字典: Dictionary 推荐非泛型字典: HashTable构造方法属性例子常用方法例子//方法 //Add() 添加 dict2.Add(m,21); //key不存在, 添加 //dict2.Add(a, 33); //key存在: 抛异常 Console.WriteLine(new string(,16)); foreach (char k in dict2.Keys) { Console.WriteLine($key:{k},value:{dict2[k]}); } //删除 //根据key删除对应键值对 Remove(key) bool flag dict2.Remove(z); Console.WriteLine($删除key为z的键值对是否成功:{flag}); Console.WriteLine(new string(, 16)); foreach (char k in dict2.Keys) { Console.WriteLine($key:{k},value:{dict2[k]}); } //清空: Clear() //dict2.Clear(); //Console.WriteLine($字典的元素个数:{dict2.Count}); //查询: //根据key获取value //第一种方式: 索引器 字典[key] 如果key存在,返回value, 否则抛异常 Console.WriteLine($key为a的value:{dict2[a]}); //Console.WriteLine($key为z的value:{dict2[z]}); //第二种方式: public bool TryGetValue(TKey key, out TValue value) 如果key存在,返 回value, 否则不抛异常 int val; bool rs dict2.TryGetValue(a,out val); Console.WriteLine($key为a的value:{val},rs:{rs}); rs dict2.TryGetValue(z, out val); Console.WriteLine($key为z的value:{val},rs:{rs}); //判断key是否存在 Console.WriteLine($dict2字典中是否存在key为a的元素:{dict2.ContainsKey(a)}); Console.WriteLine($dict2字典中是否存在key为z的元素:{dict2.ContainsKey(z)}); //判断value是否存在 Console.WriteLine($dict2字典中是否存在value为100的元素: {dict2.ContainsValue(100)}); Console.WriteLine($dict2字典中是否存在value为1的元素:{dict2.ContainsValue(1)}); //对字段排序 升序排序OrderBy() 降序排序OrderByDescending() //对字典按照key的升序排序 var rs1 dict2.OrderBy(kvkv.Key); Console.WriteLine(new string(, 16)); foreach (var item in rs1) { Console.WriteLine($key:{item.Key},value:{item.Value}); } //对字典按照value的升序排序 //rs1 dict2.OrderBy(kvkv.Value); //对字典按照value的降序排序 rs1 dict2.OrderByDescending(kv kv.Value); Console.WriteLine(new string(, 16)); foreach (var item in rs1) { Console.WriteLine($key:{item.Key},value:{item.Value}); } //遍历字典: 第三种方式, 得到键值对,推荐, 直接遍历字典, 得到是KeyValuePair foreach (var kv in dict2) { Console.WriteLine($key:{kv.Key},value:{kv.Value}); }WinFormWindows窗体应用程序, 图形用户界面(GUI)类库,基于事件驱动整体这样工具箱在视图下面工具箱里面有很多控件右边为属性窗口可以修改控件的属性添加事件事件WInForm布局方式:第一种方式: 使用Panel Dock属性(Top,Left,Right,Botton,Fill,None(默认值))注意: 一个容器中有多个控件, 设置不同Dock的值, 设置Dock为Fill的控件最后添加,最后设置设置panel位置设置背景颜色来区分运行看看第二种方式: 表格布局 TableLayoutPanel 使用最多跨列:ColumnSpann跨行: RowSpan n第三种布局器: FlowLayoutPanel: 流式布局器, 按照水平, 垂直排列左到右上到下右到左下到上常用控件文本框Label: 标签, 文本说明RadioButton: 单选按钮CheckBox: 复选框, 属性基本一样, 多选ComboBox: 下拉框添加成员运行看看属性:Items: 添加选项也可以添加成员DateTimePicter: 日期时间控件Timer 定时器每隔多久,执行一次方法:Start(): 开启定时器Stop(): 停止定时器DataGridView最重要的数据展示控件, 支持数据绑定, 以表格形式展示数据使用步骤:1. 设置表格的表头(列头定义), 包含一些属性设置列头的设置 第一种方式第二种方式: 属性面板-- Columns2. 绑定数据第一种方式: 通过手动添加row 了解 通过 DataGridView 的Rows属性(集合属性, 只 读)第二种方式: 数据绑定重要 实际开发使用 通过 DataGridView 的 DataSource属性绑定List集合绑定DataTable 学习绑定 BindingSources先创建一个类存储数据public class Student { private int age; public int Age { get { return age; } set { age value; } } private string name; public string Name { get { return name; } set { name value; } } private DateTime birth; public DateTime Birth { get { return birth; } set { birth value; } } private int id; public int Id { get { return id; } set { id value; } } public Student() { } public Student(int id,int age,string name,DateTime dateTime) { this.id id; this.age age; this.birth dateTime; this.name name; } }添加列设置列添加成员students.Add(new Student(1, 12, 小明, new DateTime(2001, 9, 10))); students.Add(new Student(2, 32, 大明, new DateTime(2002, 7, 10))); students.Add(new Student(3, 42, 张三, new DateTime(2003, 6, 10))); students.Add(new Student(4, 22, 王五, new DateTime(2004, 5, 10))); students.Add(new Student(5, 42, 李四明, new DateTime(2005, 4, 10))); students.Add(new Student(6, 52, 麻子, new DateTime(2001, 3, 10)));绑定数据运行看看事件每个控件都有一个默认事件如果误操作, 双击,生成默认事件, 不需要事件, 怎么删除?直接删除事件方法, UI设计器报错正确做法: 1. 找到控件的事件属性窗口, 把对应的事件绑定的方法,清除2. 回到窗体.cs代码中,把对应的方法删除所选内容
阅读完成 · 觉得有帮助?
咨询建站