封装与抽象?(Encapsulation vs Abstraction?)
以下是封装和抽象的简要定义。
抽象:
Java中的抽象过程用于隐藏某些细节,仅显示对象的基本特征。 换句话说,它处理对象(接口)的外部视图。 我唯一看到的不同网站的好例子就是界面。
封装:
它基本上是在私有,公共,受保护的等等的修改器的帮助下隐藏对象的状态。只有在需要的时候,我们通过公共方法暴露出状态。
我们用
private
,public
修改器实现了我们,也隐藏了来自外部世界的不必要的细节,这也是一个抽象概念因此,从上面的解释看,封装是抽象的一部分,或者我们可以说它是抽象的一个子集。 但是为什么当我们可以处理它的抽象只是发明封装术语? 我相信应该有一些区别的区别,但网上的大部分材料对他们来说都是一样的。
虽然这个问题早已在这个论坛上提出,但我再次发表具体的疑问。 一些答复也表示抽象是一个概念,封装是实现。 但我不买这个 - 如果是真的,那么我可以认为这两个不同的概念是混淆我们的。
更新: - 5年后,我提出了自己的答案,这是基于这个帖子和下面的答案的要点
- 抽象和封装之间的区别?
- 封装与抽象现实世界的例子
Here are the brief definitions of encapsulation and abstraction.
Abstraction:
The process of abstraction in Java is used to hide certain details and only show the essential features of the object. In other words, it deals with the outside view of an object (interface). The only good example i see for this across different sites is interface.
Encapsulation:
Its basically about hiding the state of object with the help of modifiers like private,public,protected etc. we expose the state thru public methods only if require.
What we achieve with modifiers like
private
,public
also hides unnecessary details from out side world which is nothing but also a abstraction conceptSo, from above explanation looks like encapsulation is a part of abstraction or we can say it's a subset of abstraction. But why then encapsulation term is invented when we could deal it with abstraction only? I am sure there should be some major difference which distinguishes them but most of material on net says almost same thing for both of them.
Though this question has been raised on this forum earlier too but I am posting it again with specific doubts. Some replies also says abstraction is a concept and encapsulation is implementation. But I don't buy this - If it is true, then I can think these two different concepts are provided to confuse us.
Update:- After 5 years i have come up with my own answer whichs is the gist based on answers in this post and below ones
原文:https://stackoverflow.com/questions/8960918
最满意答案
在我最近工作的网站上实现自动完成时,我遇到了同样的问题。 这是我想出的解决方案:
$("input").focusin(function () { $(document).keypress(function (e) { if (e.which == 13) { var firstResult = $(".pac-container .pac-item:first").text(); var geocoder = new google.maps.Geocoder(); geocoder.geocode({"address":firstResult }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { var lat = results[0].geometry.location.lat(), lng = results[0].geometry.location.lng(), placeName = results[0].address_components[0].long_name, latlng = new google.maps.LatLng(lat, lng); $(".pac-container .pac-item:first").addClass("pac-selected"); $(".pac-container").css("display","none"); $("#searchTextField").val(firstResult); $(".pac-container").css("visibility","hidden"); moveMarker(placeName, latlng); } }); } else { $(".pac-container").css("visibility","visible"); } }); });
http://jsfiddle.net/dodger/pbbhH/
I had the same issue when implementing autocomplete on a site I worked on recently. This is the solution I came up with:
$("input").focusin(function () { $(document).keypress(function (e) { if (e.which == 13) { var firstResult = $(".pac-container .pac-item:first").text(); var geocoder = new google.maps.Geocoder(); geocoder.geocode({"address":firstResult }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { var lat = results[0].geometry.location.lat(), lng = results[0].geometry.location.lng(), placeName = results[0].address_components[0].long_name, latlng = new google.maps.LatLng(lat, lng); $(".pac-container .pac-item:first").addClass("pac-selected"); $(".pac-container").css("display","none"); $("#searchTextField").val(firstResult); $(".pac-container").css("visibility","hidden"); moveMarker(placeName, latlng); } }); } else { $(".pac-container").css("visibility","visible"); } }); });
相关问答
更多-
你的修复也适用于我。 我使用Geocomplete jQuery插件http://ubilabs.github.com/geocomplete/,并在其主页上的说明中声明使用此 但它没有为我工作,并得到同样的错误。 请参阅https://developers.google.com/maps/documentatio ...
-
Google Maps Places V3自动填充 - 选择第一个选项(Google maps Places API V3 autocomplete - select first option on enter)[2022-12-06]
在我最近工作的网站上实现自动完成时,我遇到了同样的问题。 这是我想出的解决方案: $("input").focusin(function () { $(document).keypress(function (e) { if (e.which == 13) { var firstResult = $(".pac-container .pac-item:first").text(); var geocoder = new google. ... -
谷歌地图api v3仅为选定的城市放置自动完成功能(google maps api v3 places autocomplete only for selected city)[2022-04-13]
你使用相同的LatLng用于NorthEast和SouthWest的hyderabadBounds,使用包含所需区域的LatLngBounds对象,例如: bounds: new google.maps.Circle({center:new google.maps.LatLng( 28.3914,-81.936035 ), radius:50000}).getBounds() 注意:无法保证自动完成将结果限制为给定的边界 you are us ... -
使用jQuery自动完成插件进行Google Maps Api v3位置搜索(Google Maps Api v3 Location Search with jQuery Autocomplete Plugin)[2023-06-23]
您正在查看的示例是使用旧版Google Maps API完成的。 当前版本有一个自动完成小部件,可以处理您的用例。 简单地做 var input = document.getElementById('searchbox'); var autocomplete = new google.maps.places.Autocomplete(input); 每当一个人选择一个建议的位置时,将触发place_changed事件,因此您可以设置一个侦听器来检测此类事件并处理该位置: google.maps.event ... -
事实证明,它似乎是我在我的iPhone上安装的Swype键盘中的一个错误,这也与我安装的其他一些应用程序有问题(例如eBay应用程序中的搜索字段。)使用股票键盘工作得非常好。 我将把这个报告为Swype的错误。 Turns out it seems to be a bug in the Swype keyboard I have installed on my iPhone, which also has issues with some other apps I have installed, (such ...
-
我可以告诉你我如何做这个页面http://www.a-zhotels.net/register ,你可以轻松地将所有字段组合成一个自动完成地址字段。 但是,这应该给你一个想法。 首先,创建一个函数: function getMapByGeoLocation() { //build the address using many fields. var postcode = $("#HotelPostcode").val(); var address = $("#HotelAddress ...
-
Google Maps v3 API地理编码和地点不一致的视口(Google Maps v3 APIs Geocoding and Places Inconsistent Viewport)[2022-04-03]
您是否希望搜索后地图缩放级别保持一致? 我用谷歌地图做android。 只需查看上面提供的链接中的代码即可。 将map.setZoom()方法更改为您想要的级别可能会有所帮助。 我还没有完成javascript google map api。 只是广泛猜测。 如果我回答错误的问题,请纠正我。 Are you wanting the map zoom level to be consistent after the search ? I do android with google map. And just ... -
问题在于页面上的JavaScript。 我应该使用“==”,而不是使用多个“===”运算符。 The issue was with the JavaScript on the page. Instead of using multiple "===" operator I should have been using "==".
-
请参阅此Google maps api 。 您可以使用方法setComponentRestrictions Please see this Google maps api. You can use the method setComponentRestrictions
-
尝试使用jquery.simulate.js : $(elem).simulate(mouse_or_keyboard_event_type, options); 支持的事件类型: 鼠标: mouseover,mouseout,mousedown,mouseup,mousemove,click,dblclick 键盘: keyup,keydown,keypress Try to use jquery.simulate.js : $(elem).simulate(mouse_or_keyboard_even ...