C#--核心

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.mzph.cn/news/628289.shtml

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

相关文章

在 .NET 中使用可以漫游的 Web 凭据

Windows 凭据管理器是一个内置在 Windows 操作系统中的功能&#xff0c;为用户提供一种安全的方式来存储和管理凭据。本文主要介绍如何在 .NET 中使用可以漫游的 Web 凭据&#xff0c;以及使用中的基本事项。 1. 引言 在前面的文章《试用 Windows Terminal 中的 Terminal Chat…

FPGA时序分析与时序约束(四)——时序例外约束

目录 一、时序例外约束 1.1 为什么需要时序例外约束 1.2 时序例外约束分类 二、多周期约束 2.1 多周期约束语法 2.2 同频同相时钟的多周期约束 2.3 同频异相时钟的多周期约束 2.4 慢时钟域到快时钟域的多周期约束 2.5 快时钟域到慢时钟域的多周期约束 三、虚假路径约…

Redis实现延迟任务队列(一)

业务需求 业务里面需要文章的定时发布功能&#xff0c;因此打算采用mq和redis来实现一下定时发布的功能。mq之前用过了。基于一些私信交换机地信息过期策略实现。所以这次采用redis。并且打算将这个延迟任务的服务集成在一个微服务里面&#xff0c;提供对外的feign的远程调用接…

线性代数基础【4】线性方程组

第四章 线性方程组 一、线性方程组的基本概念与表达形式 二、线性方程组解的基本定理 定理1 设A为mXn矩阵,则 (1)齐次线性方程组AX0 只有零解的充分必要条件是r(A)n; (2)齐次线性方程组AX0 有非零解(或有无数个解)的充分必要条件是r(A)&#xff1c;n 推论1 设A为n阶矩阵,则…

GPT2 GPT3

what is prompt 综述1.Pre-train, Prompt, and Predict: A Systematic Survey of Prompting Methods in Natural Language Processing(五星好评) 综述2. Paradigm Shift in Natural Language Processing(四星推荐) 综述3. Pre-Trained Models: Past, Present and Future Pro…

网络安全的威胁PPT

建议的PPT免费模板网站&#xff1a;http://www.51pptmoban.com/ppt/ 此PPT模板下载地址&#xff1a;https://file.51pptmoban.com/d/file/2023/03/20/1ae84aa8a9b666d2103f19be20249b38.zip 内容截图&#xff1a;

day07打卡

day07打卡 454. 四数相加 II 时间复杂度&#xff1a;O(N)&#xff0c;空间复杂度&#xff1a;O(N) 第一想法&#xff1a;创建一个哈希表&#xff0c;存下nums[i] nums[j]&#xff0c;再遍历nums3和nums4得到nums[k]nums[l]&#xff0c;从哈希表中找0-nums[k] - nums[l]即可…

Linux命令之服务器的网络配置hostname,sysctl,ifconfig,service,ifdown,ifup,route,ping的使用

1、查看当前主机名称&#xff0c;编辑配置文件修改主机名为你姓名拼音的首字母&#xff08;如张三&#xff0c;则为zs&#xff09; 2、查看本机网卡IP地址&#xff0c;编辑/etc/sysconfig/network-scripts/ifcfg-ens33&#xff0c;要求在一块物理网卡上绑定2个IP地址&#xff0…

Invalid bound statement (not found)(xml文件创建问题)

目录 解决方法&#xff1a; 这边大致讲一下我的经历&#xff0c;不想看的直接点目录去解决方法 今天照着老师视频学习&#xff0c;中间老师在使用动态SQL时&#xff0c;直接复制了一份&#xff0c;我想这么简单的一个&#xff0c;我直接从网上找内容创建一个好了&#xff0c;…

vue前端开发自学,借助KeepAlive标签保持组件的存活

vue前端开发自学,借助KeepAlive标签保持组件的存活&#xff01;如果不想让组件在切换的时候&#xff0c;被默认操作&#xff08;卸载掉了&#xff09;。他们需要使用这个这个表情哦。 下面给大家看看代码情况。 <template><h3>ComA</h3><p>{{ messag…

