C#基础知识(以宝马,车,车轮为例)

2023-11-05 02:21

本文主要是介绍C#基础知识(以宝马,车,车轮为例),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

派生类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ClassLibrary1.util;
using ClassLibrary1.util.util2;namespace ClassLibrary1
{//声明代理类public delegate void MyEvent();public class Class1{//enum是C#的关键字,定义枚举类型, Enum 是C#的一个类enum color:int {red=1, blue=2, white=3, gray=4, yellow=5};static void Main(string[] args){//ReadOrWriteFile();//BaseTest();//TestEnum();//ExceptionDeal();//varStringIsNull();// DeleGateTest();Class1 c = new Class1();c.TestEvent();Console.ReadKey();}/// <summary>/// 类,抽象类以及接口/// </summary>private static void varStringIsNull(){//String s = "";//bool b = StringUtil.IsNullOrEmpty(s);//Console.WriteLine(b);//Cars mycar = new Cars();//mycar.Name1 = "BMW";//mycar.Number1 = "888888";//Console.WriteLine("我是{0},牌号为{1}.", mycar.Name1, mycar.Number1);//基类和派生类//Bmw bmw = new Bmw();//bmw.Name1 = "BMW";//bmw.Number1 = "888888";//Console.WriteLine("我是{0},牌号为{1}.", bmw.Name1, bmw.Number1);//bmw.Country = "Germany";//Console.WriteLine("生产地:{0}", bmw.Country);//Console.WriteLine(bmw.getReturnValue());//抽象类 和 接口Bmw bmw = new Bmw();bmw.Name1 = "BMW";bmw.Number1 = "888888";Console.WriteLine("我是{0},牌号为{1}.", bmw.Name1, bmw.Number1);bmw.Country = "Germany";bmw.Count = 4;Console.WriteLine("生产地:{0}", bmw.Country);Console.WriteLine(bmw.getReturnValue());Console.WriteLine("轮子数目:{0}", bmw.getWheelCount());bmw.createCar();bmw.useCar();bmw.destroyCar();}/// <summary>/// 声明一个委托类/// </summary>public delegate void MyDel(String msg);/// <summary>/// 声明调用委托/// </summary>/// <param name="msg"></param>public static void DelMessage(String msg){Console.WriteLine(msg);}/// <summary>/// 委托测试方法/// </summary>public static void DeleGateTest(){MyDel del = DelMessage;del("I am a delegate.");}public event MyEvent DelEvent;//定义事件,绑定代理/// <summary>/// 事件触发方法/// </summary>public virtual void FireEvent(){if (DelEvent != null){DelEvent();}}/// <summary>/// 事件测试方法/// </summary>public  void TestEvent(){DelEvent += new MyEvent(Test2Event);//注册FireEvent();//触发事件
        }/// <summary>/// 事件处理函数/// </summary>public void Test2Event(){Console.WriteLine("事件处理");}/// <summary>/// 捕获异常/// </summary>private static void ExceptionDeal(){int x = 1;int y = 0;try{int a = x / y;}catch (Exception e){Console.WriteLine(e.ToString());}}/// <summary>/// 枚举类型/// </summary>private static void TestEnum(){Console.WriteLine(color.blue);color c = color.red;switch (c){case color.blue: Console.WriteLine(color.blue); break;case color.gray: Console.WriteLine(color.gray); break;default: Console.WriteLine("other"); break;}String[] s = { "a", "b" };foreach (String e in s){Console.WriteLine(e);}}/// <summary>/// 基础测试/// </summary>private static void BaseTest(){//float f = 12.23F;//long  a = 1266666777777777863L;//String str = @"D:\files\temp\a.txt\t";//逐字符串:按原样输出//String str1 = "D:\files\temp\a.txt\t";//String str2 = @"""";//String str3 = """";编译时报错//Console.WriteLine(f);//Console.WriteLine(a);//Console.WriteLine("\v");//Console.WriteLine(str);//Console.WriteLine(str1);//Console.WriteLine(str2);//Console.WriteLine(str3);//String str = "XiaoLi";//Console.WriteLine("My name is {0}, how are you?", str);//字符串格式化, 参数化//字符串比较,可以直接用==号,也可以用Equals//String s1 = "hello,world";//String s2 = "hello,world";//if (s1 == s2)//{//    Console.WriteLine("相等");//}//if (s1.Equals(s2))//{//    Console.WriteLine("相等");//}//else//{//    Console.WriteLine("不相等");//}//值比较, 也可判断是否指向同一对象//String s1 = "hello,world";//String s2 = "hello,wor2ld";//if (s1.CompareTo(s2) <= 0)//{//    Console.WriteLine("指向同一对象");//}//else//{//    Console.WriteLine("不是指向同一对象");//}
}/// <summary>/// 读写文件/// </summary>private static void ReadOrWriteFile(){//@string path = @"e:\MyTest.txt";// Delete the file if it exists.if (File.Exists(path)){File.Delete(path);}//Create the file.//using using (FileStream fs = File.Create(path)){AddText(fs, "This is some text");AddText(fs, "This is some more text,");AddText(fs, "\r\nand this is on a new line");AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n");for (int i = 1; i < 120; i++){AddText(fs, Convert.ToChar(i).ToString());//Split the output at every 10th character.if (Math.IEEERemainder(Convert.ToDouble(i), 10) == 0){AddText(fs, "\r\n");}}}//Open the stream and read it back.using (FileStream fs = File.OpenRead(path)){byte[] b = new byte[1024];UTF8Encoding temp = new UTF8Encoding(true);while (fs.Read(b, 0, b.Length) > 0){Console.WriteLine(temp.GetString(b));}}}/// <summary>/// 写文件/// </summary>/// <param name="fs"></param>/// <param name="value"></param>private static void AddText(FileStream fs, string value){byte[] info = new UTF8Encoding(true).GetBytes(value);fs.Write(info, 0, info.Length);}}
}
工具类
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 
 5 
 6 namespace ClassLibrary1.util
 7 {
 8     class StringUtil
 9     {
10         public static bool IsNullOrEmpty(String str)
11         {
12             if (str == null || "".Equals(str)) return true;
13             return false;
14         }
15     }
16 }
基类
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using ClassLibrary1.util.util2;
 6 
 7 namespace ClassLibrary1.util.util2
 8 {
 9     class Cars:Wheels
10     {
11         private String Name;
12 
13         public String Name1
14         {
15             get { return Name; }
16             set { Name = value; }
17         }
18         private String Number;
19 
20         public String Number1
21         {
22             get { return Number; }
23             set { Number = value; }
24         }
25         public Cars()
26         {
27             Console.WriteLine("调用基类构造方法:{0}", DateTime.Now);
28         }
29     }
30 }
派生类
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using ClassLibrary1.util.util2;
 6 
 7 namespace ClassLibrary1.util.util2
 8 {
 9     class Bmw:Cars,MyObjects
10     {
11         private String country;
12 
13         public String Country
14         {
15             get { return country; }
16             set { country = value; }
17         }
18         public Bmw()
19         {
20             Console.WriteLine("调用派生类构造方法:{0}", DateTime.Now);
21         }
22         public  String getReturnValue()
23         {
24             return Name1 + Number1;
25         
26         }
27 
28         #region MyObjects 成员
29 
30         public void createCar()
31         {
32             Console.WriteLine("创建车。");
33         }
34 
35         public void useCar()
36         {
37             Console.WriteLine("车使用中...");
38         }
39 
40         public void destroyCar()
41         {
42             Console.WriteLine("销毁车。");
43         }
44 
45         #endregion
46     }
47 }
接口
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ClassLibrary1.util.util2
 7 {
 8     interface MyObjects
 9     {
10          void createCar();
11          void useCar();
12          void destroyCar();
13 
14     }
15 }

 

