封装与普通(Encapsulation Vs Plain)
如果下面的代码产生相同的结果,为什么我应该使用封装?
封装的主要好处是能够修改我们实现的代码而不会破坏使用我们代码的其他人的代码。
但我可以使用这个好处而不使用封装吗? 因为每个对象的字段都彼此不同。
// Person.java public class Person { // Plain public String name; // Uses encapsulation private String name2; public void setName(String name2) { this.name2 = name2; } public String getName() { return name2; } } // Main.java public class Main() { public static void main(String[] args) { // Plain Person person = new Person(); person.name = "Jordan"; System.out.println(person.name); // Uses encapsulation Person person2=new Person(); person2.setName("Jordan"); System.out.println(person2.getName()); } }
Why should I use encapsulation if the code below will produce the same result?
The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code.
But I can use this benefit whithout using encapsulation right? because every object's field differs from each other's field.
// Person.java public class Person { // Plain public String name; // Uses encapsulation private String name2; public void setName(String name2) { this.name2 = name2; } public String getName() { return name2; } } // Main.java public class Main() { public static void main(String[] args) { // Plain Person person = new Person(); person.name = "Jordan"; System.out.println(person.name); // Uses encapsulation Person person2=new Person(); person2.setName("Jordan"); System.out.println(person2.getName()); } }
原文:https://stackoverflow.com/questions/31529809
最满意答案
参考分辨率是MSBuild最棘手的部分之一。 程序集的定位逻辑是在一组标准任务中实现的: ResolveAssemblyReference , ResolveNativeReference等。逻辑是如何工作的非常复杂,你可以看到只是通过查看那些任务的可能参数的数量。
但是,您无需知道查找引用文件位置的确切逻辑。 有一些标准目标称为“ResolveAssemblyReferences”,“ResolveProjectReferences”,还有一些更专门用于本机引用,COM引用。 这些目标作为正常构建的一部分执行。 如果您只是单独执行这些目标,您可以找到返回值,这正是您所需要的。 IDE使用相同的机制来获取引用的位置,用于智能感知,内省等。
以下是如何在代码中执行此操作:
using Microsoft.Build.BuildEngine; using Microsoft.Build.Execution; using Microsoft.Build.Framework; using System; using System.Collections.Generic; class Program { static int Main(string[] args) { if (args.Length < 1) { Console.WriteLine("Usage: GetReferences.exe <projectFileName>"); return -1; } string projectFileName = args[0]; ConsoleLogger logger = new ConsoleLogger(LoggerVerbosity.Normal); BuildManager manager = BuildManager.DefaultBuildManager; ProjectInstance projectInstance = new ProjectInstance(projectFileName); var result = manager.Build( new BuildParameters() { DetailedSummary = true, Loggers = new List<ILogger>() { logger } }, new BuildRequestData(projectInstance, new string[] { "ResolveProjectReferences", "ResolveAssemblyReferences" })); PrintResultItems(result, "ResolveProjectReferences"); PrintResultItems(result, "ResolveAssemblyReferences"); return 0; } private static void PrintResultItems(BuildResult result, string targetName) { var buildResult = result.ResultsByTarget[targetName]; var buildResultItems = buildResult.Items; if (buildResultItems.Length == 0) { Console.WriteLine("No refereces detected in target {0}.", targetName); return; } foreach (var item in buildResultItems) { Console.WriteLine("{0} reference: {1}", targetName, item.ItemSpec); } } }
请注意,调用引擎以调用项目中的特定目标。 您的项目通常不会构建,但某些目标可能会被先决条件目标调用。
只需编译它并打印所有依赖项的子集。 如果对项目使用COM引用或本机依赖项,则可能存在更多依赖项。 应该很容易修改样本以获得这些。
The reference resolution is one of the trickiest parts of MSBuild. The logic of how assemblies are located is implemented inside the a standard set of tasks: ResolveAssemblyReference, ResolveNativeReference, etc. The logic is how this works is very complicated, you can see that just by looking at number of possible parameters to those tasks.
However you don't need to know the exact logic to find the location of referenced files. There are standard targets called "ResolveAssemblyReferences", "ResolveProjectReferences" and some others more specialized for native references, COM references. Those targets are executed as part of the normal build. If you just execute those targets separately, you can find out the return values, which is exactly what you need. The same mechanism is used by IDE to get location of refereces, for Intellisense, introspection, etc.
Here is how you can do it in code:
using Microsoft.Build.BuildEngine; using Microsoft.Build.Execution; using Microsoft.Build.Framework; using System; using System.Collections.Generic; class Program { static int Main(string[] args) { if (args.Length < 1) { Console.WriteLine("Usage: GetReferences.exe <projectFileName>"); return -1; } string projectFileName = args[0]; ConsoleLogger logger = new ConsoleLogger(LoggerVerbosity.Normal); BuildManager manager = BuildManager.DefaultBuildManager; ProjectInstance projectInstance = new ProjectInstance(projectFileName); var result = manager.Build( new BuildParameters() { DetailedSummary = true, Loggers = new List<ILogger>() { logger } }, new BuildRequestData(projectInstance, new string[] { "ResolveProjectReferences", "ResolveAssemblyReferences" })); PrintResultItems(result, "ResolveProjectReferences"); PrintResultItems(result, "ResolveAssemblyReferences"); return 0; } private static void PrintResultItems(BuildResult result, string targetName) { var buildResult = result.ResultsByTarget[targetName]; var buildResultItems = buildResult.Items; if (buildResultItems.Length == 0) { Console.WriteLine("No refereces detected in target {0}.", targetName); return; } foreach (var item in buildResultItems) { Console.WriteLine("{0} reference: {1}", targetName, item.ItemSpec); } } }
Notice, the engine is called to invoke specific targets in the project. Your project usually does not build, but some targets might be invoked by pre-requisite targets.
Just compile it and will print a sub-set of all dependencies. There might be more dependencies if you use COM references or native dependencies for your project. It should be easy to modify the sample to get those as well.
相关问答
更多-
嗯,事实证明这根本不是TFS的错... 有问题的DLL虽然存在于References文件夹中,但实际上并未检入TFS。 右键单击VS中的References文件夹(添加为“解决方案文件夹”)并选择添加现有项目,然后检入以解决问题。 因此,正如您所期望的那样使用References文件夹,就像VS使用它一样。 MSBuild在本地工作,因为我在我的本地文件夹中有文件,但因为它不是解决方案的一部分,所以它不是源代码的其余部分。 Well it turns out this wasn't a fault with ...
-
这是您的环境特有的。 通常,如果您的解决方案中引用了nuget包EnterpriseLibrary.Common版本5.0.505.0,并且您在新计算机上打开解决方案并还原nuget包,则还会还原该DLL。 您可以尝试在程序包管理器控制台中运行它: Update-Package EnterpriseLibrary.Common -Reinstall 并希望它有所帮助。 如果没有,则nuget包本身(* .nupkg)应该位于 packages\EnterpriseLibrary.Common.5.0.50 ...
-
在msbuild + xbuild中使用Microsoft.Build.Tasks(Using Microsoft.Build.Tasks in msbuild + xbuild)[2022-05-11]
使用AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll"解决了这个问题。 看起来$(MSBuildToolsPath)根据运行平台设置为适当的值,因此它始终有效。 另外我不得不专门使用v12.0,v4.0没有用。 Using AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll" solved the problem. Looks like $(MSB ... -
您的项目引用了无法解析的Newtonsoft.Json。 您需要签入您的包文件夹(在解决方案的根目录中)或在构建期间启用Nuget恢复。 The MSBuild parameter /p:VisualStudioVersion=14.0 fixed this issue
-
您可以像这样使用Assembly.LoadFrom Method (根据文件名或路径加载程序集。): Assembly assembly = Assembly.LoadFrom("Path of the DLL"); You can use Assembly.LoadFrom Method Like this (Loads an assembly given its file name or path.): Assembly assembly = Assembly.LoadFrom("Path of th ...
-
尝试使用Microsoft.Build.Evaluation时出现UnitTask错误(UnitTask error trying to use Microsoft.Build.Evaluation)[2023-11-14]
在任务中包含Microsoft.Build引用可以解决该问题。 (首先需要将Microsoft.Build.dll引用添加到您的项目中)使用Microsoft.Build.Evaluation解析实际的引用路径(Resolve actual Reference path using Microsoft.Build.Evaluation)[2022-09-08]
参考分辨率是MSBuild最棘手的部分之一。 程序集的定位逻辑是在一组标准任务中实现的: ResolveAssemblyReference , ResolveNativeReference等。逻辑是如何工作的非常复杂,你可以看到只是通过查看那些任务的可能参数的数量。 但是,您无需知道查找引用文件位置的确切逻辑。 有一些标准目标称为“ResolveAssemblyReferences”,“ResolveProjectReferences”,还有一些更专门用于本机引用,COM引用。 这些目标作为正常构建的一部分 ...在运行Microsoft.Build.BuildEngine的应用程序中,我将构建类型从Any CPU更改为x86。 现在,这使应用程序能够按预期构建目标。 In the application that is running the Microsoft.Build.BuildEngine I changed the type of build from Any CPU to x86. This has now made the application be able to build the target ...如何使用Microsoft.Build.Evaluation.Project.RemoveItem(How to use Microsoft.Build.Evaluation.Project.RemoveItem)[2022-06-02]
这是我最终写作的课程。 没有简单的解决方案。 public static class SourceControlHelper { public static void CheckoutFile(string filePath) { TFSAction((workspace) => workspace.PendEdit(filePath), filePath); } public static void ...Visual Studio 2010是一个32位进程, WOW将为32位进程提供c:\Program Files (X86) 。 我的假设在这里: 如果我试图聪明,而是使用Microsoft.Build.Evaluation.ProjectCollection.LoadProject([project file]),试图在代码中构建它 当您在“代码中”执行此操作时,您的代码正在执行x64,因此您可以获得正常的环境值。 如果需要x86程序文件目录的路径,可以使用x64中的ProgramFiles(x86)环境 ...相关文章
更多- java 封装
- 微信公众平台消息接口开发(2)-封装weixin.class.php
- 谁有封装分页的例子哇!!!!!
- 封装一个简单的solrserver组件
- 用jquery validate封装自己的验证库
- Storm【实践系列-如何写一个爬虫- 对于Protocol进行的封装】
- 自己封装的一个Solr Data Import Request Handler Scheduler
- 全手动封装教程+SRS9.80102 文本教程(适合初学)
- 微信公众平台消息接口开发(2)-封装weixin.class.php
- 微信公众平台消息接口开发-封装weixin.class.php(转)
最新问答
更多- 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
- 如何通过引用返回对象?(How is returning an object by reference possible?)
- 矩阵如何存储在内存中?(How are matrices stored in memory?)
- 每个请求的Java新会话?(Java New Session For Each Request?)
- css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
- 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
- xcode语法颜色编码解释?(xcode syntax color coding explained?)
- 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
- 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
- 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
- 西安哪有PLC可控制编程的培训
- 在Entity Framework中选择基类(Select base class in Entity Framework)
- 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
- 电脑二级VF有什么用
- Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
- 金华英语角.
- 手机软件如何制作
- 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
- 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
- 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
- Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
- 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
- python的访问器方法有哪些
- DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
- 在Ruby中对组合进行排序(Sorting a combination in Ruby)
- 网站开发的流程?
- 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
- 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
- 透明度错误IE11(Transparency bug IE11)
- linux的基本操作命令。。。