7、c#面向对象03

张开发
2026/4/20 2:47:59 15 分钟阅读

分享文章

7、c#面向对象03
1 作业using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; ​ namespace demo02_oop { internal class demo08_作业 { ​ //成员变量---字段 static void Main(string[] args) { Worker w1 new Worker(韩梅梅,19,10000); w1.addr new Address(河南.郑州.二七区,400001); w1.Work(); w1.show(); //35i*46i 3*4-5*63*64*5i -18 38i Complex c1 new Complex(3,5); Complex c2 new Complex(4, 6); Complex c3 c1.add(c2); c3.show(); c3 c2.sub(c1); c3.show(); c3c1.mul(c2); c3.show(); ​ } //1. 面向对象基础写一个Worker 类并创建多个Worker 对象。 //1) 为Worker 类添加三个字段1String 类型的name表示工人的姓名 //2int 类型的age表示工人的年龄3double 类型的salary表示工人 //的工资。 //2) 为Worker 类添加两个构造方法1公开无参构造方法2接受三个参 //数的构造方法三个参数分别为字符串、int 和double 类型。 //3) 为Worker 类添加两个work 方法一个无参另一个带整数参数表示 //工人工作的时间为多少小时。 public class Worker { //字段的类型可以是基本数据类型 也 可以是引用数据类型 //基本数类型字段赋值 一个值即可 //引用数类型字段赋值 一个对应类型的对象 //1 成员变量表示数据 public System.String name;//引用数据类型 public int age1;//值类型 public double salary111.1;//值类型 public Address addrnew Address();//引用数据类型 //2 成员方法表示功能 public void Work() { Console.WriteLine(工人name正在奋力工作); } public void Work(int hour) { Console.WriteLine(工人 name 正在奋力工作已经连续工作小时数为hour); } public void show() { //方法中 成员前面默认有this. Console.WriteLine($我是一名工人名字是{name}年龄是{age}工资是{salary},地址是{this.addr.address}邮编是{this.addr.zipCode}); } ​ ​ ​ //3 构造方法满足创建不同对象的需求 public Worker() { } //构造方法的参数列表一般是 给字段赋值 public Worker(string name, int age, double salary) { this.name name; this.age age; this.salary salary; } } ​ ​ ​ //2. 面向对象基础创建一个Address 类描述如下 //1 该类有两个字段1String 类型的address表示地址2String 类型 //的zipCode表示邮编。 //2 该类有两个构造方法一为无参构造方法一为带两个参数的方法。 public class Address { public string address; public string zipCode; public Address() { } public Address(string address, string zipCode) { this.address address; this.zipCode zipCode; } } ​ //3. *面向对象基础为第1 题中的Worker 类添加一个属性addr该属性为 //Address 类型。 //创建一个Worker 对象其姓名为zhangsan年龄为25工资为2500家庭住 //址为“北京市海淀区清华园1 号”邮政编码为100084。 ​ ​ ​ ​ //4. 面向对象基础**复数概念如下 //每个复数都有实部和虚部。例如3 5i3 为实部5i 为虚部。其中i //称为虚数单位有i* i -1。 //两个复数进行加法运算运算时实部与实部相加虚部与虚部相加。例如 //(1.5 – 3i) (2.3 2.4i) (1.52.3) (-3 2.4)i 3.8 – 0.6i //两个复数进行减法运算与加法运算类似。 //两个复数进行乘法运算其过程如下 //(abi) * (c di) ac adi bci bd(i* i) (ac-bd) (adbc)i //例如 //35i*46i 3*4-5*63*64*5i -18 38i //写一个类Complex用来表示复数。这个复数类具有两个字段double real //表示实部double im表示虚部。并为Complex 类增加add、sub、mul 方法 //分别表示复数的加法、减法和乘法运算。其中add 方法的声明如下 //public Complex add(Complex c) //表示当前Complex 对象与参数c 对象相 //加 public class Complex { public double im;//虚部 public double real;//实部 public Complex() { } public Complex( double real,double im) { this.im im;this.real real; } //123 public Complex add(Complex c) { //add涉及到三个Complex this当前对象参数c返回值值 //两个加数this和c 和是返回值 Complex result new Complex(); result.real c.real this.real; result.im c.im this.im; return result; } public Complex sub(Complex c) { //add涉及到三个Complex this当前对象参数c返回值值 //两个加数this和c 和是返回值 Complex result new Complex(); result.real this.real-c.real; result.im this.im-c.im; return result; } ​ public Complex mul(Complex c) { //(a bi) * (c di) ac adi bci bd(i * i) (ac - bd) (ad bc)i Complex result new Complex(); result.real this.real * c.real - this.im * c.im; result.im this.real * c.im this.im * c.real; return result; } ​ public void show() { Console.WriteLine(realimi); } ​ ​ } ​ ​ } } ​2 创建对象内存图2.1 准备类class Car{ public int a1; public string logo; public int zuoWei; public Car(){} public Car(string logo,int zuoWei){} public void show(){} }2.2 画图3 属性​ internal class demo09_属性 { ​ static void Main(string[] args) { Console.WriteLine(Hello, World!); ​ // public int age 取值有使用者决定 Demo08 d1 new Demo08(); d1.age 19; d1.age -1;//调用者可以直接调用字段不安全 不可控 ​ //解决方案1把字段私有化 通过公共的getset方法 d1.setAgePrivate(-11);//设置值 Console.WriteLine(d1.getAgePrivate());//获取值 ​ ​ //解决方案2通过属性(自带getset访问器的字段)来操作字段 d1.PropertyAge01 1; Console.WriteLine(d1.PropertyAge01); ​ d1.PropertyAge02 19; Console.WriteLine(d1.PropertyAge02 d1.PropertyAge02); ​ d1.PropertyAge03 191; Console.WriteLine(d1.PropertyAge03 d1.PropertyAge03); ​ ​ } ​ } class Demo08 { public int age;//Demo08类无法控制age值的取值 ​ private int agePrivate;//private修饰符 私有的 作用域是当前类 //提供公共的getset方法来访问私有的agePrivate public int getAgePrivate() { return agePrivate; } public void setAgePrivate(int a) { if (a0 a130) { agePrivate a; } } public void show() { Console.WriteLine(ageage); Console.WriteLine(agePrivate agePrivate); Console.WriteLine(PropertyAge01 PropertyAge01); Console.WriteLine(PropertyAge02 PropertyAge02); } //字段field //属性property ​ ​ ​ ​ //属性写法1简略写法没有对设置的值进行校验 public int PropertyAge01 { get;set;//getset访问器 } ​ ​ private int ageField;//字段 //属性写法2通过value对指定的字段进行校验 public int PropertyAge02 { get{ return ageField; } set { if (value 0 value 130) { ageField value;//此处的value指的是当前属性PropertyAge02赋的值 } } } ​ //属性写法3lambda表达式 public int PropertyAge03 { get ageField; set ageField value; } }4 static静态​ /* static:::修饰符:::可以修饰成员变量普通方法 ​ //id name age都是个人的每个学生都有一套这样的数据 独立的 //jiangShi ysj gsadress这些是集体的不应该是每个学生都有一套 而是而是只有一份 大家共享 // 想让数据 只有一份 大家共享 可以在前面加static // //加static的成员类成员 不加static的成员实例成员(对象成员) //加static的成员变量类变量 不加static的成员变量实例变量 //加static的成员方法类方法 不加static的成员方法实例方法 // static成员在类加载时 加载进类的静态区域中 ​ 特点1static修饰的变量共享数据 不能通过对象直接调用 但可以通过对象的方法间接调用 可以通过类名直接调用 只有一份 特点2static修饰的方法类方法 不能通过对象直接调用 可以通过类名直接调用 静态方法中只能调用类成员 不能调用实例成员 ​ 思考把什么变量定义成静态变量 把什么方法定义成静态方法 共享的数据定义为静态变量 属于个人的数据定义为实例变量 当一个方法没有涉及到实例成员时 尽量定义为静态方法 可以不用创建对象 直接通过类名调用 */ internal class demo10_static静态的 { ​ static void Main(string[] args) { Console.WriteLine(Hello, World!); Console.ReadLine(); ​ // // //三个学生 Student10 s11 new Student10(1001,韩梅梅,19,苗天宝,饮水机1号,十六大街1); s11.show(); Student10 s12 new Student10(1002,韩寒,19,苗天宝,饮水机1号,十六大街2); s12.show(); Student10 s13 new Student10(1003,韩红,19,苗天宝,饮水机1号,十六大街3); ​ //s11.JiangShi 张三; //s12.JiangShi 张三; //s13.JiangShi 张三; s11.show(); s12.show(); s13.show(); ​ Student10.GSAddress 五大街;//通过类名调用static类变量 ​ s11.show(); s12.show(); s13.show(); ​ s11.haha(); Student10.hehe();//通过类名调用static类方法 ​ } ​ } //写一个类 描述本公司班级的学生 class Student10 { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } ​ //讲师 public static string JiangShi { get; set; } //饮水机 public static string YSJ { get; set; } //公司地址 public static string GSAddress { get; set; } ​ public Student10() { } public Student10(int id, string name, int age, string jiangShi, string ySJ, string gSAddress) { Id id; Name name; Age age; JiangShi jiangShi; YSJ ySJ; GSAddress gSAddress; } ​ public void show() { Console.WriteLine(${Id}:{Age}:{Name}:{JiangShi}:{GSAddress}:{YSJ}); } ​ public void haha() { show(); hehe(); Console.WriteLine(${Id}:{Age}:{Name}:{JiangShi}:{GSAddress}:{YSJ}); ​ } public static void hehe() { //静态方法中 只能调用静态成员 不能调用实例成员 // show(); hehe(); // Console.WriteLine(${Id}:{Age}:{Name}:{JiangShi}:{GSAddress}:{YSJ}); Console.WriteLine(${JiangShi}:{GSAddress}:{YSJ}); } }5 图书管理系统管理员登录系统 对图书进行增删改查 学生增删改查 借阅还书功能 登出功能 页面Console::WriteLine显示数据 ReadLine获取数据 存储数组 技术OOP ​ ​ 分析牵扯的对象类型管理员User 学生Student 图书Book 借阅记录Recode ​5.1 需求分析获取客户的需求和业务逻辑 管理员登录系统 对图书进行增删改查 学生增删改查 借阅还书功能 登出功能 形成一个文档5.2 技术选型选择技术平台框架 页面Console::WriteLine显示数据 ReadLine获取数据 存储数组 技术OOP5.3 原型设计基本页面设计出来5.4 数据库设计表 字段 约束 测试数据5.5 模块开发管理员模块登录 登出 图书模块增删改查 crud::create read update delete 学生模块增删改查 crud::create read update delete 记录模块借阅 还书 查看5.6 测试单元测试整体测试5.7 部署上线把项目发布 监控 排查5.8 更新维护添加新功能客流量激增6 开发6.1 实体类封装数据的类实体类一个实体类 对应一个数据库的表 User(管理员)uid uname upwd uLoginTime Book(图书): bid bname btype bcontent bcount bkuCun bstatus(状态) Student(学生):sid sname sex sclassName status(状态) Recode(借阅记录):rid sid bid rstartTime rendTime ​ 实体类组成属性要封装的数据 构造方法创建对象 给属性赋值 tostring方法获取一个字符串 包含了所有的数据信息 方便打印对象User​ public class User { // User(管理员)uid uname upwd uLoginTime public string Uid { get; set; } public string Uname { get; set; } public string Upwd { get; set; } public string ULoginTime { get; set; } //方便打印对象的属性信息 public string ToString() { return $User:Uid{Uid},Uname{Uname},Upwd{Upwd},ULoginTime{ULoginTime}; } public User() { } public User(string uid, string uname, string upwd, string uLoginTime) { Uid uid; Uname uname; Upwd upwd; ULoginTime uLoginTime; } }Book​ public class Book { //Book(图书) : bid bname btype bcontent bcount bkuCun bstatus(状态) public string Bid { get; set; } public string Bname { get; set; } public string Btype { get; set; } public string Bcontent { get; set; } public int Bcount { get; set; } public int BkuCun { get; set; } public int Bstatus { get; set; }//0冻结 1 可用 ​ public string ToString() { return $Book:Bid{Bid},Bname{Bname},Btype{Btype},Bcontent{Bcontent},Bcount{Bcount},BkuCun{BkuCun},Bstatus{Bstatus}; } ​ public Book() { } public Book(string bid, string bname, string btype, string bcontent, int bcount, int bkuCun, int bstatus) { Bid bid; Bname bname; Btype btype; Bcontent bcontent; Bcount bcount; BkuCun bkuCun; Bstatus bstatus; } }Student​ public class Student { //Student(学生) :sid sname sex sclassName status(状态) public string Sid { get; set; } public string Sname { get; set; } public string Sex { get; set; } public string SclassName { get; set; } public int Status { get; set; } public string ToString() { return $Student:Sid{Sid},Sname{Sname},Sex{Sex},SclassName{SclassName},Status{Status}; } ​ public Student() { } public Student(string sid, string name, string sex, string sclassName,int status) { this.Sid sid ; this.Sex sex ; this.SclassName sclassName ; this.Status status ; this.Sname sclassName ; } }Recode​ public class Recode { public string Rid { get; set; } public string Bid { get; set; } public string Sid { get; set; } public string Rstart { get; set; } public string Rend { get; set; } ​ public string ToString() { return $Recode:Rid{Rid},Bid{Bid},Sid{Sid},Rstart{Rstart},Rend{Rend}; } public Recode(string rid, string bid, string sid, string rstart, string rend) { this.Rid rid ; this.Bid bid ; this.Sid sid ; this.Rstart rstart ; this.Rend rend ; } }6.2 LibraryUtil中的初始化方法//定义数组模拟数据库 public static User[] users { get; set; } public static Student[] students { get; set; } public static Book[] books { get; set; } public static Recode[] recodes { get; set; } private static Random random new Random(); ​ //给数组设置初始数据 private static void InitArray() { users new User[10]; users[0]new User(u_001,张三,123,2025/7/16); users[1] new User(u_002, 李四, 123, 2025/7/16); users[2] new User(u_003, 王五, 123, 2025/7/16); users[3] new User(u_004, 赵六, 123, 2025/7/16); ​ students new Student[50]; for (int i 0; i 10; i) { string name Guid.NewGuid().ToString().Substring(0, 8);//随机一个8位的字符串 students[i] new Student( s_00 (i1), name, random.NextDouble() 0.5 ? 男 : 女, class_ i % 4 1, random.Next(0, 2)); } books new Book[100]; books[0] new Book(b_0011, 红楼梦, 文学, 一群美女每一个伪娘!, 10, 5, 1); books[1] new Book(b_0012, 三国演义, 文学, 三个国家打架!, 10, 5, 1); books[2] new Book(b_0013, 西游记, 文学, 一个秃头带三个妖怪!, 10, 5, 1); books[3] new Book(b_0014, 水浒传, 文学, 108个好汉!, 10, 5, 1); books[4] new Book(b_0015, 平凡的世界, 文学, 建议你看看!, 10, 5, 1); books[5] new Book(b_0016, 钢铁是怎样炼成的, 文学, 看不懂!, 10, 5, 1); books[6] new Book(b_0017, 十万个我为什么, 文学, 太幼稚!, 10, 5, 1); books[7] new Book(b_0018, 航洋世界, 科学, 海洋生物千奇百怪!, 10, 5, 1); books[8] new Book(b_0019, 雪山飞狐, 文学, 雪山飞狐 飞雪飘零!, 10, 5, 1); books[9] new Book(b_0010, 天龙八部, 文学, 九阴真经吸星大法!, 10, 5, 1); ​ recodes new Recode[200]; for (int i 0; i 20; i) { recodes[i] new Recode( r_00 i, b_00 random.Next(10,20), s_00 random.Next(1, 11), 2025/1/1, random.NextDouble()0.5?( 2024/(random.Next(1, 13)) / random.Next(1, 29)): ​ ); } }测试初始数据static void Main(string[] args) { Console.WriteLine(Hello, World!); ​ InitArray(); //Console.ReadLine(); for (int i 0; i books.Length; i) { if (books[i]!null) { Console.WriteLine(books[i].ToString()); } } for (int i 0; i students.Length; i) { if (students[i] ! null) { Console.WriteLine(students[i].ToString()); } } for (int i 0; i recodes.Length; i) { if (recodes[i] ! null) { Console.WriteLine(recodes[i].ToString()); } } for (int i 0; i users.Length; i) { if (users[i] ! null) { Console.WriteLine(users[i].ToString()); } } ​ }6.3 管理员登录登出欢迎页面的方法private static void welcome() { while (true) { ​ if (loginUser null) { Console.WriteLine(----------欢迎进入 Logo ----------); } else { Console.WriteLine(----------欢迎进入 Logo ,登陆者: loginUser.Uname ----------); } Console.WriteLine( 1 管理员登录); Console.WriteLine( 2 管理员登出); Console.WriteLine( 3 学生信息管理); Console.WriteLine( 4 图书信息管理); Console.WriteLine( 5 借阅记录管理); Console.WriteLine( 6 退出系统); Console.WriteLine(请输入您的选择(1-6):); int choose int.Parse(Console.ReadLine()); switch (choose) { case 1: UserLogin(); break; case 2: UserLogout(); break; case 3: StudentManager(); break; case 4: BookManager(); break; case 5: RecodeManager(); break; default: //退出系统 Environment.Exit(0); break; } } }登录private static void UserLogin() { if (loginUser!null) { Console.WriteLine(----已是登录状态如要切换账号请先登出); return; } Console.WriteLine($----------欢迎进入{Logo}:登录页面----------); Console.WriteLine(请输入您的账号); string nameConsole.ReadLine(); Console.WriteLine(请输入您的密码); string pwd Console.ReadLine(); User user null;//定义变量记录查询到的用户 //定义变量记录错误信息 string message ; //那账号和密码查数据库 for (int i 0; i users.Length; i) { if (users[i]!null) { if (users[i].Uname name) { user users[i]; } } } if (user null) { message 用户名不存在; } else { //判断密码是否正确 if (user.Upwd!pwd) { message 密码错误; } } Console.WriteLine(); if (message ! ) { Console.WriteLine(----登录失败错误信息是 message); } else { loginUser user; Console.WriteLine(----登录成功); } }登出private static void UserLogout() { Console.WriteLine(); if (loginUsernull) { Console.WriteLine(----没有登录无法登出); return; } loginUser null; Console.WriteLine(----登出成功); }6.4 学生管理学生信息管理页面private static void StudentManager() { //判断是否登录 if (loginUser null) { Console.WriteLine(----没有登录无法对学生进行增删改查); return; } Console.WriteLine($----------欢迎{loginUser.Uname}进入{Logo}:学生信息管理页面----------); Console.WriteLine( 1 查询所有); Console.WriteLine( 2 添加); Console.WriteLine( 3 修改); Console.WriteLine( 4 删除); Console.WriteLine( 5 回到欢迎页); Console.WriteLine( 6 退出系统); Console.WriteLine(请输入您的选择(1-6):); int choose int.Parse(Console.ReadLine()); switch (choose) { case 1: listStudent(); break; case 2: addStudent(); break; case 3: updateStudent(); break; case 4: deleteStudent(); break; case 5: welcome(); break; default: //退出系统 Environment.Exit(0); break; ​ } }学生列表//显示所有学生的信息 private static void listStudent() { Console.WriteLine($----------欢迎{loginUser.Uname}进入{Logo}:学生信息查询页面----------); Console.Write( 学号 *\t); Console.Write( 姓名 *\t); Console.Write( 班级 *\t); Console.Write( 性别 *\t); Console.Write( 状态\r\n); for (int i 0; i students.Length; i) { if (students[i] ! null) { Console.Write($ {students[i].Sid} \t); Console.Write($ {students[i].Sname} \t); Console.Write($ {students[i].SclassName} \t); Console.Write($ {students[i].Sex} \t); Console.Write( (students[i].Status0?冻结:正常) \r\n); } } StudentManager(); }学生添加private static void addStudent() { Console.WriteLine($----------欢迎{loginUser.Uname}进入{Logo}:学生信息添加页面----------); Console.WriteLine(请输入添加学生的名字); string nameConsole.ReadLine(); Console.WriteLine(请输入添加学生的性别); string sex Console.ReadLine(); Console.WriteLine(请输入添加学生的状态(0/1)0冻结 1正常); int status int.Parse( Console.ReadLine()); Console.WriteLine(请输入添加学生的班级(1-4)1:class_1, 2:class_2, 3:class_3, 4:class_4); int classId int.Parse(Console.ReadLine()); string className class_ classId; ​ //获取学生的学号编号 maxStudentId; Student student new Student(s_00 maxStudentId, name, sex, className, status); //装入数组中 students[maxStudentId-1]student; Console.WriteLine(----学生添加成功); listStudent(); }获取所有的学号//写一个方法 获取现有的所有学生的学号 private static string[] getAllSid() { //获取所有学生的个数 int count 0; for (int i 0; i students.Length; i) { if (students[i] ! null) { count; } } //创建数组 装所有的学生的sid string[] sidArr new string[count]; for (int i 0,j0; i students.Length; i) { if (students[i] ! null) { sidArr[j] students[i].Sid; j; } } return sidArr; }修改学生private static void updateStudent() { Console.WriteLine($----------欢迎{loginUser.Uname}进入{Logo}:学生信息修改页面----------); string message ; string[] idsArr getAllSid();//获取所有学生的学号 for (int i 0; i idsArr.Length; i) { message (i1) : idsArr[i] \t; } Console.WriteLine(请选择修改学生的学号(1- idsArr .Length )); //1:s_001 2:s_002 3 //显示所有学生的学号 Console.WriteLine(message); int idIndexint.Parse( Console.ReadLine()); Console.WriteLine(请输入修改后的名字); string name Console.ReadLine(); Console.WriteLine(请输入修改后的性别); string sex Console.ReadLine(); Console.WriteLine(请输入修改后的状态(0/1)0冻结 1正常); int status int.Parse(Console.ReadLine()); Console.WriteLine(请输入修改后的班级(1-4)1:class_1, 2:class_2, 3:class_3, 4:class_4); int classId int.Parse(Console.ReadLine()); string className class_ classId; ​ Student student new Student(idsArr[idIndex-1], name, sex, className, status); //更改数组中元素 for (int i 0; i students.Length; i) { if (students[i].Sidstudent.Sid) { students[i] student; break; } } Console.WriteLine(); Console.WriteLine(----学生修改成功); //显示学生列表 listStudent(); ​ }删除学生private static void deleteStudent() { Console.WriteLine($----------欢迎{loginUser.Uname}进入{Logo}:学生信息删除页面----------); string message ; string[] idsArr getAllSid();//获取所有学生的学号 for (int i 0; i idsArr.Length; i) { message (i 1) : idsArr[i] \t; } Console.WriteLine(请选择删除的学生学号(1- idsArr.Length )); //1:s_001 2:s_002 3 //显示所有学生的学号 Console.WriteLine(message); int idIndex int.Parse(Console.ReadLine()); //更改数组中元素 for (int i 0; i students.Length; i) { if (students[i].Sid idsArr[idIndex-1]) { students[i] null; break; } } Console.WriteLine(); Console.WriteLine(----学生删除成功); //显示学生列表 listStudent(); }

更多文章