转载于:https://www.cnblogs.com/FCWORLD/archive/2013/02/19/2917453.html

这篇关于C#基础知识(以宝马,车,车轮为例)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/346773

相关文章

一文解析C#中的StringSplitOptions枚举

《一文解析C#中的StringSplitOptions枚举》StringSplitOptions是C#中的一个枚举类型,用于控制string.Split()方法分割字符串时的行为,核心作用是处理分割后... 目录C#的StringSplitOptions枚举1.StringSplitOptions枚举的常用

Spring Boot分层架构详解之从Controller到Service再到Mapper的完整流程(用户管理系统为例)

《SpringBoot分层架构详解之从Controller到Service再到Mapper的完整流程(用户管理系统为例)》本文将以一个实际案例(用户管理系统)为例,详细解析SpringBoot中Co... 目录引言:为什么学习Spring Boot分层架构?第一部分:Spring Boot的整体架构1.1

Python的pandas库基础知识超详细教程

《Python的pandas库基础知识超详细教程》Pandas是Python数据处理核心库,提供Series和DataFrame结构,支持CSV/Excel/SQL等数据源导入及清洗、合并、统计等功能... 目录一、配置环境二、序列和数据表2.1 初始化2.2  获取数值2.3 获取索引2.4 索引取内容2

C#自动化实现检测并删除PDF文件中的空白页面

