类的定义
类:类的定义是以关键字 class 开始,后跟类的名称。
<access specifier> class class_name{// member variables<access specifier> <data type> variable1;<access specifier> <data type> variable2;...<access specifier> <data type> variableN;// member methods<access specifier> <return type> method1(parameter_list){// method body}<access specifier> <return type> method2(parameter_list){// method body}...<access specifier> <return type> methodN(parameter_list){// method body}}
Attention:
- 访问标识符 (access specifier) 指定了对类及其成员的访问规则。如果没有指定,则使用默认的访问标识符。类的默认访问标识符是 internal,成员的默认访问标识符是 private。
- (data type)指定了变量的类型,(return type)指定了返回方法的数据类型。
下列举一个例子
using System;namespace Person{class Person{public double weight; // 重量public double height; // 高度}class Preson_detail{static void Main(string[] args){Preson Preson1 = new Preson(); // 声明 Preson1,类型为 PresonPreson Preson2 = new Preson(); // 声明 Preson2,类型为 PresonBMI = 0.0;// Preson1 详述Preson1.height = 165.3;Preson1.weight = 46.0;// Preson1 详述Preson2.height = 187.3;Preson2.weight = 78.2;// Person1 的BMI指数BMI1 = Person1.weight /Person1.height*Person1.heightConsole.WriteLine(\"Person1 的BMI指数: {0}\", BMI1);// Person2 的BMI指数BMI2 = Person2.weight /Person2.height*Person2.heightConsole.WriteLine(\"Person2 的BMI指数: {0}\", BMI);Console.ReadKey();}}}
成员函数和封装
- 成员函数:是在类定义中有它的定义或者原型的函数,它能在类的任何对象上操作,且能访问该对象的类的所有成员。
- 成员变量:是对象的属性,且他们保持私有来实现封装。这些变量只能使用公共成员函数来访问。
using System;namespace Person{class Person{private double weight; // 重量private double height; // 高度public void Setweight( double wei ){weight=wei;}public void Setheight( double hei ){height=hei;}public double GetBMI(){return weight/height*height;}}class Preson_detail{static void Main(string[] args){Preson Preson1 = new Preson(); // 声明 Preson1,类型为 PresonPreson Preson2 = new Preson(); // 声明 Preson2,类型为 PresonBMI = 0.0;// Preson1 详述Preson1.Setheight (165.3);Preson1.Setweight (46.0);// Preson2 详述Preson2.Setheight (187.3);Preson2.Setweight (78.2);// Person1 的BMI指数BMI1 = Preson1.GetBMI();Console.WriteLine(\"Person1 的BMI指数: {0}\", BMI1);// Person2 的BMI指数BMI2 = Person2.GetBMI();Console.WriteLine(\"Person2 的BMI指数: {0}\", BMI2);Console.ReadKey();}}}
构造函数
- 类的构造函数:是特殊的长远函数,当创建类的新对象时执行。
using System;namespace Person{class Person{private double height; // 人的身高public Person(){Console.WriteLine(\"对象已创建\");}public void setheight( double hei ){height = hei;}public double getheight(){return height;}static void Main(string[] args){Line line = new Person();// 设置人的身高Person.setheight(187.3);Console.WriteLine(\"人的身高: {0}\", Person.Getheight());Console.ReadKey();}}}
- 默认的构造函数没有任何参数,也可以参数化构造函数
using System;namespace Person{class Person{private double height; // 人的身高public Person(double hei) // 参数化构造函数{Console.WriteLine(\"对象已创建,height = {0}\", hei);height = hei;}public void setheight( double hei){height = hei;}public double getheight(){return height;}static void Main(string[] args){Line Person = new Person(187.3);Console.WriteLine(\"人的身高: {0}\", line.getheight());// 设置人的身高Person.setheight(187.3);Console.WriteLine(\"人的身高: {0}\", line.getheight());Console.ReadKey();}}
析构函数
- 类的析构函数:是特殊的成员函数,当类 的对象超出范围时执行。(~作为前缀,不返回值,也不带任何参数,用于在程序结束之前释放资源,它不能继承或者重载)
using System;namespace Person{class Person{private double height; // 人的身高public Person(){Console.WriteLine(\"对象已创建\");}~Person(){Console.WriteLine(\"对象已删除\");}public void setheight( double hei ){height = hei;}public double getheight(){return height;}static void Main(string[] args){Line line = new Person();// 设置人的身高Person.setheight(187.3);Console.WriteLine(\"人的身高: {0}\", Person.Getheight());}}}
静态成员
- 关键字static可以把类成员定义为静态的,当为静态的时候,无论有多少个类的对象被穿件,只会有该静态成员的一个副本。
using System;namespace EX03(static){class StaticVar{public static int Age;public void count(){Age += 2;}public int getAge(){return Age;}}class StaticTest{static void Main(string[] args){StaticVar a = new StaticVar();StaticVar b = new StaticVar();a.count();a.count();b.count();b.count();Console.WriteLine(\"a 的年龄 Age: {0}\", a.getAge());Console.WriteLine(\"b 的年龄 Age: {0}\", b.getAge());Console.ReadKey();}}}
- 静态函数的用法
using System;namespace EX03(static){class StaticVar{public static int Age;public void count(){Age += 2;}public static int getAge(){return Age;}}class StaticTest{static void Main(string[] args){StaticVar a = new StaticVar();a.count();a.count();Console.WriteLine(\"a 的年龄 Age: {0}\", StaticVar.getAge());Console.ReadKey();}}}
爱站程序员基地
![(原创)[C#] 一步一步自定义拖拽(Drag&Drop)时的鼠标效果:(一)基本原理及基本实现-爱站程序员基地](https://aiznh.com/wp-content/uploads/2022/07/20220721231016-62d9dcd85c40b-220x150.gif)

