From 0d0ba2a6c4aa0228209f2b99df7679831a98d1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?36=E5=88=98=E5=80=A9=E5=80=A9?= <2595352832@qq.com> Date: Sat, 1 Apr 2023 14:52:45 +0800 Subject: [PATCH 01/14] =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=8F=B0=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...23\345\207\272\345\206\205\345\256\271.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 "36\345\210\230\345\200\251\345\200\251/1/\346\216\247\345\210\266\345\217\260\350\276\223\345\207\272\345\206\205\345\256\271.md" diff --git "a/36\345\210\230\345\200\251\345\200\251/1/\346\216\247\345\210\266\345\217\260\350\276\223\345\207\272\345\206\205\345\256\271.md" "b/36\345\210\230\345\200\251\345\200\251/1/\346\216\247\345\210\266\345\217\260\350\276\223\345\207\272\345\206\205\345\256\271.md" new file mode 100644 index 0000000..9430aa7 --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/1/\346\216\247\345\210\266\345\217\260\350\276\223\345\207\272\345\206\205\345\256\271.md" @@ -0,0 +1,22 @@ +```c# +namespace 作业 //命名空间的声明 +{ + internal class Program //定义类 + + + { + static void Main(string[] args) //主方法,程序的入口 + + { + //向控制台界面换行输出内容 + Console.WriteLine("闽西职业技术学院"); + Console.WriteLine("软件工程学院"); + //向控制台界面不换行输出内容 + Console.Write("22级软件技术7班"); + Console.Write("刘倩倩"); + + } + } +} +``` + -- Gitee From ca926df6a253e734fa1af08f4fc655910b63153f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?36=E5=88=98=E5=80=A9=E5=80=A9?= <2595352832@qq.com> Date: Fri, 7 Apr 2023 13:29:02 +0800 Subject: [PATCH 02/14] =?UTF-8?q?=E5=9F=BA=E7=A1=80=E8=AF=AD=E6=B3=95?= =?UTF-8?q?=E5=B7=A9=E5=9B=BA1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\346\263\225\345\267\251\345\233\2721.md" | 146 ++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 "36\345\210\230\345\200\251\345\200\251/01/\345\237\272\347\241\200\350\257\255\346\263\225\345\267\251\345\233\2721.md" diff --git "a/36\345\210\230\345\200\251\345\200\251/01/\345\237\272\347\241\200\350\257\255\346\263\225\345\267\251\345\233\2721.md" "b/36\345\210\230\345\200\251\345\200\251/01/\345\237\272\347\241\200\350\257\255\346\263\225\345\267\251\345\233\2721.md" new file mode 100644 index 0000000..c6aeb51 --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/01/\345\237\272\347\241\200\350\257\255\346\263\225\345\267\251\345\233\2721.md" @@ -0,0 +1,146 @@ +```C# +namespace 作业 +{ + + //控制台输出5行字符串类型常量值 + internal class Homework1 + { + static void Main(string[] args) + { + Console.WriteLine("我的身份为:"); + Console.WriteLine("闽西职业技术学院"); + Console.WriteLine("软件工程"); + Console.Write("22级软件技术7班"); + Console.Write(" 刘倩倩"); + } + } + + /* + //控制台输出所有布尔类型常量值 + internal class Homework3 + { + static void Main(string[] args) + { + Console.WriteLine("true"); + Console.WriteLine("false"); + } + } + + //1. 定义2个 byte类型变量,分别赋byte类型范围内最大值和最小值,并输出在控制台. + //2. 定义2个 short类型变量,分别赋short类型范围内的值,并输出在控制台. + //3. 定义2个 int类型变量,分别赋int类型范围内的值,并输出在控制台. + //4. 定义2个 long类型变量,分别赋超过int类型范围的值,并输出在控制台. + internal class Homework4 + { + static void Main(string[] args) + { + byte max = 255; + byte mix = 0; + Console.WriteLine("byte的取值范围:"+mix+"-"+max); + + short mix1 = -32768; + short max1 = 32767; + Console.WriteLine("short的取值范围:" + mix1 + "-" + max1); + + int mix2 =-2147483648; + int max2 =2147483647; + Console.WriteLine("int的取值范围:" + mix2 + "-" + max2); + + long num1 = -2147483649; + long num2 = 2147483648; + Console.WriteLine("long的值:" + num1 + "," + num2); + + } + } + + //1. 定义2个 float类型变量,分别赋值,并输出在控制台. + //2. 定义2个 double类型变量,分别赋值,并输出在控制台. + internal class Homework5 + { + static void Main(string[] args) + { + float num1=-3.14f; + float num2 = 3.14f; + Console.WriteLine(num1); + Console.WriteLine(num2); + + double num3 = -3.4; + double num4 = 3.4; + Console.WriteLine(num3); + Console.WriteLine(num4); + + } + } + + //1. 定义两个整数变量a,b并赋值 + //2. 控制台输出变量a,b互换前的值 + //3. 定义一个第三方变量temp + //4. 利用第三方变量temp使a,b的值互换 + //5. 控制台输出变量a,b互换后的值 + internal class Homework6 + { + static void Main(string[] args) + { + int num1 = 10; + int num2 = 20; + Console.WriteLine("互换前的值:a="+num1+",b="+num2); + + int temp = num2; + num2 = num1; + num1 = temp; + Console.WriteLine("互换后的值:a=" + num1 + ",b=" + num2); + } + } + + //1. 定义2个int类型变量x、y,x赋值为100,y赋值为200 + //2. 定义新变量add,保存变量x,y的和并打印到控制台 + //3. 定义新变量sub,保存变量x,y的差并打印到控制台 + //4. 定义新变量mul,保存变量x,y的积并打印到控制台 + //5. 定义新变量div,保存变量x,y的商并打印到控制台 + internal class Homework7 + { + static void Main(string[] args) + { + int x = 100; + int y = 200; + int add = x + y; + Console.WriteLine("x,y的和为:"+add); + + int sub = x - y; + Console.WriteLine("x,y的差为:" + sub); + + int mul = x * y; + Console.WriteLine("x,y的积为:" + mul); + + int div = x / y; + Console.WriteLine("x,y的商为:" + div); + } + } + + //1. 定义2个double类型变量x、y,x赋值为100.8,y赋值为20.6 + //2. 定义新变量add,保存变量x,y的和并打印到控制台 + //3. 定义新变量sub,保存变量x,y的差并打印到控制台 + //4. 定义新变量mul,保存变量x,y的积并打印到控制台 + //5. 定义新变量div,保存变量x,y的商并打印到控制台 + internal class Homework8 + { + static void Main(string[] args) + { + double x = 100.8; + double y = 20.6; + double add = x + y; + Console.WriteLine("x,y的和为:" + add); + + double sub = x - y; + Console.WriteLine("x,y的差为:" + sub); + + double mul = x * y; + Console.WriteLine("x,y的积为:" + mul); + + double div = x / y; + Console.WriteLine("x,y的商为:" + div); + } + }*/ +} +``` + -- Gitee From dd8569228711659f22fdca966a909924710fdb1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?36=E5=88=98=E5=80=A9=E5=80=A9?= <2595352832@qq.com> Date: Fri, 14 Apr 2023 19:04:10 +0800 Subject: [PATCH 03/14] =?UTF-8?q?=E5=9F=BA=E7=A1=80=E8=AF=AD=E6=B3=95?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\351\242\230\344\275\234\344\270\232.md" | 451 ++++++++++++++++++ 1 file changed, 451 insertions(+) create mode 100644 "36\345\210\230\345\200\251\345\200\251/03/15\351\242\230\344\275\234\344\270\232.md" diff --git "a/36\345\210\230\345\200\251\345\200\251/03/15\351\242\230\344\275\234\344\270\232.md" "b/36\345\210\230\345\200\251\345\200\251/03/15\351\242\230\344\275\234\344\270\232.md" new file mode 100644 index 0000000..7c6e6d0 --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/03/15\351\242\230\344\275\234\344\270\232.md" @@ -0,0 +1,451 @@ +```c# + //1. + // 求圆的面积 + // 要求用户输入半径的值,打印出以此值为半径的圆的面积 + // 求圆的面积的公式:πr²。 + // 圆周率π定义成常量取3.14。 + // r由用户输入并存入一个变量中此变量用double比较合适,因为用户可能会输入小数。 + + + //输出要求 + Console.WriteLine("输入半径的值:"); + //接受用户填写的变量 + double r = double.Parse(Console.ReadLine()); + + const double π = 3.14; //定义常量 + double o = r * r * π; //圆的面积公式 + + //输出结果 + Console.WriteLine("半径为" + r + "的圆面积为:" + o); + + /* + + //2. + // 编写一个程序,请用户输入一个四位整数, + // 将用户输入的四位数的千位、百位、十位和个位分别显示出来 + // 如5632,则显示“用户输入的千位为5,百位为6,十位为3,个位为2” + + Console.WriteLine("请用户输入一个四位整数的数字:"); + int num = int.Parse(Console.ReadLine()); + + int g = num % 10; + int s = num % 100 / 10; + int b = num % 1000 / 100; + int q = num / 1000; + + Console.WriteLine("用户输入的千位为" + q + "百位为" + b + "十位为" + s + "个位为" + g); + + + //3. + // 用户输入三个数,找出最大的数,打印输出。 + + + Console.WriteLine("请用户输入第一个数字:"); + int num1 = int.Parse(Console.ReadLine()); + + Console.WriteLine("请用户输入第二个数字:"); + int num2 = int.Parse(Console.ReadLine()); + + Console.WriteLine("请用户输入第三个数字:"); + int num3 = int.Parse(Console.ReadLine()); + + if (num1 > num2 && num1 > num3) + { + Console.WriteLine("用户输入三个数中最大的数为:" + num1); + } + else if (num2 > num1 && num2 > num3) + { + Console.WriteLine("用户输入三个数中最大的数为:" + num2); + } + else if (num3 > num1 && num3 > num2) + { + Console.WriteLine("用户输入三个数中最大的数为:" + num3); + } + + + + //4. + // 接受用户输入一个数n,求这个数的阶乘;5! = 5*4*3*2*1; + + + Console.WriteLine("请用户输入一个数字:"); + int avg = int.Parse(Console.ReadLine()); + + int sum = 1; + for (int i = 1; i <= avg; i++) + { + sum = sum * i; + + } + Console.WriteLine(sum); + + + //5. + // 接受用户输入的一个数n,求n到1所有数的阶乘和;n!+(n-1!)+(n-2)!+……+1! + + Console.WriteLine("请用户输入一个数字:"); + int avg1 = int.Parse(Console.ReadLine()); + int res = 0; + int sum1 = 1; + for (int ii = 1; ii <= avg1; ii++) + { + sum1 = sum1 * ii; + res += sum1; + } + Console.WriteLine(res); + + + //6. + // 根据用户输入的菱形边长,打印菱形;如边长为3,行数为5; + + Console.WriteLine("请用户输入菱形的行数为:"); + int ab = Convert.ToInt32(Console.ReadLine()); + + //上 + for (int l = 1; l <= (ab + 1) / 2; l++) + { + for (int j = (ab - 1) / 2; j >= l; j--) + { + Console.Write(" "); + } + for (int k = 1; k <= l * 2 - 1; k++) + { + Console.Write("*"); + } + Console.WriteLine(); + } + + //下 + for (int l = (ab - 1) / 2; l >= 1; l--) + { + for (int j = l - 1; j < (ab - 1) / 2; j++) + { + Console.Write(" "); + } + for (int k = 1; k <= l * 2 - 1; k++) + { + Console.Write("*"); + } + Console.WriteLine(); + } + + + + //7. + //用循环打印九九乘法表(用二维数组保存字符串后再打印) + int k = 0; + Console.WriteLine("直角在左角下"); + for (int i = 1; i < 10; i++) + { + for (int j = 1; j <= i; j++) + { + Console.Write("{0}*{1}={2}\t", j, i, i * j); + } + Console.WriteLine(); + } + Console.WriteLine();//换行 + + Console.WriteLine("直角在左角上"); + for (int i = 9; i > 0; i--) + { + for (int j = 1; j <= i; j++) + { + Console.Write("{0}*{1}={2}\t", j, i, i * j); + } + Console.WriteLine(); + } + Console.WriteLine();//换行 + + Console.WriteLine("直角在右角上"); + for (int i = 9; i > 0; i--) + { + for (int j = i; j > 0; j--) + { + Console.Write("{0}={1}*{2}\t", i * j, i, j); + } + Console.WriteLine(); + for (k = 10 - i; k > 0; k--) + { + Console.Write("\t"); + } + } + Console.WriteLine();//换行 + + Console.WriteLine("直角在右角下"); + for (int i = 1; i < 10; i++) + { + for (k = 9 - i; k > 0; k--) + { + Console.Write("\t"); + } + for (int j = i; j > 0; j--) + { + Console.Write("{0}={1}*{2}\t", i * j, j, i); + } + Console.WriteLine(); + } + + Console.WriteLine(); + Console.ReadLine(); + + + //8. + // 输入一行字符串,分别统计出其中英文字母、数字、空格的个数。 + string str = ""; + int zm = 0; + int num = 0; + int kg = 0; + + Console.WriteLine("输入一行字符串"); + str =Console.ReadLine(); + + char[] c = str.ToCharArray();//把字符串转换成字符数组 + foreach (char i in c) + { + if (i >= 'a' && i <= 'z' || i >= 'A' && i <= 'Z') + zm++; + + else if (i >= '0' && i <= '9') + num++; + + else if (i == ' ') + kg++; + + } + + Console.WriteLine("字母有" + zm + "个,空格有" + kg + "个,数字有" + num + "个."); + Console.ReadLine(); + + + + //9. + // 在 Main 方法中创建一个 double 类型的数组, + //并在该数组中存入 5 名学生的考试成绩, + // 计算总成绩和平均成绩。(要求使用foreach语句实现该功能) + + double[] sore = new double[5]; + double sum = 0; + double avg = 0; + + for(int i=0;i<5;i++) + { + Console.WriteLine($"请输入第{i+1}学生的成绩"); + sore[i]=Convert.ToInt32(Console.ReadLine()); + } + + foreach(double d in sore) + { + sum += d; + } + avg = sum / sore.Length; + + Console.WriteLine(); + Console.WriteLine("5 名学生的总成绩:" + sum); + Console.WriteLine("5 名学生的平均成绩;" + avg); + + + //10. + // 定义一个方法,实现一维数组的排序功能,从大到小排序。(不要用Array类的方法) + + const int N = 10; + int[] num = new int[N]; + Console.WriteLine("请输入10个数字:"); + + try + { + for(int i = 0; i < num.Length; i++) + { + num[i]=Convert.ToInt32(Console.ReadLine()); + } + + //------------------------------------------------------------- + // 冒泡 + // 小到大 + for(int i=0;i Date: Fri, 5 May 2023 13:52:37 +0800 Subject: [PATCH 04/14] =?UTF-8?q?=E6=9E=84=E9=80=A0=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\351\200\240\346\226\271\346\263\225.md" | 192 ++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 "36\345\210\230\345\200\251\345\200\251/04/\346\236\204\351\200\240\346\226\271\346\263\225.md" diff --git "a/36\345\210\230\345\200\251\345\200\251/04/\346\236\204\351\200\240\346\226\271\346\263\225.md" "b/36\345\210\230\345\200\251\345\200\251/04/\346\236\204\351\200\240\346\226\271\346\263\225.md" new file mode 100644 index 0000000..e68b341 --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/04/\346\236\204\351\200\240\346\226\271\346\263\225.md" @@ -0,0 +1,192 @@ +```c# + // 一.定义一个用户类 Stu + internal class Stu + { + + // 1.定义字段存放用户的账号、用户名和密码; + private int id; + private string name; + private string password; + + //定义字段属性 + public int Id { get => id; set => id = value; } + public string Name { get => name; set => name = value; } + public string Password + { + get => password; + set + { + //判断外部赋值给密码的中间变量value长度是否为6~10 + if (value.Length >= 6 && value.Length <= 10) + { + password = value; + } + else + { + password = "123456"; + } + } + } + + // 2.在用户类中定义一个方法输出当前用户对象的账号、用户名和密码的信息; + public void Print() + { + Console.WriteLine($"账号:{id}"); + Console.WriteLine($"用户名:{name}"); + Console.WriteLine($"密码:{password}"); + } + + // 3.在用户类中定义一个 登录身份辨认的方法:规则为用户名为“admin”和密码为“123456”时返回“管理员”,否则返回“普通用户”; + public string Identity() + { + if (name == "admin" && password == "123456") + { + return "管理员";//将数据返回到方法调用处,并且流程控制权页返回 + + } + else + { + return "普通用户"; + + } + + } + } +``` + +```c# + // 二.定义一个学生类 Student + internal class Student + { + // 1.定义字段存放学生的学号、姓名、性别、年龄、专业信息; + int id; + string name; + char sex; + int age; + string zy; + + // 2.对年龄字段进行赋值的安全性设置,如果是非法值(小于0或者大于128岁),该年龄值为0; + public int Id { get => id; set => id = value; } + public string Name { get => name; set => name = value; } + public char Sex { get => sex; set => sex = value; } + public int Age + { + get => age; + set + { + if (age < 0 || age > 128) { age = 0; } + else { age = value; } + } + } + public string Zy { get => zy; set => zy = value; } + + + // 3.在学生类中定义一个方法输出学生信息。 + public void Print() + { + Console.WriteLine($"学号:{id}"); + Console.WriteLine($"姓名:{name}"); + Console.WriteLine($"性别:{sex}"); + Console.WriteLine($"年龄:{age}"); + Console.WriteLine($"专业:{zy}"); + } + + } +``` + +```c# + // 三.定义一个图书类 + internal class Book + { + // 1.定义字段存放图书的编号、书名、价格、出版社、作者信息; + int id; + string name; + double jg; + string cbs; + string zz; + + public int Id { get => id; set => id = value; } + public string Name { get => name; set => name = value; } + + // 2.对价格进行赋值限制,小于0价格,赋值为0 + public double Jg + { + get => jg; set + { + if (jg < 0) { jg = 0; } + else { jg = value; } + } + } + public string Cbs { get => cbs; set => cbs = value; } + public string Zz { get => zz; set => zz = value; } + + + // 3.在图书类中定义一个方法输出图书信息 + public void Print() + { + Console.WriteLine($"编号:{id}"); + Console.WriteLine($"姓名:{name}"); + Console.WriteLine($"价格:{jg}"); + Console.WriteLine($"出版社:{cbs}"); + Console.WriteLine($"作者:{zz}"); + } + } +``` + +```c# + internal class Program + { + static void Main(string[] args) + { + /******************************* 一、用户类 Stu **********************************/ + + //4.在主方法中实例化用户类的对象 + Stu stu = new Stu(); + //然后对其账号、用户名和密码赋值 + stu.Id = 1; + stu.Name = "admin"; + stu.Password = "123456"; + + //5.在主方法中用实例化的对象调用方法 + stu.Print(); + //输出用户对象的信息和身份(管理员或者普通用户) + Console.WriteLine("你的身份是:" + stu.Identity()); + + Console.WriteLine(); + + Stu stu1 = new Stu(); + stu1.Id = 2; + stu1.Name = "liu"; + stu1.Password = "123456"; + stu1.Print(); + Console.WriteLine("你的身份是:" + stu1.Identity()); + + /******************************* 二、学生类 Student **********************************/ + + Console.WriteLine(); + // 4. 在主方法实例化学生对象,赋值并输出 + Student student = new Student(); + student.Id = 36; + student.Name = "liu"; + student.Sex = '女'; + student.Age = 18; + student.Zy = "软件技术"; + student.Print(); + + /******************************* 三、图书类 Book **********************************/ + + Console.WriteLine(); + //4.在主方法实例化对象,赋值并输出 编号、书名、价格、出版社、作者信息; + Book book = new Book(); + book.Id = 14; + book.Name = "恋爱的贡多拉"; + book.Jg =43.06; + book.Cbs = "现代出版社"; + book.Zz = "东野圭吾"; + book.Print(); + + + } + } +``` + -- Gitee From e55e1b255c51a75e2e0379b456dc9abea24f9717 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?36=E5=88=98=E5=80=A9=E5=80=A9?= <2595352832@qq.com> Date: Fri, 5 May 2023 13:54:10 +0800 Subject: [PATCH 05/14] =?UTF-8?q?=E9=87=8D=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../05/\351\207\215\350\275\275.md" | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 "36\345\210\230\345\200\251\345\200\251/05/\351\207\215\350\275\275.md" diff --git "a/36\345\210\230\345\200\251\345\200\251/05/\351\207\215\350\275\275.md" "b/36\345\210\230\345\200\251\345\200\251/05/\351\207\215\350\275\275.md" new file mode 100644 index 0000000..53912fb --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/05/\351\207\215\350\275\275.md" @@ -0,0 +1,78 @@ +```c# +internal class Program + { + static void Main(string[] args) + { + + //1 + Mianji m = new Mianji(); + double sy = Mianji.Num(3); + double sc = Mianji.Num(4, 5); + Console.WriteLine($"圆的面积为{sy};长方形的面积为:{sc}"); + //2 + SumUtils sum = new SumUtils(); + sum.Num(3, 4); + sum.Num(1.2, 2.5); + sum.Num("Hello,", "World!"); + sum.Num(10); + } + + } +``` + +```c# + internal class Mianji + { + + /*1. 定义一个计算图形面积的类,类中定义2个计算面积的方法(重载,方法名相同),分别计算圆面积和长方形面积两个方法。 + 提示:计算圆的面积传半径,计算长方形面积传长和宽。*/ + + //在被调用的非静态函数或变量前加 static 关键字,将其变成静态方法或变量。以被静态函数调用。 + + //圆的面积 + public static double Num(int r) + { + return r * 3.14 * r; + } + //长方形面积 + public static double Num(int a, int b) + { + return a * b; + } + } +``` + +```c# +/*2.创建一个名为计算工具类 SumUtils,在类中定义4个方法: + 计算两个整数相加、 + 两个小数相加、 + 两个字符串相加、 + 以及从 1 到指定整数的和的方法。 + 在 Main 方法中分别调用定义好的方法。 + 提示:根据题目要求,分别定义 3 个带两个参数的方法,以及一个带一个整型参数的方法, 四个方法名相同。*/ + internal class SumUtils + { + public void Num(int a, int b) + { + Console.WriteLine($"两个整数相加{a + b}"); + } + public void Num(double a, double b) + { + Console.WriteLine($"两个小数相加{a + b}"); + } + public void Num(string a, string b) + { + Console.WriteLine($"两个整数相加{a + b}"); + } + public void Num(int a) + { + int sum = 0; + for (int i = 1; i <= a; i++) + { + sum += i; + } + Console.WriteLine($"以及从 1 到指定整数的和{sum}"); + } + } +``` + -- Gitee From a2ba2eb47e59a6b7fc680ef705dee9f5e3875f79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?36=E5=88=98=E5=80=A9=E5=80=A9?= <2595352832@qq.com> Date: Fri, 5 May 2023 13:56:16 +0800 Subject: [PATCH 06/14] =?UTF-8?q?=E7=B1=BB=E5=92=8C=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\345\222\214\345\261\236\346\200\247.md" | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 "36\345\210\230\345\200\251\345\200\251/06/\347\261\273\345\222\214\345\261\236\346\200\247.md" diff --git "a/36\345\210\230\345\200\251\345\200\251/06/\347\261\273\345\222\214\345\261\236\346\200\247.md" "b/36\345\210\230\345\200\251\345\200\251/06/\347\261\273\345\222\214\345\261\236\346\200\247.md" new file mode 100644 index 0000000..ede18ee --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/06/\347\261\273\345\222\214\345\261\236\346\200\247.md" @@ -0,0 +1,156 @@ + 一、定义一个员工类 + 1.定义字段,存放用户的工号、姓名、性别、学历和部门信息; + 2.定义属性封装字段 + 3.定义2个构造函数: + 一个是无参构造函数,学历默认为专科; + 一个有参构造函数,根据参数对类的属性进行初始化。 + +```c# + internal class Stu + { + //1.定义字段 + public int id; + public string name; + public char sex; + public string xl; + public string bm; + + //2.定义属性 + public int Id { get => id; set => id = value; } + public string Name { get => name; set => name = value; } + public char Sex { get => sex; set => sex = value; } + public string Xl { get => xl; set => xl = value; } + public string Bm { get => bm; set => bm = value; } + + //3.构造函数 + //定义无参,并设置默认值 + public Stu() { xl = "专科"; } + + //定义有参 + public Stu(int id, string name, char sex, string xl, string bm) + { + Id = id; + Name = name; + Sex = sex; + Xl = xl; + Bm = bm; + + } + + //方法 + public void Print() + { + Console.WriteLine($"学号:{id}"); + Console.WriteLine($"姓名:{name}"); + Console.WriteLine($"性别:{sex}"); + Console.WriteLine($"学历:{xl}"); + Console.WriteLine($"部门:{bm}"); + + + } + + static void Main(string[] args) + { + // 一 + + //无参调用 + Stu stu = new Stu(); + stu.Id = 1; + stu.Name = "刘"; + stu.Sex = '女'; + //stu.Xl = "大专"; --默认值 + stu.Bm = "软工"; + stu.Print(); + + Console.WriteLine(); + + //有参调用 + Stu stu2 = new Stu(2, "韦", '男', "大专", "护理"); + stu2.Print(); + + Console.WriteLine(); + + Stu stu3 = new Stu(3, "李", '女', "二本", "教育"); + stu3.Print(); + } + } +``` + +二、为之前作业中的学生类、用户类和图书类添加2个构造方法 + 1.一个无参的构造方法 + 2.一个有参的构造方法,根据参数对类的属性进行初始化 + +```c# + + #region 用户类 无参,有参的构建 + + //无参 + public Stu() { } + + //有参 + public Stu(int id, string name, string password) + { + this.id = id; + this.name = name; + this.password = password; + } + + #endregion +``` + +```c# + #region 学生类 无参,有参的构建 + + //无参 + public Student() { } + + //有参 + public Student(int id,string name,char sex,int age,string zy) + { + this.id = id; + this.name = name; + this.sex = sex; + this.age = age; + this.zy = zy; + } + #endregion +``` + +```c# +#region 图书类 无参,有参的构建 + + //无参 + public Book() { } + + //有参 + public Book(int id,string name,double jg,string cbs,string zz) + { + this.id = id; + this.name = name; + this.jg = jg; + this.cbs = cbs; + this.zz = zz; + } + + #endregion +``` + +```c# + #region 有参 对类的属性进行初始化 + Console.WriteLine(""); + //1 + Console.WriteLine(" 用户类"); + Stu stu11=new Stu(13,"ai","123654"); + stu11.Print(); + Console.WriteLine("你的身份是:" + stu11.Identity()); + //2 + Console.WriteLine(" 学生类"); + Student student11=new Student(37,"lst",'女',19,"软件技术"); + student11.Print(); + //3 + Console.WriteLine(" 图书类"); + Book book11=new Book(2022,"闽大学生手册",9000,"学生工作处","闽西职业技术学院"); + book11.Print(); + #endregion +``` + -- Gitee From 2be73f51b1da24056ed377eb8daa51d7956a2b22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?36=E5=88=98=E5=80=A9=E5=80=A9?= <2595352832@qq.com> Date: Fri, 5 May 2023 13:58:47 +0800 Subject: [PATCH 07/14] =?UTF-8?q?=E9=9D=99=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...31\346\200\201\345\255\227\346\256\265.md" | 0 ...31\346\200\201\346\226\271\346\263\225.md" | 51 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 "36\345\210\230\345\200\251\345\200\251/07/7_\351\235\231\346\200\201\345\255\227\346\256\265.md" create mode 100644 "36\345\210\230\345\200\251\345\200\251/07/7_\351\235\231\346\200\201\346\226\271\346\263\225.md" diff --git "a/36\345\210\230\345\200\251\345\200\251/07/7_\351\235\231\346\200\201\345\255\227\346\256\265.md" "b/36\345\210\230\345\200\251\345\200\251/07/7_\351\235\231\346\200\201\345\255\227\346\256\265.md" new file mode 100644 index 0000000..e69de29 diff --git "a/36\345\210\230\345\200\251\345\200\251/07/7_\351\235\231\346\200\201\346\226\271\346\263\225.md" "b/36\345\210\230\345\200\251\345\200\251/07/7_\351\235\231\346\200\201\346\226\271\346\263\225.md" new file mode 100644 index 0000000..6a529a1 --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/07/7_\351\235\231\346\200\201\346\226\271\346\263\225.md" @@ -0,0 +1,51 @@ +```c# +static void Main(string[] args) + { + //1 + string str = " "; + Console.WriteLine(StringUtil.Two(str)); + //2 + int[] num = new int[10]; + Console.WriteLine(ArrayUtil.One(num)); + } +``` + +```c# +/*1、写一个工具类StringUtil,在里面定义一个静态方法,用来判断字符串是否为空。 +如果字符串是null,或者字符串是空"",或者字符串是N个空格" ",那这个方法返回true,否则返回false +然后在主类中(有Main方法的类)调用测试。*/ + internal class StringUtil + { + public static bool Two(string str) + { + char[] arrchar = str.ToCharArray(); + if (str == null || str == "") + { + return true; + } + foreach (char c in arrchar) + { + if (c != ' ') + { + return false; + } + } + return true; + } + } +``` + +```c# +/*2、写一个工具类ArrayUtil,在里面定义一个静态方法,用来判断数组是否为空。 +如果数组是null,或者数组长度为0,那此方法返回true,否则返回false +然后在主类中(有Main方法的类)调用测试。 +*/ + internal class ArrayUtil + { + public static bool One(int[] num) + { + if (num == null || num.Length == 0) return true; return false; + } + } +``` + -- Gitee From ead0bb970e0c972a7ac645d50090a92898c9247e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?36=E5=88=98=E5=80=A9=E5=80=A9?= <2595352832@qq.com> Date: Fri, 5 May 2023 14:52:17 +0800 Subject: [PATCH 08/14] =?UTF-8?q?=E9=9D=99=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../07/7_\345\255\227\346\256\265.md" | 58 +++++++++++++++++++ ...31\346\200\201\345\255\227\346\256\265.md" | 0 2 files changed, 58 insertions(+) create mode 100644 "36\345\210\230\345\200\251\345\200\251/07/7_\345\255\227\346\256\265.md" delete mode 100644 "36\345\210\230\345\200\251\345\200\251/07/7_\351\235\231\346\200\201\345\255\227\346\256\265.md" diff --git "a/36\345\210\230\345\200\251\345\200\251/07/7_\345\255\227\346\256\265.md" "b/36\345\210\230\345\200\251\345\200\251/07/7_\345\255\227\346\256\265.md" new file mode 100644 index 0000000..2221199 --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/07/7_\345\255\227\346\256\265.md" @@ -0,0 +1,58 @@ +```c# +//静态字段存储的值在所有实例化之间是共享的。 + /*作业: + 现在开班了,班里老师只有一个,叫唐僧。班里的五个人都知道老师叫唐僧(也就是说老师这个字段是共享的)。 + 但是某一天唐僧要去女儿国走丈母娘家了,课不能停啊。所以就请嫦娥姐姐过来带一段课。 + + 现在5个学生都知道老师换成嫦娥姐姐了,都很激动哈。但是她们是怎么都知道换老师的呢? + 还是这个静态字段的功劳,将老师姓名声明为静态字段,这个字段是共享的。所以该类的属性都能知道。 + + 请用代码写出上面的剧情。*/ + internal class Program + { + public static string techer = "唐僧"; + static void Main(string[] args) + { + Student student = new Student("孙悟空"); + Console.WriteLine($"我的老师叫{techer}"); + Student student1 = new Student("猪八戒"); + Console.WriteLine($"我的老师叫{techer}"); + Student student2 = new Student("沙和尚"); + Console.WriteLine($"我的老师叫{techer}"); + Student student3 = new Student("白龙马"); + Console.WriteLine($"我的老师叫{techer}"); + Console.WriteLine(""); + techer = "嫦娥姐姐"; + Student student6 = new Student("孙悟空"); + Console.WriteLine($"我的老师叫{techer}"); + Student student7 = new Student("猪八戒"); + Console.WriteLine($"我的老师叫{techer}"); + Student student8 = new Student("沙和尚"); + Console.WriteLine($"我的老师叫{techer}"); + Student student9 = new Student("白龙马"); + Console.WriteLine($"我的老师叫{techer}"); + } + } +``` + +```c# + /*提示: +定义一个学生类(有哪些字段属性?修饰符是什么?自己考量定义,至少学生姓名要吧), + 定义一个有参构造方法用来初始化学生姓名。 + 学生类中定义一个成员方法,用来输出学生的信息。 + +Main方法中,创建5个学生对象,每个学生执行输出学生信息的方法。 +学生的老师变更, +再执行每个学生执行输出学生信息的方法。*/ + internal class Student + { + string name; + public string Name { get; set; } + public Student(string name) + { + this.Name = name; + Console.Write($"大家好,我是{this.Name},"); + } + } +``` + diff --git "a/36\345\210\230\345\200\251\345\200\251/07/7_\351\235\231\346\200\201\345\255\227\346\256\265.md" "b/36\345\210\230\345\200\251\345\200\251/07/7_\351\235\231\346\200\201\345\255\227\346\256\265.md" deleted file mode 100644 index e69de29..0000000 -- Gitee From 8f8c5ce4307cc32e8cb2e4f4a9b00cd8e172ab18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?36=E5=88=98=E5=80=A9=E5=80=A9?= <2595352832@qq.com> Date: Sat, 6 May 2023 14:52:18 +0800 Subject: [PATCH 09/14] =?UTF-8?q?=E8=99=9A=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\350\231\232\346\226\271\346\263\225.md" | 323 ++++++++++++++++++ 1 file changed, 323 insertions(+) create mode 100644 "36\345\210\230\345\200\251\345\200\251/10/\350\231\232\346\226\271\346\263\225.md" diff --git "a/36\345\210\230\345\200\251\345\200\251/10/\350\231\232\346\226\271\346\263\225.md" "b/36\345\210\230\345\200\251\345\200\251/10/\350\231\232\346\226\271\346\263\225.md" new file mode 100644 index 0000000..d48666e --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/10/\350\231\232\346\226\271\346\263\225.md" @@ -0,0 +1,323 @@ +# 一、 + +```c# +internal class Program + { + //员工系统 + + /*5. + * 在主方法中为每个类实例化一个对象, + * 用每个对象调用工作方法;*/ + static void Main(string[] args) + { + Staff staff= new Staff("员工",'女',30); + staff.Work(); + + Doctor doctor= new Doctor("医生",'男',26); + doctor.Work(); + + Programmer programmer= new Programmer("程序员",'男',25); + programmer.Work(); + + Dustman dustman= new Dustman("清洁工",'女',35); + dustman.Work(); + + + } + } +``` + +```c# + /*1. Staff + * 定义一个员工类,作为基类, + * 包含姓名,性别,年龄等属性; + * 包含一个方法代表工作的行为*/ + + //属性 + internal class Staff + { + public string Name { get; set; } + public char Sex { get; set; } + public int Age { get; set; } + + public Staff() { } + public Staff(string name,char sex,int age) + { + Name = name; + Sex= sex; + Age = age; + } + + //行为 卖茶叶 + public virtual void Work() + { + Console.WriteLine($"{Name}在卖茶叶"); + } + } +``` + +```c# +/*2. Doctor + * 定义一个医生类,继承员工类,重写工作的方法, + * 描述医生的具体工作*/ + internal class Doctor:Staff + { + + public Doctor() { } + public Doctor(string name, char sex, int age) + { + Name = name; + Sex = sex; + Age = age; + } + + + public override void Work() + { + Console.WriteLine($"{Name}在救死扶伤"); + } + } +``` + +```c# +/*3. Programmer + * 定义一个程序员类,继承员工类,重写工作方法, + * 描述程序员具体工作*/ + internal class Programmer:Staff + + { + public Programmer() { } + public Programmer(string name, char sex, int age) + { + Name = name; + Sex = sex; + Age = age; + } + + public override void Work() + { + Console.WriteLine($"{Name}在敲代码"); + } + } +``` + +```c# +/*4. Dustman + * 定义一个清洁工类,继承员工类,重写工作方法, + * 描述清洁工的具体工作*/ + internal class Dustman : Staff + { + public Dustman() { } + public Dustman(string name, char sex, int age) + { + Name = name; + Sex = sex; + Age = age; + } + public override void Work() + { + Console.WriteLine($"{Name}在打扫卫生"); + } + } +``` + + + +# 二、 + +```c# + static void Main(string[] args) + { + AnimalClass animalClass = new AnimalClass(4,25,"小狗狗"); + animalClass.Eat(); + animalClass.Cry(); + animalClass.Sleep(); + Console.WriteLine(); + + Cat cat = new Cat(8,"猫咪"); + cat.Eat(); + cat.Cry(); + Tiger tiger= new Tiger(8,"老虎") ; + tiger.Eat(); + tiger.Cry(); + + Eagle eagle = new Eagle("老鹰"); + eagle.Eat(); + eagle.Cry(); + Swallow swallow=new Swallow(2,3,"燕子"); + swallow.Eat(); + swallow.Cry(); + + } +``` + +```c# + //动物类 AnimalClass 具有动物的 共同属性:腿,重量等;共同行为:吃,叫,睡 + internal class AnimalClass + { + //属性 腿,重量,名字 + public int Tui { get; set; } + public int ZhongLiang { get; set; } + public string Name { get; set; } + + public AnimalClass() { } + public AnimalClass(int tui, int zhongLiang, string name) + { + Tui = tui; + ZhongLiang = zhongLiang; + Name = name; + } + + + + //行为 吃,叫,睡 + public virtual void Eat() + { + Console.WriteLine($"{Name}在填饱肚子"); + } + public virtual void Cry() + { + Console.WriteLine($"{Name}在发出叫声"); + } + public virtual void Sleep() + { + Console.WriteLine($"{Name}在睡觉"); + } + + } +``` + +```c# +//猫科 Felidae 具有猫科动物的 共同属性:胡须等,共同能力:夜视能力 + internal class Felidae:AnimalClass + { + public int HuXu { get; set; } + + public Felidae() { } + + public Felidae(int huxu,string name) + { + HuXu = huxu; + Name = name; + } + + public virtual void YeShi() + { + Console.WriteLine($"{Name}有夜视能力"); + } + + } +``` + +```c# +//老虎 Tiger + //定义老虎、猫、老鹰、燕子四个子类,并且在这些子类中重写Animal 中吃和叫的方法; + internal class Tiger:Felidae + { + public Tiger() { } + public Tiger(int huxu,string name) + { + HuXu = huxu; + Name= name; + + } + + public override void Eat() + { + Console.WriteLine($"{Name}在捕食猎物"); + } + public override void Cry() + { + Console.WriteLine($"{Name}填饱肚子后发出满足的叫声"); + } + } +``` + +```c# +//猫 Cat + //定义老虎、猫、老鹰、燕子四个子类,并且在这些子类中重写Animal 中吃和叫的方法; + internal class Cat:Felidae + { + public Cat() { } + public Cat(int huxu,string name) + { + HuXu= huxu; + Name= name; + + } + + public override void Eat() + { + Console.WriteLine($"{Name}在吃猫粮"); + } + public override void Cry() + { + Console.WriteLine($"{Name}伸懒腰时发出舒适的叫声"); + } + } +``` + +```c# +//鸟类 Birds 具有鸟类的共同行为:飞行 + internal class Birds:AnimalClass + { + public Birds() { } + public Birds(int tui,int zhongliang,string name) + { + Tui= tui; + ZhongLiang= zhongliang; + Name= name; + } + public virtual void FeiXing() + { + Console.WriteLine($"{Name}在飞"); + } + } +``` + +```c# +//老鹰 Eagle + //定义老虎、猫、老鹰、燕子四个子类,并且在这些子类中重写Animal 中吃和叫的方法; + internal class Eagle:Birds + { + public Eagle() { } + public Eagle(string name) + { + Name= name; + } + + public override void Eat() + { + Console.WriteLine($"{Name}在吃刚捕捉来的蛇"); + } + public override void Cry() + { + Console.WriteLine($"{Name}在高空中发出鹰叫"); + } + } +``` + +```c# +//燕子 Swallow + //定义老虎、猫、老鹰、燕子四个子类,并且在这些子类中重写Animal 中吃和叫的方法; + internal class Swallow:Birds + { + public Swallow() { } + public Swallow(int tui,int zhongliang,string name) + { + Tui= tui; + ZhongLiang= zhongliang; + Name= name; + } + + public override void Eat() + { + Console.WriteLine($"{Name}妈妈在觅食"); + } + public override void Cry() + { + Console.WriteLine($"小{Name}们因为肚子饿一直叫"); + } + } +``` + -- Gitee From f6d0904caabab2e803b0b9dfb622856b05ef9e88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=80=A9=E5=80=A9?= <2595352832@qq.com> Date: Sat, 6 May 2023 23:12:58 +0800 Subject: [PATCH 10/14] =?UTF-8?q?=E5=A4=9A=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../11/\350\256\241\347\256\227.md" | 136 ++++++++++++++++++ .../11/\351\242\234\350\211\262.md" | 89 ++++++++++++ 2 files changed, 225 insertions(+) create mode 100644 "36\345\210\230\345\200\251\345\200\251/11/\350\256\241\347\256\227.md" create mode 100644 "36\345\210\230\345\200\251\345\200\251/11/\351\242\234\350\211\262.md" diff --git "a/36\345\210\230\345\200\251\345\200\251/11/\350\256\241\347\256\227.md" "b/36\345\210\230\345\200\251\345\200\251/11/\350\256\241\347\256\227.md" new file mode 100644 index 0000000..5eb5953 --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/11/\350\256\241\347\256\227.md" @@ -0,0 +1,136 @@ +```c# +internal class Program + { +/* + * 一、编写一个控制台应用程序,接受用户输入的两个整数和一个操作符, + * 以实现对两个整数的加、减、乘、除运算,并显示出计算结果。 + +1、创建Calculate基类,其中包含两个整型的protected成员,用以接收用户输入的两个整数。 + 定义一个DisplayResult()虚方法,计算并输出结果。 +2、定义四个类继承自Calculate类,分别重写DisplayResult()方法, + 实现两个整数的加、减、乘、除运算,并输出结果。 +3、在主方法中接受用户输入操作符,根据用户输入的操作符,实例化相应的类,完成运算并输出结果。 +4、在主类中添加一个方法,形参为父类对象,根据传递实参的类型,调用方法,实现计算和显示结果。 + * + * **/ + static void Main(string[] args) + { + //接收用户输入 + Console.WriteLine("请输入第一个整数:"); + int num1=Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("请输入第二个整数:"); + int num2 = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("请输入运算操作符:"); + string fuhao=Console.ReadLine(); + + //3.判断fuhao的值,实例化出相应的对象 + + if (fuhao == "+") + { + Jia jia = new Jia(num1,num2,fuhao); + jia.DisplayResult(); + } + else if (fuhao == "-") + { + Jian jian=new Jian(num1,num2,fuhao); + jian.DisplayResult(); + } + else if (fuhao=="*") + { + Cheng chn=new Cheng(num1,num2,fuhao); + chn.DisplayResult(); + } + else if (fuhao == "/") + { + Chu chu=new Chu(num1,num2,fuhao); + chu.DisplayResult(); + } + + } + //4.在主类中添加一个方法,形参为父类对象,根据传递实参的类型,调用方法,实现计算和显示结果。 + public static void Calculate(Calculate calculate) + { + calculate.DisplayResult(); + } + } +``` + +1. + +```c# + + internal class Calculate + { + //其中包含两个整型的protected成员,用以接收用户输入的两个整数。 + + protected int num1; + protected int num2; + protected string fuhao; + + public Calculate(int num1,int num2,string fuhao) + { this.num1=num1;this.num2 = num2;this.fuhao = fuhao; } + + //定义一个DisplayResult()虚方法,计算并输出结果。 + public virtual void DisplayResult() + { + //运算由子类实现 + } + + } +``` + +2. + +```c# + + + internal class Jia:Calculate + { + public Jia(int num1, int num2,string fuhao) : base(num1, num2,fuhao) { } + + public override void DisplayResult() + { + Console.WriteLine($"{num1}{fuhao}{num2}={num1 + num2}"); + } + } +``` + +```c# +internal class Jian:Calculate + { + public Jian(int num1, int num2, string fuhao) : base(num1, num2, fuhao) { } + + + public override void DisplayResult() + { + Console.WriteLine($"{num1}{fuhao}{num2}={num1 - num2}"); + } + } +``` + +```c# + internal class Cheng : Calculate + { + public Cheng(int num1, int num2, string fuhao) : base(num1, num2, fuhao) { } + + + public override void DisplayResult() + { + Console.WriteLine($"{num1}{fuhao}{num2}={num1 * num2}"); + } + } +``` + +```c# + + internal class Chu : Calculate + { + public Chu(int num1, int num2, string fuhao) : base(num1, num2, fuhao) { } + + public override void DisplayResult() + { + Console.WriteLine($"{num1}{fuhao}{num2}={num1 / (double)num2}"); + } + } +``` + diff --git "a/36\345\210\230\345\200\251\345\200\251/11/\351\242\234\350\211\262.md" "b/36\345\210\230\345\200\251\345\200\251/11/\351\242\234\350\211\262.md" new file mode 100644 index 0000000..7503550 --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/11/\351\242\234\350\211\262.md" @@ -0,0 +1,89 @@ +```c# + internal class Program + { + /*二、创建一个Shape(形状)类,此类包含一个名为color的数据成员,用于存储颜色, + 这个类还包含一个名为GetArea()的虚方法(这个方法是用来获取形状面积的)。 + 基于这个Shape,创建两个子类:Circle(圆形类)和Square(正方形类), + Circle类中包含radius(半径)的数据成员,Square类中包含sideLen(边长)的数据成员, + 这两个子类都去重写父类的GetArea()方法,各自去实现计算自己的面积。 + + 在主类中添加一个方法,参数类型就是Shape,方法体中,用形参去调用GetArea()方法。 + main方法中去测试这个方法。*/ + static void Main(string[] args) + { + Console.WriteLine("请输入颜色:"); + string color=Console.ReadLine(); + Console.WriteLine("请输入圆的半径:"); + int radius=Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("请输入正方形的边长为:"); + int sideLen=Convert.ToInt32(Console.ReadLine()); + + Circle circle = new Circle(color,radius); + circle.GetArea(); + Square square = new Square(color,sideLen); + square.GetArea(); + + + } + //在主类中添加一个方法,参数类型就是Shape,方法体中,用形参去调用GetArea()方法。 + public static void Shape(Shape shape) { shape.GetArea(); } + } +``` + +```c# + + //创建一个Shape(形状)类, + //此类包含一个名为color的数据成员,用于存储颜色, + //这个类还包含一个名为GetArea()的虚方法(这个方法是用来获取形状面积的)。 + + internal class Shape + { + public string color; + //public int radius; + //public int sideLen; + + public Shape(string color) + { + this.color = color; + + } + + + public virtual void GetArea() { } + } +``` + +```c# +//Circle(圆形类) + //Circle类中包含radius(半径)的数据成员 + //重写父类的GetArea()方法,各自去实现计算自己的面积。 + internal class Circle:Shape + { + public int radius; + + public Circle(string color, int radius) :base(color) { this.radius = radius; } + + + + public override void GetArea() + { Console.WriteLine($"{color}圆的面积为:3.14*{radius}*{radius}={3.14*radius*radius}"); } + } +``` + +```c# + //Square(正方形类) + //Square类中包含sideLen(边长)的数据成员 + //重写父类的GetArea()方法,各自去实现计算自己的面积。 + internal class Square:Shape + { + public int sideLen; + + public Square(string color,int sideLen) : base(color) { this.sideLen = sideLen; } + public override void GetArea() + { Console.WriteLine($"{color}正方形的面积为:{sideLen}*{sideLen}={sideLen * sideLen}"); } + + + + } +``` + -- Gitee From 77680d68104cc3f7f667a6f289794d0ab79a9b1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=80=A9=E5=80=A9?= <2595352832@qq.com> Date: Sun, 7 May 2023 16:18:32 +0800 Subject: [PATCH 11/14] =?UTF-8?q?=E7=B1=BB=E5=92=8C=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../04/\347\261\273\345\222\214\345\261\236\346\200\247.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "36\345\210\230\345\200\251\345\200\251/04/\346\236\204\351\200\240\346\226\271\346\263\225.md" => "36\345\210\230\345\200\251\345\200\251/04/\347\261\273\345\222\214\345\261\236\346\200\247.md" (100%) diff --git "a/36\345\210\230\345\200\251\345\200\251/04/\346\236\204\351\200\240\346\226\271\346\263\225.md" "b/36\345\210\230\345\200\251\345\200\251/04/\347\261\273\345\222\214\345\261\236\346\200\247.md" similarity index 100% rename from "36\345\210\230\345\200\251\345\200\251/04/\346\236\204\351\200\240\346\226\271\346\263\225.md" rename to "36\345\210\230\345\200\251\345\200\251/04/\347\261\273\345\222\214\345\261\236\346\200\247.md" -- Gitee From b518276d49bd06cfc2b763360c35e190a6626d3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=80=A9=E5=80=A9?= <2595352832@qq.com> Date: Sun, 7 May 2023 16:22:40 +0800 Subject: [PATCH 12/14] =?UTF-8?q?=E6=9E=84=E9=80=A0=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../06/\346\236\204\351\200\240\346\226\271\346\263\225.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "36\345\210\230\345\200\251\345\200\251/06/\347\261\273\345\222\214\345\261\236\346\200\247.md" => "36\345\210\230\345\200\251\345\200\251/06/\346\236\204\351\200\240\346\226\271\346\263\225.md" (100%) diff --git "a/36\345\210\230\345\200\251\345\200\251/06/\347\261\273\345\222\214\345\261\236\346\200\247.md" "b/36\345\210\230\345\200\251\345\200\251/06/\346\236\204\351\200\240\346\226\271\346\263\225.md" similarity index 100% rename from "36\345\210\230\345\200\251\345\200\251/06/\347\261\273\345\222\214\345\261\236\346\200\247.md" rename to "36\345\210\230\345\200\251\345\200\251/06/\346\236\204\351\200\240\346\226\271\346\263\225.md" -- Gitee From 5f37ccc885439d7c18bc36ead0951e9eedf6627a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C36=E5=88=98=E5=80=A9=E5=80=A9=E2=80=9D?= <2595352832@qq.com> Date: Wed, 31 May 2023 14:05:30 +0800 Subject: [PATCH 13/14] =?UTF-8?q?=E6=B3=9B=E5=9E=8B=E9=9B=86=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Program.cs" | 86 +++++++++++++++++++ .../Student.cs" | 44 ++++++++++ 2 files changed, 130 insertions(+) create mode 100644 "36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Program.cs" create mode 100644 "36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Student.cs" diff --git "a/36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Program.cs" "b/36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Program.cs" new file mode 100644 index 0000000..abba3ee --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Program.cs" @@ -0,0 +1,86 @@ +using System.Collections; +using System.Collections.Generic; + +namespace _12_集合和泛型 +{ + internal class Program + { + /* + * 作业1:输入班级人数,输入每个人的学号,姓名、语,数,英成绩进入集合 + 求语文的总分,数学的平均分,英语的最高分的人的姓名(使用ArrayList) + + 数据:学号 姓名 语文 数学 英语 + 1 乔峰 85 42 67 + 2 段誉 94 34 46 + 3 虚竹 99 99 99 + */ + + //存储学生信息 + static int length = 0; + static ArrayList list = new ArrayList(length); + + static void Main(string[] args) + { + Console.WriteLine("请输入班级人数:"); + int length = Convert.ToInt32(Console.ReadLine()); + + // List<数据类型> 对象名 = new List<数据类型>(); + // List arraylist = new List(length); + + for (int i = 1; i <= length; i++) + { + //添加学生信息 + Student student = new Student(); + Console.WriteLine($"请输入第{i}个学生的学号:"); + student.Id = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine($"请输入第{i}个学生的姓名:"); + student.Name = Console.ReadLine(); + Console.WriteLine($"请输入第{i}个学生的语文成绩:"); + student.Score1 = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine($"请输入第{i}个学生的数学成绩:"); + student.Score2 = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine($"请输入第{i}个学生的英语成绩:"); + student.Score3 = Convert.ToInt32(Console.ReadLine()); + //把学生对象添加到集合中 + list.Add(student); + + } + + int num1 = 0; + int num2 = 0; + int num3 = 0; + string na = ""; + + Console.WriteLine("该班级的学生信息为:"); + Console.WriteLine("学号 姓名 语文 数学 英语"); + PrintAll(); + + for (int i = 0; i < list.Count; i++) + { + //因为是ArrayList所以强制转换 + Student stu = list[i] as Student; + num1 += stu.Score1; + num2 += stu.Score2; + + + + } + + Console.WriteLine($"语文的总分为:{num1}"); + Console.WriteLine($"数学的平均分为:{num2 / list.Count}"); + Console.WriteLine($"英语最高分的人为:{na}"); + + } + public static void PrintAll() + { + + //迭代遍历输出信息 + foreach (Student student in list) + { + + //Console.WriteLine($"{student.Id} {student.Name} {student.Score1} {student.Score2} {student.Score3}"); + Console.WriteLine(student); + } + } + } +} \ No newline at end of file diff --git "a/36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Student.cs" "b/36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Student.cs" new file mode 100644 index 0000000..e9addb3 --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Student.cs" @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace _12_集合和泛型 +{ + internal class Student + { + //数据:学号 姓名 语文 数学 英语 + + private int id; + private string name; + private int score1; + private int score2; + private int score3; + + public int Id { get => id; set => id = value; } + public string Name { get => name; set => name = value; } + public int Score1 { get => score1; set { if (value >= 0) { score1 = value; } } } + public int Score2 { get => score2; set { if (value >= 0) { score2 = value; } } } + public int Score3 { get => score3; set { if (value >= 0) { score3 = value; } } } + + public Student() { } + + public Student(int id, string name, int score1, int score2, int score3) + { + this.id = id; + this.name = name; + this.score1 = score1; + this.score2 = score2; + this.score3 = score3; + } + + //重写方法 + public override string ToString() + { + return $"{id},{name},{score1},{score2},{score3}"; + } + } +} + + -- Gitee From b2154e607c057586f297709b866307f23c718c03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C36=E5=88=98=E5=80=A9=E5=80=A9=E2=80=9D?= <2595352832@qq.com> Date: Wed, 31 May 2023 14:12:22 +0800 Subject: [PATCH 14/14] =?UTF-8?q?=E6=8A=BD=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Program.cs" | 86 --------------- .../Student.cs" | 44 -------- .../CookRobot.cs" | 49 +++++++++ .../DeliveryRobot.cs" | 26 +++++ .../Program.cs" | 75 +++++++++++++ .../Robot.cs" | 31 ++++++ .../\346\270\270\346\210\217/Blacksmith.cs" | 27 +++++ .../13/\346\270\270\346\210\217/Npc.cs" | 27 +++++ .../13/\346\270\270\346\210\217/Program.cs" | 102 ++++++++++++++++++ .../13/\346\270\270\346\210\217/Task.cs" | 27 +++++ .../13/\346\270\270\346\210\217/Vendors.cs" | 27 +++++ 11 files changed, 391 insertions(+), 130 deletions(-) delete mode 100644 "36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Program.cs" delete mode 100644 "36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Student.cs" create mode 100644 "36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/CookRobot.cs" create mode 100644 "36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/DeliveryRobot.cs" create mode 100644 "36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/Program.cs" create mode 100644 "36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/Robot.cs" create mode 100644 "36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Blacksmith.cs" create mode 100644 "36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Npc.cs" create mode 100644 "36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Program.cs" create mode 100644 "36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Task.cs" create mode 100644 "36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Vendors.cs" diff --git "a/36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Program.cs" "b/36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Program.cs" deleted file mode 100644 index abba3ee..0000000 --- "a/36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Program.cs" +++ /dev/null @@ -1,86 +0,0 @@ -using System.Collections; -using System.Collections.Generic; - -namespace _12_集合和泛型 -{ - internal class Program - { - /* - * 作业1:输入班级人数,输入每个人的学号,姓名、语,数,英成绩进入集合 - 求语文的总分,数学的平均分,英语的最高分的人的姓名(使用ArrayList) - - 数据:学号 姓名 语文 数学 英语 - 1 乔峰 85 42 67 - 2 段誉 94 34 46 - 3 虚竹 99 99 99 - */ - - //存储学生信息 - static int length = 0; - static ArrayList list = new ArrayList(length); - - static void Main(string[] args) - { - Console.WriteLine("请输入班级人数:"); - int length = Convert.ToInt32(Console.ReadLine()); - - // List<数据类型> 对象名 = new List<数据类型>(); - // List arraylist = new List(length); - - for (int i = 1; i <= length; i++) - { - //添加学生信息 - Student student = new Student(); - Console.WriteLine($"请输入第{i}个学生的学号:"); - student.Id = Convert.ToInt32(Console.ReadLine()); - Console.WriteLine($"请输入第{i}个学生的姓名:"); - student.Name = Console.ReadLine(); - Console.WriteLine($"请输入第{i}个学生的语文成绩:"); - student.Score1 = Convert.ToInt32(Console.ReadLine()); - Console.WriteLine($"请输入第{i}个学生的数学成绩:"); - student.Score2 = Convert.ToInt32(Console.ReadLine()); - Console.WriteLine($"请输入第{i}个学生的英语成绩:"); - student.Score3 = Convert.ToInt32(Console.ReadLine()); - //把学生对象添加到集合中 - list.Add(student); - - } - - int num1 = 0; - int num2 = 0; - int num3 = 0; - string na = ""; - - Console.WriteLine("该班级的学生信息为:"); - Console.WriteLine("学号 姓名 语文 数学 英语"); - PrintAll(); - - for (int i = 0; i < list.Count; i++) - { - //因为是ArrayList所以强制转换 - Student stu = list[i] as Student; - num1 += stu.Score1; - num2 += stu.Score2; - - - - } - - Console.WriteLine($"语文的总分为:{num1}"); - Console.WriteLine($"数学的平均分为:{num2 / list.Count}"); - Console.WriteLine($"英语最高分的人为:{na}"); - - } - public static void PrintAll() - { - - //迭代遍历输出信息 - foreach (Student student in list) - { - - //Console.WriteLine($"{student.Id} {student.Name} {student.Score1} {student.Score2} {student.Score3}"); - Console.WriteLine(student); - } - } - } -} \ No newline at end of file diff --git "a/36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Student.cs" "b/36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Student.cs" deleted file mode 100644 index e9addb3..0000000 --- "a/36\345\210\230\345\200\251\345\200\251/12/\345\255\246\347\224\237\346\210\220\347\273\251/Student.cs" +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace _12_集合和泛型 -{ - internal class Student - { - //数据:学号 姓名 语文 数学 英语 - - private int id; - private string name; - private int score1; - private int score2; - private int score3; - - public int Id { get => id; set => id = value; } - public string Name { get => name; set => name = value; } - public int Score1 { get => score1; set { if (value >= 0) { score1 = value; } } } - public int Score2 { get => score2; set { if (value >= 0) { score2 = value; } } } - public int Score3 { get => score3; set { if (value >= 0) { score3 = value; } } } - - public Student() { } - - public Student(int id, string name, int score1, int score2, int score3) - { - this.id = id; - this.name = name; - this.score1 = score1; - this.score2 = score2; - this.score3 = score3; - } - - //重写方法 - public override string ToString() - { - return $"{id},{name},{score1},{score2},{score3}"; - } - } -} - - diff --git "a/36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/CookRobot.cs" "b/36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/CookRobot.cs" new file mode 100644 index 0000000..09af332 --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/CookRobot.cs" @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace 抽象 +{ + + internal class CookRobot : Robot + { + + //在CookRobot中添加一个代表菜的类型的字段,用枚举类型 + // [访问修饰符] + // enum 枚举类型名 : [数据类型] + // { + // 值l, + // 值2, + //..., + // 值N + // } + public enum Vegetable + { + 西红柿炒鸡蛋 = 1, + 酸辣土豆丝, + 荔枝肉 + + } + + //public Vegetable foods; + // 枚举类型名称 枚举变量名 = 枚举类型名称.枚举元素值; + public static Vegetable food = Vegetable.西红柿炒鸡蛋; + //定义枚举类型的变量,该变量的值只能是枚举类型定义的值 + int i = (int)food; + + + + + public CookRobot(string name) : base(name) { } + + //实现具体的Working()方法 + public override void Working() + { + Console.WriteLine($"{Name}正在抄{food}"); + } + + + } +} diff --git "a/36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/DeliveryRobot.cs" "b/36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/DeliveryRobot.cs" new file mode 100644 index 0000000..c69002d --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/DeliveryRobot.cs" @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace 抽象 +{ + + internal class DeliveryRobot:Robot + { + //在DeliveryRobot中添加一个代表连续工作时长的字段hours + private int hours; + + public int Hours { get => hours; set => hours = value; } + + public DeliveryRobot(string name, int hours) : base(name) => this.hours = hours; + + + //实现具体的Working()方法 + public override void Working() + { + Console.WriteLine($"请稍等,{Name}正在传菜,预计需要{Hours}分钟"); + } + } +} diff --git "a/36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/Program.cs" "b/36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/Program.cs" new file mode 100644 index 0000000..76f6e58 --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/Program.cs" @@ -0,0 +1,75 @@ +using System.Xml.Linq; +using static 抽象.CookRobot; + +namespace 抽象 +{ + + internal class Program + { + /*Main方法中:用户输入所选择的机器人的功能,根据用户的输入Robot执行对应的功能。 + * Robot类应包含机器人姓名name字段、机器人工作的方法Working(), + * 该方法应该在子类中被实现,机器人工作的方式很多,所以Working()方法应该被定义为抽象方法。 + + 可以在CookRobot中添加一个代表菜的类型的字段,用枚举类型; + 在DeliveryRobot中添加一个代表连续工作时长的字段hours。 + CookRobot和DeliveryRobot应实现具体的Working()方法。 + + 在主类中定义一个方法,形参数据类型是string,返回值数据类型是Robot, + 方法中实现如果传入的字符串是“炒菜”,那就返回CookRobot的实例(当然是要用Robot的引用指向的), + 如果传入的字符串是“传菜”,那就返回DeliveryRobot的实例。 + + +*/ + + static void Main(string[] args) + { + Console.WriteLine("请输入机器人所要执行功能的序号:1.炒菜 or 2.传菜"); + int Robot =Convert.ToInt32 (Console.ReadLine()); + + if (Robot== 1) + { + Fstring("炒菜"); + } + else if(Robot == 2) + { + Fstring("传菜"); + } + else { Console.WriteLine("没有该功能,请重新输入"); } + + + } + //在主类中定义一个方法,形参数据类型是string,返回值数据类型是Robot + public static Robot Fstring (string robot) + { + + if (robot == "炒菜") + { + + Console.WriteLine("请输入需要抄菜的序号:(1.西炒鸡蛋红柿 2.酸辣土豆丝 3.荔枝肉)"); + int i = Convert.ToInt32(Console.ReadLine()); + + //Vegetable foods=food; + // string 转换成 枚举 + // Vegetable food = (Vegetable)System.Enum.Parse(typeof(Vegetable), Robot); + + CookRobot cookRobot = new CookRobot("小韦"); + cookRobot.Working(); + return cookRobot; + } + + else if (robot == "传菜") + { + DeliveryRobot deliveryRobot = new DeliveryRobot("小刘", 3); + deliveryRobot.Working(); + return deliveryRobot; + } + + + + return null; + + + } + } + +} \ No newline at end of file diff --git "a/36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/Robot.cs" "b/36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/Robot.cs" new file mode 100644 index 0000000..fd636b9 --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/13/\346\234\272\345\231\250\344\272\272/Robot.cs" @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace 抽象 +{ + // Robot类应包含机器人姓名name字段、机器人工作的方法Working(),Working()方法为抽象方法 + + // [访问修饰符] abstract class 类名{ //类成员 } + +internal abstract class Robot + { + //字段 + private string name; + + //属性 + public string Name { get => name; set => name = value; } + + //有参构造 + public Robot (string name) + { + this.Name = name; + } + + // [访问修饰符] abstract 方法返回值类型 方法名(参数列表); + public abstract void Working(); + } + +} diff --git "a/36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Blacksmith.cs" "b/36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Blacksmith.cs" new file mode 100644 index 0000000..461f46b --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Blacksmith.cs" @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace 抽象2 +{ + // 铁匠 + internal class Blacksmith : Npc + { + //3)铁匠NPC可以修理装备 + private string repair; + + public Blacksmith(string name,int id,string repair):base(name,id) + { + this.Repair = repair; + } + + public string Repair { get => repair; set => repair = value; } + + public override void Speak() + { + Console.WriteLine($"玩家请稍等,{Name}正在{repair}"); + } + } +} diff --git "a/36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Npc.cs" "b/36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Npc.cs" new file mode 100644 index 0000000..0c3a065 --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Npc.cs" @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace 抽象2 +{ + internal abstract class Npc + { + // 共有属性:npc 的名字,npc 的类型; + private string name; + private int id; + //属性 + public string Name { get => name; set => name = value; } + public int Id { get => id; set => id = value; } + //构造方法 + public Npc(string name, int id) + { + this.Name = name; + this.Id = id; + } + // 共有方法:都能和玩家交互(交谈); + public abstract void Speak(); + + } +} diff --git "a/36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Program.cs" "b/36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Program.cs" new file mode 100644 index 0000000..99e705d --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Program.cs" @@ -0,0 +1,102 @@ +namespace 抽象2 +{ + /*2.使用抽象类结构实现游戏中NPC 模块 + + 在游戏中会出现很多种不同用途的 NPC,这些 NPC 有各自的存在的价值和作用,同时又具备一些共性的东西。 + 在开发 NPC 系统的时候,往往是需要提取共性,独立出一个父类,然后子类继承实现不同作用的 NPC。 + + 分析:任务 NPC,商贩 NPC,铁匠 NPC,三种 NPC 的种类。 + + 共有属性:npc 的名字,npc 的类型; + + 共有方法:都能和玩家交互(交谈); + + 不同类型的 NPC 实现自己的行为(可以定义为抽象方法) + 1)任务NPC下达任务 + 2)商贩NPC出售物品 + 3)铁匠NPC可以修理装备 +*/ + internal class Program + { + static void Main(string[] args) + { + + Console.WriteLine(); + Console.WriteLine("玩家所要去找的NPC类型序号为(1.任务 2.商贩 3.铁匠):"); + int num = Convert.ToInt32(Console.ReadLine()); + + //任务 + if (num == 1) + { + Console.WriteLine("请玩家选择NPC类型序号(1.送信 2.打怪 3.采集)"); + int renwu=Convert.ToInt32(Console.ReadLine()); + + if (renwu ==1) + { + Npc npc = new Task("小李", 1, "请把信送给狄大人"); + npc.Speak(); + }else if (renwu ==2) + { + Npc npc = new Task("小古", 1, "前往峡谷,通过打怪收集足够的怪兽碎片"); + npc.Speak(); + }else if (renwu ==3) + { + Npc npc = new Task("小农", 1, "前往森林,采集所需要的药草"); + npc.Speak(); + } + else { Console.WriteLine("没有该NPC,请重新选择!"); } + } + + //商贩 + else if (num == 2) + { + Console.WriteLine("请玩家选择NPC类型序号(1.武器 2.材料 3.食物)"); + int goods=Convert.ToInt32(Console.ReadLine()); + + if (goods==1) + { + Npc npc1 = new Vendors("小程", 2, "弓箭,长剑"); + npc1.Speak(); + }else if (goods==2) + { + Npc npc1 = new Vendors("小鲁", 2, "木头,钢铁"); + npc1.Speak(); + }else if (goods ==3) + { + Npc npc1 = new Vendors("小满", 2, "小鱼干,AD钙,辣条"); + npc1.Speak(); + }else { Console.WriteLine("没有该NPC,请重新选择!"); } + + } + + //铁匠 + else if (num == 3) + { + Console.WriteLine("请玩家选择NPC类型序号(1.修补 2.强化 3.打造)"); + int tie = Convert.ToInt32(Console.ReadLine()); + + if (tie==1) + { + Npc npc2 = new Blacksmith("小灰", 3, "修补你破损的装备"); + npc2.Speak(); + }else if (tie==2) + { + Npc npc2 = new Blacksmith("小铭", 3, "强化你的孔雀翎"); + npc2.Speak(); + } + else if (tie==3) + { + Npc npc2 = new Blacksmith("小唐", 3, "打造你的武器"); + npc2.Speak(); + } + else { Console.WriteLine("没有该NPC,请重新选择!"); } + + } + + //输入错误 + else { Console.WriteLine("没有该NPC类型,请重新选择!"); } + + + } + } +} \ No newline at end of file diff --git "a/36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Task.cs" "b/36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Task.cs" new file mode 100644 index 0000000..996ff8f --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Task.cs" @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace 抽象2 +{ + // 任务 + internal class Task : Npc + { + //1)任务NPC下达任务 + private string a; + + public string A { get => a; set => a = value; } + + public Task(string name,int id,string a):base(name,id) + { + this.A = a; + } + + public override void Speak() + { + Console.WriteLine($"{Name}:玩家你所接到的任务为 {a}"); + } + } +} diff --git "a/36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Vendors.cs" "b/36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Vendors.cs" new file mode 100644 index 0000000..78e6978 --- /dev/null +++ "b/36\345\210\230\345\200\251\345\200\251/13/\346\270\270\346\210\217/Vendors.cs" @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace 抽象2 +{ + // 商贩 + internal class Vendors : Npc + { + //2)商贩NPC出售物品 + private string shangping; + + public string Shangping1 { get => shangping; set => shangping = value; } + + public Vendors(string name,int id,string shangping):base(name,id) + { + this.Shangping1 = shangping; + } + + public override void Speak() + { + Console.WriteLine($"{Name}:玩家所需要的商品 {shangping} 正在准备中"); + } + } +} -- Gitee