C#知识整理

2019-12-30 16:21:45丽君

这里简单介绍了一些常用的属性,以及一些术语的解释和举例说明,不太全面,希望读者多多补充。

1.重载:函数名相同,参数的个数或参数类型不同;


 public void MyDog(string s);
 public void MyDog(int i);
 public void MyDog(string s,int i); 

2.继承:一个类继承另一个类中的成员,被继承的叫做基类,继承类叫做派生类;


class A
 {
 成员;
 }
class B:A //继承的写法 派生类:基类
 { 
 成员;
 }

3.多态:可以在子类中重写父类的方法 重写方法需要定义override类型


 public override DuoTai()
 {
 Console.WriteLine("此处可以重写父类中的'DuoTai'方法");
 }

4.接口:实现提供了一种规范和约束,关键词 Interface

 1.修饰符:new public protected internal private;

 2.接口成员前不允许有修饰符;

 3.一个类可以继承多个接口;

 4.格式: 接口修饰符  关键字  接口名


 public interfa JieKou
 {
 void g(); //接口成员;
 }

5.抽象类:关键字(abstract)


 public abstract class PiSaAll
 {
 成员;
 }

6.封装(类的属性):将一组数据打包封装.


 public string MianBing { get; set; }
 public string Shui { get; set; }

7.构造函数:与类同名,public修饰,没有返回值(不是void)


class Dog
 {
 public Dog(string s,int i ) //构造函数写法
 {
 Console.WriteLine("这是一只小狗!名叫{0},今年{1}岁",s,i);
 }
 }

 Mian中调用:
 Dog dog = new Dog("铛铛",4);

8.成员访问控制符:

   1.Public (共有的):允许任何类中的成员进行访问.

   2.Private (私有的):不能被其他类中的成员访问,包括派生类也不好使.

   3.Internal (内部成员):只能被程序集内的类的成员访问,而程序集外的类(包括派生类)的成员是不允许访问的.

   4.Protected (保护成员):可以被本类或派生类中的成员访问,其他类中的成员不允许访问.

9.连接数据库用的语句:


 1. string conStr = "Data Source=IUCL8V4Y7NW5IRASQLEXPRESS;Initial catalog=BookShopPlus;User Id=sa;Pwd=sa123"; 
 
 2. static string s = @"server=MY-20150918RBSF;database=Beauty;Integrated Security = true"; 
 
 3. static string s = ConfigurationSettings.AppSettings["dbinfo"].ToString(); 

10.异常处理:


 1. try{}catch{}
 
 2. try{}catch{}finally{}

 3. using(SqlConnection con = new SqlConnection(conStr)){}

11.命名空间:


 1.using System.Data.SqlClient; ==>用于SQL数据库

 2.using System.Data; ==>可使用Data类

 3.using System.Collections; ==>ArrayList数组