亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C# 类完全指南:定义、封装、构造函数、析构函数与静态成员

定义类即定义数据类型蓝图。类定义不包含实际数据,但定义了类名含义:对象包含什么、能执行什么操作。对象是类的实例。类的方法和变量称为类成员。

定义类

类定义以 class 关键字开始,后跟类名,类体用大括号包围:

<access specifier> class class_name {
    // 成员变量
    <access specifier> <data type> variable1;
    <access specifier> <data type> variable2;
    ...
    // 成员方法
    <access specifier> <return type> method1(parameter_list) { ... }
    ...
}

注意事项

基础示例:Box 类

using System;
namespace BoxApplication {
    class Box {
        public double length;
        public double breadth;
        public double height;
    }
    
    class Boxtester {
        static void Main(string[] args) {
            Box Box1 = new Box();
            Box Box2 = new Box();
            double volume = 0.0;
            
            Box1.height = 5.0; Box1.length = 6.0; Box1.breadth = 7.0;
            Box2.height = 10.0; Box2.length = 12.0; Box2.breadth = 13.0;
            
            volume = Box1.height * Box1.length * Box1.breadth;
            Console.WriteLine("Volume of Box1 : {0}", volume);  // 210
            
            volume = Box2.height * Box2.length * Box2.breadth;
            Console.WriteLine("Volume of Box2 : {0}", volume);  // 1560
            Console.ReadKey();
        }
    }
}

成员函数与封装

成员函数定义在类内部,操作该类对象,可访问该对象所有成员。成员变量作为对象属性应设为 private 实现封装,通过公共成员函数访问。

封装示例

using System;
namespace BoxApplication {
    class Box {
        private double length;
        private double breadth;
        private double height;
        
        public void setLength(double len) { length = len; }
        public void setBreadth(double bre) { breadth = bre; }
        public void setHeight(double hei) { height = hei; }
        public double getVolume() { return length * breadth * height; }
    }
    
    class Boxtester {
        static void Main(string[] args) {
            Box Box1 = new Box(), Box2 = new Box();
            double volume;
            
            Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0);
            Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0);
            
            volume = Box1.getVolume();
            Console.WriteLine("Volume of Box1 : {0}", volume);  // 210
            volume = Box2.getVolume();
            Console.WriteLine("Volume of Box2 : {0}", volume);  // 1560
            Console.ReadKey();
        }
    }
}

构造函数

类构造函数是创建新对象时自动执行的特殊成员函数。与类同名,无返回类型。

默认构造函数

public Line() {
    Console.WriteLine("Object is being created");
}

带参数构造函数

public Line(double len) {  // 参数化构造函数
    Console.WriteLine("Object is being created, length = {0}", len);
    length = len;
}

参数化构造函数允许创建对象时赋初值。

析构函数

析构函数在对象超出作用域时执行。名称与类同且前缀 ~,无返回值、无参数。用于释放内存资源。不可继承、不可重载。

~Line() {
    Console.WriteLine("Object is being deleted");
}

静态成员

用 static 关键字定义静态成员。无论创建多少对象,静态成员只有一份拷贝。静态变量用于定义常量,无需创建实例即可通过类名访问。

静态变量示例

class StaticVar {
    public static int num;
    public void count() { num++; }
    public int getNum() { return num; }
}

StaticVar s1 = new StaticVar(), s2 = new StaticVar();
s1.count(); s1.count(); s1.count();
s2.count(); s2.count(); s2.count();
Console.WriteLine(s1.getNum());  // 6
Console.WriteLine(s2.getNum());  // 6(共享同一静态变量)

静态方法示例

class StaticVar {
    public static int num;
    public void count() { num++; }
    public static int getNum() { return num; }  // 静态方法只能访问静态成员
}

StaticVar.getNum();  // 无需实例直接调用

对象初始化器语法 (C# 3.0+)

var box = new Box { Length = 5, Breadth = 6, Height = 7 };

简化属性赋值,编译器生成临时变量赋值后再赋给目标。

C语言

  1. C# 类与对象:面向对象编程的基石
  2. 理解 C# 访问修饰符:public、private、protected、internal 等完全指南
  3. C# static 关键字:静态变量、方法与类完全掌握
  4. 掌握 C# 抽象类与抽象方法:实战指南
  5. 理解 C# 嵌套类:定义、访问与继承实战
  6. C++类与对象:面向对象编程实战指南
  7. C 语言存储类详解:作用域、生命周期与四大类型实战
  8. C++存储类别全解析:auto/register/static/extern/mutable作用域与生命周期
  9. C++接口编程:抽象类与纯虚函数实现规范契约
  10. C# 程序结构解析:从 Hello World 看命名空间、类、Main 入口与编译运行
  11. C# 基础语法与面向对象核心:类、成员、实例化、标识符与关键字
  12. C# 封装详解:访问修饰符与面向对象设计最佳实践