【C#】课堂知识点#3

1、讲解了实验1中,利用Char.is***来进行判断字符类型。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace MyProject
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             
13             Console.WriteLine("请输入一个字符");
14             int x = Console.Read();
15             char ch = (char)x;
16             string res = "";
17             if ( char.IsDigit(ch) )
18             {
19                 res = "数字";
20             }
21             else if ( char.IsLower(ch) )
22             {
23                 res = "小写字母";
24             }
25             else if ( char.IsUpper(ch) )
26             {
27                 res = "大写字母";
28             }
29             else
30             {
31                 res = "其他字符";
32             }
33             Console.WriteLine("{0} 是 {1}",ch,res);
34         }
35     }
36 }
判断字符类型

2、讲解了实验2中,利用Split分割字符数组,以及统计每一个字母的输出的次数。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace MyProject
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             string s = "If you ask me how much i love you!";
13             //利用了匿名数组,省略了实例化的过程.
14             string []str = s.Split(new char[] {',','.',' ',' '},StringSplitOptions.RemoveEmptyEntries);
15 
16             //获取数组的长度
17             int len = str.Length;
18             int Len = str.GetLength(0);
19 
20             //两次转换,一次转化成大写字母,第二次转化成Char类型的数组.
21             char [] arr = s.ToUpper().ToCharArray();
22             int[] Cnt = new int[26];
23             foreach (var item in arr )
24             {
25                 if( char.IsUpper(item))
26                     Cnt[item-'A']++;
27             }
28             
29             //输出对应字母的出现的次数
30             for (int i = 0 ; i < 26; i++  )
31             {
32                 Console.WriteLine("{0} - {1}",(char)(i+'A') , Cnt[i]);
33             }
34 
35         }
36     }
37 }
View Code

3、讲解了C#中类的“属性”,“构造函数”,“override-Tostring”

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace MyProject
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Shape s = new Shape();
13             Console.WriteLine(s);
14         }
15     }
16     //通常写在调试类的下面
17     //这个类为: internal (内部的),只能在当前项目程序能使用.
18     class Shape
19     {
20         //类里面的 数据成员
21         //类型为:private,原因对成员进行封装,
22         //              不允许直接在类外部直接访问或修改.
23         private int weight;
24         private int height;
25 
26         //传统的Java: get/set方法
27         public void setWeight( int weight)
28         {
29             this.weight = weight;
30         }
31         public int getWeight()
32         {
33             return weight;
34         }
35 
36         //C#特有的"属性"
37         //注意: 1:属性一定是首字母大写!大写!大写!
38         //     2:只是对字段进行封装的方法
39 
40         public int Weight
41         {
42             get { return weight; }
43             set { weight = value; }
44         }
45 
46         //匿名属性
47         public int NULL_P
48         {
49             get { return NULL_P; }
50             set { NULL_P = value; }
51         }
52 
53         //右键重构 数据成员的代码
54         public int Height { get => height; set => height = value; }
55 
56         //有参数的构造函数
57         public Shape ( int width , int height )
58         {
59             this.weight = width ;
60             this.height = height ;
61         }
62         //无参数的构造函数
63         public Shape() : this(3, 4) { }
64 
65         //返回面积
66         public int Area()
67         {
68             //这里直接 就是 数据成员的weight,height.
69             //这里和加了this. 是一样的.
70             return weight * height;
71         }
72 
73         public override string ToString()
74         {
75             //1、利用类ToString方法把数字转化为String
76             //return (weight * height).ToString();
77 
78             //2、默认为 返回 "命名空间+类名" 的结构
79             //return base.ToString();
80 
81             //3、利用string.Format()进行格式化
82             return String.Format("Area = {0}",weight*height);
83         }
84     }
85 }
类的基本操作
原文地址:https://www.cnblogs.com/Osea/p/11599362.html