对我来说,封装是将冗长/复杂的过程分解成更小和更有组织的子例程的过程。
function DoEverything() {
// do everything
}
function Encapsulation() {
GetTheLegos();
BuildTheLegoShip();
PretendYouAreAPirate();
}
抽象是找出一组进程的相似性并将这些相似性合并成一个进程的过程。
private void ProcessOne() {
try {
// code
}catch(exception ex) {
Utilities.log(ex);
Error.Text = "Error!";
}
}
private void ProcessTwo() {
try {
// different code
}catch(exception ex) {
Utilities.log(ex);
Error.Text = "Error!";
}
}
然后将其转化为如下所示的内容。
private void HandleException(exception ex) {
Utilities.log(ex);
Error.Text = "Error!";
}
private void ProcessOne() {
try {
// code
}catch(exception ex) {
HandleException(ex);
}
}
private void ProcessTwo() {
try {
// code
}catch(exception ex) {
HandleException(ex);
}
}
最后一个公共的方法来访问这些领域,这样 奉献一流水平的封装,是不是每个类得到这份工作? :) – 2013-03-02 15:57:39
@Adrian - nope;) – WpfBee 2013-03-02 17:17:55
@WPFbee不幸! – 2017-04-10 15:13:26