C#--核心

news/2024/7/27 7:58:21/文章来源:https://blog.csdn.net/SunsetSunup/article/details/135034949

CSharp核心知识点学习

学习内容有:

绪论:面向对象的概念

Lesson1:类和对象

练习:

Lesson2:封装--成员变量和访问修饰符

练习:

Lesson3:封装--成员方法

Lesson4:封装--构造函数和析构函数

知识点四 垃圾回收机制--面试常考

练习:

Lesson5:成员属性

练习:

Lesson6:封装--索引器

练习:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Lesson6_练习
{#region 练习//自定义一个整形数组类,该类中有一个整形数组变量//为它封装增删查改的方法class IntArray{private int[] array;//房间容量private int capacity;//当前放了几个房间private int length;public IntArray(){capacity = 5;length = 0;array = new int[capacity];}//增public void Add(int value){//如果要增加就涉及扩容//扩容就涉及"搬家"if(length < capacity){array[length] = value;++length;}//扩容“搬家”else{capacity *= 2;//新房子int[] tempArray = new int[capacity];for (int i = 0; i < array.Length; i++){tempArray[i] = array[i];}//老的房子地址 指向新房子地址array = tempArray;//传入的往后放array[length] = value;++length;}}//删//指定内容删除public void Remove(int value){//找到 传入值 在哪个位置for (int i = 0; i < length; i++){if(array[i] == value){RemoveAt(i);return;}}Console.WriteLine("没有在数组中找到{0}",value);}//指定索引删除public void RemoveAt(int index){if(index > length){Console.WriteLine("当前数组只有{0},你越界了",length);return;}for (int i = index; i < length - 1; i++){array[i] = array[i + 1];}--length;}//查改public int this[int index]{get{if(index >= length || index < 0){Console.WriteLine("越界了");return 0;}return array[index];}set{if (index >= length || index < 0){Console.WriteLine("越界了");}array[index] = value;}}public int Length{get{return length;}}}#endregionclass Program{static void Main(string[] args){Console.WriteLine("索引器练习");IntArray intArray = new IntArray();intArray.Add(1);intArray.Add(2);Console.WriteLine(intArray.Length);//intArray.RemoveAt(1);intArray.Remove(2);Console.WriteLine(intArray[0]);//Console.WriteLine(intArray[1]);Console.WriteLine(intArray.Length);}}
}

Lesson7:封装--静态成员

练习:

Lesson8:封装-- 静态类和静态构造函数

Lesson9:封装--拓展方法

练习:

Lesson10:封装--运算符重载

练习:

Lesson11:封装--内部类和分部类

Lesson12:继承--继承的基本规则

练习:

Lesson13:继承--里氏替换原则

练习:

Lesson14:继承--继承中的构造函数

练习:

Lesson15:继承--万物之父和装箱拆箱

练习:

Lesson16:继承--密封类

练习:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Lesson16_练习
{#region 练习//定义一个载具类//有速度,最大速度,可乘人数,司机和乘客等,有上车,下车,行驶,车祸等方法//用载具类申明一个对象,并将若干人装载上车class Car{public int speed;public int maxSpeed;//当前装了多少人public int num;public Person[] person;public Car(int speed, int maxSpeed, int num){this.speed = speed;this.maxSpeed = maxSpeed;this.num = 0;person = new Person[num];}public void GetIn(Person p){if(num >= person.Length){Console.WriteLine("满载了");return;}person[num] = p;++num;}public void GetOff(Person p){for (int i = 0; i < person.Length; i++){if(person[i] == null){break;}if (person[i] == p){//移动位置for (int j = i; j < num - 1; j++){person[j] = person[j + 1];}//最后一个位置清空person[num - 1] = null;--num;break;}}}public void Move(){}public void Boom(){}}class Person{}class Driver : Person{}class Passenger : Person{}#endregionclass Program{static void Main(string[] args){Console.WriteLine("封装继承综合练习");Car car = new Car(80, 100, 20);Driver driver = new Driver();Passenger p1 = new Passenger();Passenger p2 = new Passenger();Passenger p3 = new Passenger();car.GetIn(driver);car.GetIn(p1);car.GetIn(p2);car.GetIn(p3);car.GetOff(p1);}}
}

练习调试!!!

Lesson17:多态--vob

