我正在学习Java(和OOP),虽然它可能与我现在所处的位置无关,但我想知道是否可以分享一些常见的陷阱或良好的设计实践.
"命名空间污染"一词的含义是什么?为什么静态方法有助于防止它?
这个问题看起来很相似,但具体与JavaScript有关,答案并没有定义这个术语.
我一直在想,什么时候使用静态函数,何时不在ASP.NET中?
使用它们有哪些优点和缺点,包括性能,遵循良好实践等各方面(以及更多,无论您认为哪些相关).
我继承了一些常规类的代码,其中包含一些私有静态方法.代码(伪代码)看起来像这样
public class Animal
{
private string typeOfAnimal;
public Animal(string typeOfAnimal)
{
this.typeOfAnimal = typeOfAnimal;
}
public void MakeSound()
{
var sound = Animal.GetSound(typeOfAnimal);
// Make use of sound here
}
private static string GetSound(string typeOfAnimal)
{
if(typeOfAnimal == "dog")
return "bark";
else if(typeOfAnimal == "cat")
return "mjau";
}
}
Run Code Online (Sandbox Code Playgroud)
与使GetSound成为常规实例方法相比,这样做有什么好处?
假设WebApi2控制器具有一个SearchClient在启动时以scoped-lifestyle依赖项配置的。
public class SearchController : ApiController {
private readonly SearchClient _indexClient;
public SearchController(SearchClient client) {
_indexClient = client; // dependency injected
}
public IEnumerable<string> Get(string keyword){
return SearchDocuments(_indexClient, keyword);
}
public static IEnumerable<string> SearchDocuments(SearchClient indexClient, string text)
{
return indexClient.Search(text);
}
}
Run Code Online (Sandbox Code Playgroud)
如我们所见,SearchDocumentsmethod具有static关键字。
我的问题是;
static方法的好坏?static方法的安全或在这样的多所访问的网络环境建议?async static在网络环境的方法?与async方法不同吗?我知道静态方法对于接口来说是不正确的(请参阅:为什么 C# 不允许静态方法实现接口?)但遇到了这样的情况:我有一个对象实现了接口的所有方法,其中所有方法都可以静态的,所以我想我的设计一定是错误的。
问题是,我看不到任何替代方案
我的接口IDataSerializer是由几个类实现的。一种对 XML 进行反序列化,一种对 JSON 进行序列化,等等。所有这些类都实现相同的功能,并且没有任何类具有任何“状态数据”(成员等),但最终都会输出相同类型的对象。
例如,XML 类:
public class MyXmlSerializer : IDataSerializer
{
public string SerializeFoo(object foo)
{
// uses .Net XML serialzer to serialize foo
}
public object DeserializeFoo(string foo)
{
// uses .NET XML serializer to deserialize foo
}
// Object type returned by above methods is only ever
// used by following method which returns a type available
// to all IDataSerializer implementations as this is
// the data …Run Code Online (Sandbox Code Playgroud) static int _i;
static void Display()
{
//operates on _i;
}
Run Code Online (Sandbox Code Playgroud)
和
static int _i;
void Display()
{
//operates on _i;
}
Run Code Online (Sandbox Code Playgroud)
哪些方案特定于方法的静态 - 非静态问题?
编辑; 注意:这个问题不是关于静态和非静态方法之间的差异,正如许多人似乎回答的那样.问题是我应该担心差异的情景/用例是什么.为清楚起见,请参阅@ ziesemer的答案,它似乎正好解决了这个问题.
嘿伙计们,请看看我的代码,看看这里有什么问题.我查看了文档和所有内容,看起来这应该可行.
public Boolean CollisionTest (Rect one, Rect two) {
if (one.intersect(two)) {
collided = true;
} else {
collided = false;
}
return(collided);
}
Run Code Online (Sandbox Code Playgroud)
如果两个矩形碰撞,这不会返回吗?我怀疑这个的原因我在我的主线程中有一些空指针异常(它在我的游戏循环线程的finally语句中停止)调试时错误,当我不使用此函数时它很好.
非常奇怪,如果有人能发布链接到有用的碰撞检测教程,我也很感激.我想处理我自己的碰撞检测,而不是使用外部库.
感谢大家!
我正在使用 WPF 转换器并想知道在以下示例中使用类成员或局部变量在性能方面哪个更好?
public object Convert(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture)
{
if ((int)value == 1)
return (Color)ColorConverter.ConvertFromString("#FCD44E");
return (Color)ColorConverter.ConvertFromString("#FCD44E");
}
Run Code Online (Sandbox Code Playgroud)
或者 :
Color _color1 = (Color)ColorConverter.ConvertFromString("#FCD44E");
Color _color2 = (Color)ColorConverter.ConvertFromString("#FCD666");
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((int)value == 1)
return _color1;
return _color2;
}
Run Code Online (Sandbox Code Playgroud) 在代码审查期间,我向团队迅速提出了一种方法,我已经制定了静态方法,一个人同意没有理由不静态,一个人不同意说他不会让它静止,因为没有必要只是为了安全起见,以便将来进行修改和测试.
所以我做了很多研究,显然这是一个专门的案例,但我想知道在这种情况下你会做什么以及为什么?
(它基本上是一种帮助方法,我从一些不同的方法调用,一个非常低的流量页面.更多我的知识和静态学习.)
private IEnumerable<Category> GetCategoryByID(int id, Context context)
{
var categoryQuery = from selectAllProc in context.SelectAll_sp()
where selectAllProc.CategoryID == id
select selectAllProc;
return categoryQuery;
}
Run Code Online (Sandbox Code Playgroud) 是var x = new Stuff(); x.DoStuff();快new Stuff().DoStuff();吗?
我不知道为什么,但我在我的代码中注意到第一种方法使它更快,任何人都知道哪一种更快,为什么?
c# ×6
java ×2
performance ×2
static ×2
.net ×1
android ×1
asp.net ×1
class-design ×1
converter ×1
interface ×1
linq-to-sql ×1
namespaces ×1
oop ×1
optimization ×1
private ×1
wpf ×1