《C#自动化实现检测并删除PDF文件中的空白页面》PDF文档在日常工作和生活中扮演着重要的角色,本文将深入探讨如何使用C#编程语言,结合强大的PDF处理库,自动化地检测并删除PDF文件中的空白页面,感... 目录理解PDF空白页的定义与挑战引入Spire.PDF for .NET库核心实现:检测并删除空白页

C#利用Free Spire.XLS for .NET复制Excel工作表

《C#利用FreeSpire.XLSfor.NET复制Excel工作表》在日常的.NET开发中,我们经常需要操作Excel文件,本文将详细介绍C#如何使用FreeSpire.XLSfor.NET... 目录1. 环境准备2. 核心功能3. android示例代码3.1 在同一工作簿内复制工作表3.2 在不同

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

C#使用iText获取PDF的trailer数据的代码示例

《C#使用iText获取PDF的trailer数据的代码示例》开发程序debug的时候,看到了PDF有个trailer数据,挺有意思,于是考虑用代码把它读出来,那么就用到我们常用的iText框架了,所... 目录引言iText 核心概念C# 代码示例步骤 1: 确保已安装 iText步骤 2: C# 代码程

C#实现高性能拍照与水印添加功能完整方案

《C#实现高性能拍照与水印添加功能完整方案》在工业检测、质量追溯等应用场景中,经常需要对产品进行拍照并添加相关信息水印,本文将详细介绍如何使用C#实现一个高性能的拍照和水印添加功能,包含完整的代码实现... 目录1. 概述2. 功能架构设计3. 核心代码实现python3.1 主拍照方法3.2 安全HBIT

C#实现SHP文件读取与地图显示的完整教程

《C#实现SHP文件读取与地图显示的完整教程》在地理信息系统(GIS)开发中,SHP文件是一种常见的矢量数据格式,本文将详细介绍如何使用C#读取SHP文件并实现地图显示功能,包括坐标转换、图形渲染、平... 目录概述功能特点核心代码解析1. 文件读取与初始化2. 坐标转换3. 图形绘制4. 地图交互功能缩放

C#使用SendMessage实现进程间通信的示例代码

《C#使用SendMessage实现进程间通信的示例代码》在软件开发中,进程间通信(IPC)是关键技术之一,C#通过调用WindowsAPI的SendMessage函数实现这一功能,本文将通过实例介绍... 目录第一章:SendMessage的底层原理揭秘第二章:构建跨进程通信桥梁2.1 定义通信协议2.2