通过这节课的知识--解决了Lesson13 的练习题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Lesson13_练习
{#region 练习一//is和as的区别//is 用于判断 类对象判断是不是某个类型的对象  bool//as 用于转换 把一个类对象 转换成一个指定类型的对象  null#endregion#region 练习二//写一个Monster类,它派生出Boss和Gobin两个类//Boss有技能,小怪有攻击//随机生成10个怪,装载到数组中//遍历这个数组,调用他们的攻击方法,如果是boss就释放技能class Monster{}class Boss : Monster{public void Skill(){Console.WriteLine("Boss释放技能");}}class Goblin : Monster{public void Atk(){Console.WriteLine("小怪攻击");}}#endregion#region 练习三//FPS游戏模拟//写一个玩家类,玩家可以拥有各种武器//线在有四种武器,冲锋枪,散弹枪,手枪,匕首//玩家默认拥有匕首//请在玩家类中写一个方法,可以拾取不同的武器替换自己拥有的武器class Weapon{public virtual string Input(){return null;}}/// <summary>/// 冲锋枪/// </summary>class SubmachineGun : Weapon{public override string Input(){return "冲锋枪";//Console.WriteLine("冲锋枪");}}/// <summary>/// 散弹枪/// </summary>class ShotGun : Weapon{public override string Input(){return "散弹枪";//Console.WriteLine("散弹枪");}}/// <summary>/// 手枪/// </summary>class Pistol : Weapon{public override string Input(){return "手枪";//Console.WriteLine("手枪");}}/// <summary>/// 匕首/// </summary>class Dagger : Weapon{public override string Input(){return "匕首";//Console.WriteLine("匕首");}}class Player{private Weapon nowHaveWeapon;public Player(){nowHaveWeapon = new Dagger();}public void PickUp(Weapon weapon){nowHaveWeapon = weapon;}public void Show(){Console.WriteLine("当前玩家使用的武器是:{0}",nowHaveWeapon.Input());}}#endregionclass Program{static void Main(string[] args){Console.WriteLine("里氏替换原则练习");//随机生成10个怪物Monster[] objects = new Monster[10];Random r = new Random();int box = 0;for (int i = 0; i < objects.Length; i++){box = r.Next(1, 3);if(box == 1){objects[i] = new Boss();}else{objects[i] = new Goblin();}}//遍历数组for (int i = 0; i < objects.Length; i++){if(objects[i] is Boss){(objects[i] as Boss).Skill();}else if(objects[i] is Goblin){(objects[i] as Goblin).Atk();}}Player p = new Player();p.Show();Weapon submachineGun = new SubmachineGun();p.PickUp(submachineGun);p.Show();//遗留一个问题:重写?!!//通过学习Lesson17--多态ovb    得以解决}}
}

练习:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Lesson17_练习
{#region 练习一//真的鸭子嘎嘎叫,木头鸭子吱吱叫,橡皮鸭子唧唧叫class Duck{public virtual void Speak(){Console.WriteLine("嘎嘎叫");}}class WoodDuck : Duck{public override void Speak(){//base.Speak();Console.WriteLine("吱吱叫");}}class RubberDuck : Duck{public override void Speak(){//base.Speak();Console.WriteLine("唧唧叫");}}#endregion#region 练习二//所有员工9点打卡//但经理十一点打卡,程序员不打卡class Staff{public virtual void PunchTheClock(){Console.WriteLine("普通员工9点打卡");}}class Manager : Staff{public override void PunchTheClock(){//base.PunchTheClock();Console.WriteLine("经理11点打卡");}}class Programmer : Staff{public override void PunchTheClock(){//base.PunchTheClock();Console.WriteLine("程序员不打卡");}}#endregion#region 练习三//创建一个图形类,有求面积和周长两个方法//创建矩形类,正方形类,圆形类继承图形类//实例化矩形,正方形,圆形对象求面积和周长class Graph{public virtual float GetLength(){return 0;}public virtual float GetArea(){return 0;}}class Rect : Graph{private float w;private float h;public Rect(float w, float h){this.w = w;this.h = h;}public override float GetLength(){//return base.GetLength();return 2 * (w + h);}public override float GetArea(){//return base.GetArea();return w * h;}}class Square : Graph{private float h;public Square(float h){this.h = h;}public override float GetLength(){//return base.GetLength();return 4 * h;}public override float GetArea(){//return base.GetArea();return h * h;}}class Circule : Graph{private float r;public Circule(float r){this.r = r;}public override float GetLength(){//return base.GetLength();return 2 * 3.14F * r;}public override float GetArea(){//return base.GetArea();return 3.14f * r * r;}}#endregionclass Program{static void Main(string[] args){Console.WriteLine("多态ovb");Console.WriteLine("virtual   override   base");Duck d = new Duck();d.Speak();Duck wd = new WoodDuck();wd.Speak();Duck rd = new RubberDuck();rd.Speak();Staff s = new Staff();s.PunchTheClock();Staff ma = new Manager();ma.PunchTheClock();(ma as Manager).PunchTheClock();Staff pr = new Programmer();(pr as Programmer).PunchTheClock();Rect r = new Rect(2, 4);Console.WriteLine("矩形的周长是:" + r.GetLength());Console.WriteLine("矩形的面积是:" + r.GetArea());Square sq = new Square(4);Console.WriteLine("正方形的周长是:" + sq.GetLength());Console.WriteLine("正方形的面积是:" + sq.GetArea());Circule ci = new Circule(3);Console.WriteLine("圆的周长是:" + ci.GetLength());Console.WriteLine("圆的面积是:" + ci.GetArea());}}
}