【Linux基础】Linux对时配置

Linux对时配置 ntp配置文件ntp.conf解析&#xff1a; &#xff08;1&#xff09;配置上层server 利用 server 关键字设定上层 NTP 服务器&#xff0c;上层 NTP 服务器的设定方式为&#xff1a; server [IP or hostname] [prefer]在 server 后端可以接 IP 或主机名&#xff…

谷歌裁员千人,搅动硅谷!终身编程终结,我们何以苟活?

新年第一个月&#xff0c;硅谷爆发了新一轮裁员潮。在这波浪潮中&#xff0c;有消息称谷歌计划裁员千人&#xff0c;另有Meta、Unity、Discord等多家公司也陆续放出了裁员的消息。就当前的就业环境来说&#xff0c;技术人员似乎面临着极其严峻的考验。 过去的一年间&#xff0c…

Qt第二周周二作业

代码&#xff1a; widget.h #ifndef WIDGET_H #define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACEclass Widget : public QWidget {Q_OBJECTpublic:Widget(QWidget *parent nullptr);~Widget();void paintEvent(…

【下云】旧笔记本实现私人服务器

背景&缘由&想法 背景&#xff1a; 自己是做Java的&#xff0c;做互联网或者说学计算机的都知道&#xff0c;近几年大环境太差&#xff0c;人却越来越多&#xff0c;造成行业越来越卷&#xff1b;针对Java来说&#xff0c;被迫要学习多方面的知识&#xff0c;工作拧螺…

第七在线荣获百灵奖 Buylink Awards 2023零售圈年度卓越服务商品牌

1月11日&#xff0c;由零售圈主办、20零售连锁协会协办、30零售行业媒体支持的中国零售圈大会暨2024未来零售跨年盛典在西安落下帷幕&#xff0c;在这个零售行业盛典中&#xff0c;第七在线凭借其高精尖产品和卓越的服务质量成功入选&#xff0c;并荣获了“百灵奖 Buylink Awar…

腾讯云优惠券怎样领取?附最新优惠券领取教程

腾讯云优惠券是腾讯云推出的一种优惠活动&#xff0c;通常包含代金券和折扣券两种形式&#xff0c;可以在购买腾讯云产品结算时抵扣部分费用或享受特定折扣&#xff0c;帮助用户降低购买腾讯云产品的成本。 一、腾讯云优惠券类型 1、代金券&#xff1a;代金券可以在购买腾讯云…

workflow源码解析:ThreadTask

1、使用程序&#xff0c;一个简单的加法运算程序 #include <iostream> #include <workflow/WFTaskFactory.h> #include <errno.h>// 直接定义thread_task三要素 // 一个典型的后端程序由三个部分组成&#xff0c;并且完全独立开发。即&#xff1a;程序协议算…

Python 循环结构之for循环结合range()函数使用技巧

在Python中for循环经常结合range()函数一起使用。 range()函数是一个内置函数&#xff0c;用于生成一个整数序列&#xff0c;通常与for循环结合使用。它的常见用法是生成一系列连续的整数&#xff0c;可以指定起始值、结束值和步长。 range()函数的语法如下&#xff1a; ran…

【.NET Core】C#预处理器指令

【.NET Core】C#预处理器指令 文章目录 【.NET Core】C#预处理器指令一、概述二、可为空上下文&#xff08;#nullable&#xff09;三、条件编译2.1 定义DEBUG是编译代码2.2 未定义MYTEST时&#xff0c;将编译以下代码 四、定义符号五、定义区域六、错误和警告信息 一、概述 预…

【深度学习】动手学深度学习(PyTorch版)李沐 2.4.3 梯度【公式推导】

2.4.3. 梯度 我们可以连接一个多元函数对其所有变量的偏导数&#xff0c;以得到该函数的梯度&#xff08;gradient&#xff09;向量。 具体而言&#xff0c;设函数 f : R n → R f:\mathbb{R}^{n}\to\mathbb{R} f:Rn→R的输入是一个 n n n维向量 x ⃗ [ x 1 x 2 ⋅ ⋅ ⋅ x n …