
抽象和封装是面向对象编程中相关的特性。抽象允许使相关信息可见,而封装使程序员能够实现所需的抽象级别。
可以使用 C# 中的抽象类来实现抽象。 C# 允许您创建用于提供接口的部分类实现的抽象类。当派生类继承它时,实现就完成了。抽象类包含抽象方法,这些方法由派生类实现。派生类具有更专门的功能。
以下是一些关键点 -
您无法创建抽象的实例class
不能在抽象类之外声明抽象方法
当一个类被声明为sealed时,它就不能被继承,抽象类不能声明为密封的。
实时演示
using System;
namespace Demo {
abstract class Shape {
public abstract int area();
}
class Rectangle: Shape {
private int length;
private int width;
public Rectangle( int a = 0, int b = 0) {
length = a;
width = b;
}
public override int area () {
Console.WriteLine("Rectangle class area :");
return (width * length);
}
}
class RectangleTester {
static void Main(string[] args) {
Rectangle r = new Rectangle(20, 15);
double a = r.area();
Console.WriteLine("Area: {0}",a);
Console.ReadKey();
}
}
}Rectangle class area : Area: 300
以上就是C# 中的抽象是什么?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号