Lesson18:多态--抽象类和抽象方法

练习:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Lesson18_练习
{#region 练习一//写一个动物抽象类,写三个子类//人叫、狗叫、猫叫abstract class Animal{public abstract void Speak();}class Person : Animal{public override void Speak(){Console.WriteLine("人在说话");}}class Dog : Animal{public override void Speak(){Console.WriteLine("狗在汪汪叫");}}class Cat : Animal{public override void Speak(){Console.WriteLine("猫在喵喵叫");}}#endregion#region 练习二//创建一个图形类,有求面积和周长两个方法//创建矩形类,正方形类,圆形类继承图形类//实例化矩形,正方形,圆形对象求面积和周长abstract class Graph{public abstract float GetLength();public abstract float GetArea();}class Rect : Graph{private float w;private float h;public Rect(float w, float h){this.w = w;this.h = h;}public override float GetLength(){//return base.GetLength();return 2 * (w + h);}public override float GetArea(){//return base.GetArea();return w * h;}}class Square : Graph{private float h;public Square(float h){this.h = h;}public override float GetLength(){//return base.GetLength();return 4 * h;}public override float GetArea(){//return base.GetArea();return h * h;}}class Circule : Graph{private float r;public Circule(float r){this.r = r;}public override float GetLength(){//return base.GetLength();return 2 * 3.14F * r;}public override float GetArea(){//return base.GetArea();return 3.14f * r * r;}}#endregionclass Program{static void Main(string[] args){Console.WriteLine("抽象类和抽象方法");Person p = new Person();p.Speak();Dog dog = new Dog();dog.Speak();Animal cat = new Cat();cat.Speak();}}
}

Lesson19:多态--接口

练习:重要,加深对接口的应用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Lesson19_练习
{#region 练习一//人、汽车、房子都需要登记,人需要到派出所登记,汽车需要去车管所登记,房子需要去房管局登记//使用接口实现登记方法interface IRegister{void Register();}class Person : IRegister{public void Register(){Console.WriteLine("派出所登记");}}class Car : IRegister{public void Register(){Console.WriteLine("车管所登记");}}class Home : IRegister{public void Register(){Console.WriteLine("房管局登记");}}#endregion#region 练习二//麻雀、鸵鸟、企鹅、鹦鹉、直升机、天鹅//直升机和部分鸟能飞//鸵鸟和企鹅不能飞//企鹅和天鹅能游泳//除直升机,其他都能走//请用面向对象相关知识实现abstract class Bird{public abstract void Walk();}interface IFly{void Fly();}interface ISwimming{void Swimming();}//麻雀class Sparrow : Bird,IFly{public void Fly(){}public override void Walk(){}}//鸵鸟class Ostrich : Bird{public override void Walk(){}}//企鹅class Penguin : Bird,ISwimming{public void Swimming(){throw new NotImplementedException();}public override void Walk(){}}//鹦鹉class Parrot : Bird,IFly{public void Fly(){throw new NotImplementedException();}public override void Walk(){}}//天鹅class Swan : Bird,IFly,ISwimming{public void Fly(){throw new NotImplementedException();}public void Swimming(){throw new NotImplementedException();}public override void Walk(){}}//直升机class Helicopter : IFly{public void Fly(){throw new NotImplementedException();}}#endregion#region 练习三//多态来模拟移动硬盘、U盘、MP3插到电脑上读取数据//移动硬盘与U盘都属于存储设备//MP3属于播放设备//但他们都能插在电脑上传输数据//电脑提供一个USB接口//请实现电脑的传输数据的功能interface IUSB{void ReadData();}class StorageDevice : IUSB{public string name;public StorageDevice(string name){this.name = name;}public void ReadData(){Console.WriteLine("{0}传输数据",name);}}class MP3 : IUSB{public void ReadData(){Console.WriteLine("MP3传输数据");}}class Computer{public IUSB usb1;}#endregionclass Program{static void Main(string[] args){Console.WriteLine("接口练习");IRegister p = new Person();p.Register();Car car = new Car();car.Register();Home home = new Home();home.Register();StorageDevice hd = new StorageDevice("移动硬盘");StorageDevice ud = new StorageDevice("U盘");MP3 mp3 = new MP3();Computer cp = new Computer();cp.usb1 = hd;cp.usb1.ReadData();cp.usb1 = ud;cp.usb1.ReadData();cp.usb1 = mp3;cp.usb1.ReadData();}}
}

Lesson20:多态--密封方法

Lesson21:面向对象相关--命名空间

练习:

Lesson22:面向对象相关--万物之父中的方法

练习:

Lesson23:面向对象相关--string

练习:

Lesson24:面向对象相关--StringBuilder

练习:

Lesson25:面向对象相关--结构体和类的区别

Lesson26:面向对象相关--抽象类和接口的区别

学习完成:

CSharp核心实践小项目见下一篇笔记!

牢记--多看!!!

你还有好多东西要学习呢,抓紧时间啊!!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_925459.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

git主分支合并到本地分支起冲突的解决办法

1.原因&#xff1a;一个项目几个人共同承担&#xff0c;难免有交叉的部分&#xff0c;这个时候交叉部分的提交就会导致冲突 2.方法&#xff1a;点开该类手动进行更改&#xff1a; 一般会有两种情况&#xff1a;一个是更改&#xff0c;一个是冲突(IDEA右上角会显示)&#xff1b;…

meter报OOM错误,如何解决?

根据在之前的压测过程碰到的问题&#xff0c;今天稍微总结总结&#xff0c;以后方便自己查找。 一、单台Mac进行压测时候&#xff0c;压测客户端Jmeter启动超过2000个线程&#xff0c;Jmeter报OOM错误&#xff0c;如何解决&#xff1f; 解答&#xff1a;单台Mac配置内存为8G&…

详解java中ArrayList

目录 前言 一、ArrayList是什么 二、ArrayList使用 1、ArrayList的构造 2 、ArrayList常见操作 3、 ArrayList的遍历 4、 ArrayList的扩容机制 三、来个练习 前言 当你看到这篇文章我觉得很好笑&#xff0c;因为我开始也不懂ArrayList现在轮到你了&#xff0c;嘻嘻嘻&am…

GRE隧道(初级VPN)配置步骤

一、拓朴图&#xff1a; 要求&#xff1a;1、PC1 和 PC2 能访问充当互联网接口地址的ISP环回口地址8.8.8.8 2、PC1 和 PC2 走GRE隧道互通 二、配置步骤&#xff1a; 1、配置IP 2、R1、R2 配置nat&#xff0c;代理内网地址通过G0/0/0口上外网 acl 2000rule permit source a…

Nginx配置负载均衡实例

Nginx配置反向代理实例二 提醒一下&#xff1a;下面实例讲解是在Mac系统演示的&#xff1b; 负载均衡实例实现的效果 浏览器地址栏输入地址http://192.168.0.101/test/a.html&#xff0c;刷新页面进行多次请求&#xff0c;负载均衡效果&#xff0c;平均分配到8080端口服务和8…

iOS解决内存泄漏工具

在iOS应用中&#xff0c;有一些工具和技术可以帮助识别和解决内存泄漏问题。以下是一些常用的工具&#xff0c;希望对大家有所帮助。北京木奇移动技术有限公司&#xff0c;专业的软件外包开发公司&#xff0c;欢迎交流合作。 1.Instruments&#xff1a; Instruments 是Xcode中…

分布式搜索引擎--认识

elasticsearch的作用 elasticsearch是一款非常强大的开源搜索引擎&#xff0c;具备非常多强大功能&#xff0c;可以帮助我们从海量数据中快速找到需要的内容 。 elasticsearch结合kibana、Logstash、Beats&#xff0c;也就是elastic stack&#xff08;ELK&#xff09;。被广泛…

行为型设计模式——备忘录模式

备忘录模式 备忘录模式提供了一种状态恢复的实现机制&#xff0c;使得用户可以方便地回到一个特定的历史步骤&#xff0c;当新的状态无效或者存在问题时&#xff0c;可以使用暂时存储起来的备忘录将状态复原&#xff0c;很多软件都提供了撤销&#xff08;Undo&#xff09;操作…

使用mamba替换conda和anaconda配置环境安装软件

使用mamba替换miniconda和anaconda&#xff0c;原因是速度更快&#xff0c;无论是创建新环境还是激活环境 conda、mamba、anaconda都是蟒蛇的意思… 下载mambaforge wget https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-Linux-x86_64.sh ba…

基于Matlab/Simulink开发自动驾驶的解决方案

文章目录 处理自动驾驶数据 仿真自动驾驶场景 设计感知算法 设计规划和控制算法 生成代码和部署算法 集成和测试 参考文献 使用 MATLAB/Simulink开发自动驾驶&#xff0c;能够深入建模真实世界的行为、减少车辆测试并验证嵌入式软件的功能&#xff0c;从而推进自动驾驶感…

Mybatis 常用条件语句,大于小于、if、for、模糊搜索、case when、choose

大于小于 方法1&#xff1a; > 大于 &#xff0c; < 小于 <if test"startTime ! null ">and a.create_time > #{startTime} </if> <if test"endTime ! null ">and a.create_time < #{endTime} </if> 方法2(建议写这…

java每日一题——ATM系统编写(答案及编程思路)

前言&#xff1a; 基础语句学完&#xff0c;也可以编写一些像样的程序了&#xff0c;现在要做的是多加练习&#xff0c;巩固下知识点&#xff0c;打好基础&#xff0c;daydayup! 题目&#xff1a;模仿银行ATM系统&#xff0c;可以创建用户&#xff0c;存钱&#xff0c;转账&…

一、MySQL 卸载

目录 1、软件的卸载准备 2、软件的卸载 方式一&#xff1a;通过控制面板卸载 方式二&#xff1a;通过mysql8的安装向导卸载 1、双击mysql8的安装向导 2、取消更新 3、选择要卸载的mysql服务器软件的具体版本 4、确认删除数据目录 5、执行删除 6、完成删除 3、清理残…

半小时实现GPT纯血鸿蒙版

仅需半小时&#xff0c;即可实现纯血鸿蒙版本的ChatGPT&#xff01; 废话少说&#xff0c;先看效果图&#xff1a; 如上图所示&#xff0c;这个小Demo实现了AI智能问答。靠右加粗的文本是用户点击底部提交按钮后出现的&#xff1b;后面靠左对齐的普通文本是来自AI的回答内容。当…

鸿蒙(HarmonyOS)应用开发指南

1. 概述 1.1 简介 鸿蒙&#xff08;即 HarmonyOS &#xff0c;开发代号 Ark&#xff0c;正式名称为华为终端鸿蒙智能设备操作系统软件&#xff09;是华为公司自 2012 年以来开发的一款可支持鸿蒙原生应用和兼容 AOSP 应用的分布式操作系统。该系统利用“分布式”技术将手机、电…

第十七周周报

文章目录 摘要目标检测锚框交并比NMS 非极大值抑制输出 文献阅读&#xff1a;SMPL: A Skinned Multi-Person Linear ModelIntroductionRelated WorkModel FormulationTraining评估动态SMPL讨论结论 总结 摘要 本周看了三维人体重建的领域&#xff0c;看了一篇SMPL的文章&#…

【sqlite3】sqlite3在linux下使用sqlitebrowser工具实现数据可视化

sqlite3在linux下使用sqlitebrowser工具实现数据可视化 1. ### install sqlitebrowser 1. ### install sqlitebrowser 安装指令 sudo apt-get install sqlitebrowser通过工具打开数据库 sqlitebrowser stereo.db打开效果

【蓝桥杯日记】第一篇——如何搭建系统环境

目录 前言 环境相关文件 学生机环境-Web应用开发环境&#xff08;第十五届大赛&#xff09; 学生机环境-Java编程环境&#xff08;第十五届大赛&#xff09; 学生机环境-C/C编程环境&#xff08;第十五届大赛&#xff09; 学生机环境-Python编程环境 &#xff08;第十五届…

Spring Boot - Application Events 同步 VS 异步 发布订阅事件实战

文章目录 PreCode基础工程启动类切入口事件 发布事件同步 Listener异步Listener增加EnableAsync增加 Async 测试 Pre Spring Boot - Application Events 的发布顺序_ApplicationStartingEvent Spring Boot - Application Events 的发布顺序_ApplicationEnvironmentPreparedEv…

IntelliJ IDEA - 快速去除 mapper.xml 告警线和背景(三步走)

1、去掉 No data sources configure 警告 Settings&#xff08;Ctrl Alt S&#xff09; ⇒ Editor ⇒ Inspections ⇒ SQL ⇒ No data sources configure 2、去掉 SQL dialect is not configured 警告 Settings&#xff08;Ctrl Alt S&#xff09; ⇒ Editor ⇒ Inspecti…