可能重复:
我可以向现有静态类添加扩展方法吗?
我知道我可以通过以下方式扩展课程.我有一个静态类,我想扩展.我怎么能这样做?我想写ClassName.MyFunc()
static public class SomeName
{
static public int HelperFunction(this SomeClass v)
Run Code Online (Sandbox Code Playgroud) 在下面的代码片段中,可能看起来它应该发出一些编译错误,但它不会:
class Outer {
public static class Inner {
static String obj = "Inner";
}
static Optional Inner = new Optional();
//The (inner) class name and the object name are same.
}
class Optional {
String obj = "Optional";
}
public class Main {
public static void main(String[] args) {
System.out.println(Outer.Inner.obj);
//Refers to the string inside the optional class
}
}
Run Code Online (Sandbox Code Playgroud)
该类中Outer有一个名为static的静态类Inner.另外,它声明了一个对象(静态)类Optional(static Optional Inner = new Optional();)
这个对象和类名(在类中Outer)是相同的Inner.程序显示 …
我想通过Dagger向项目引入依赖注入.以下代码用作描述注入静态类的问题的示例.
setupTextView()从多个类调用静态方法:
public abstract class TextViewHelper {
public static void setupTextView(TextView textView,
Spanned text,
TrackingPoint trackingPoint) {
textView.setText(text, TextView.BufferType.SPANNABLE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyApp.getTracker().track(trackingPoint);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
以下是如何使用辅助方法的一个示例:
TextViewHelper.setupTextView(this, R.id.some_text_view,
R.string.some_text,
TrackingPoint.SomeTextClick);
Run Code Online (Sandbox Code Playgroud)
辅助方法中使用的跟踪由应用程序类提供:
public class MyApp extends Application {
private static Tracking mTracking;
public void onCreate() {
super.onCreate();
mTracking = getTracking(getApplicationContext());
}
private Tracking getTracking(Context context) {
if (BuildConfig.DEBUG) {
return new NoTracking();
} else {
return new …Run Code Online (Sandbox Code Playgroud) 我想序列化一个非常普通的类,但问题是它嵌套在一个静态类中,如下所示:
public static class StaticClass
{
[Serializable]
public class SomeType
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码:
StaticClass.SomeType obj = new StaticClass.SomeType();
XmlSerializer mySerializer = new XmlSerializer(typeof(obj));
Run Code Online (Sandbox Code Playgroud)
产生此错误:
StaticClass.SomeType cannot be serialized. Static types cannot be used as parameters or return types.
Run Code Online (Sandbox Code Playgroud)
这个错误似乎完全无关紧要; StaticClass.SomeType不是静态类型.
有没有解决的办法?我错误地认为这个错误是愚蠢的吗?
是否有人能够解释或指向解释静态类和方法的范围如何与ASP.NET用户会话的范围交互的文章.
以下是解释我问题的更具体情况:
一旦用户B命中数据,数据是否已经初始化?
如果用户A的asp.net会话在用户B访问网站之前到期,该怎么办?
在我的ASP.NET MVC应用程序中,我有一个包含所有业务逻辑/服务层的项目.该项目与我的数据库(实体框架)进行交互,该数据库位于一个单独的项目中.
我希望能够轻松访问服务层,因此我在其中创建了静态类,以便可以轻松地引用它们.例如,如果我在我的控制器中,我需要创建一个新帐户:
ServiceLayer.Accounts.CreateAccount(userName, passWord) //etc..
Run Code Online (Sandbox Code Playgroud)
然后服务层执行所有必需的逻辑,然后通过存储库创建用户DatabaseLayer.
private static AllRepos _Repos;
private static AllRepos Repos {
get
{
if(_Repos == null)
_Repos = new AllRepos();
return _Repos
}
}
public static void CreateAccount(string username, password)
{
string salt = GenerateSalt();
Account newAccount = DatabaseLayer.Models.Account
{
Name = username,
Password = HashPassword(password, salt),
Salt = salt
};
Repos.AddAccount(newAccount);
}
Run Code Online (Sandbox Code Playgroud)
因为我不想在我的服务层中到处执行以下操作:
AccountRepository Accounts = new DatabaseLayer.AccountRepository();
Run Code Online (Sandbox Code Playgroud)
我为我的存储库创建了一个包装类,这样我只需要实例化一次就可以使用所有其他存储库.
public class AllRepos
{
private AccountRepository _Accounts;
public AccountRepository Accounts
{
get
{ …Run Code Online (Sandbox Code Playgroud) 考虑以下课程:
public class MyClass
{
public static string[] SomeAmazingConsts = { Const1 };
public static string Const1 = "Constant 1";
public static string Const2 = "Constant 2";
}
Run Code Online (Sandbox Code Playgroud)
现在,查看用法:
class Program
{
static void Main(string[] args)
{
string[] s = MyClass.SomeAmazingConsts;
//s[0] == null
}
}
Run Code Online (Sandbox Code Playgroud)
问题是s [0] == null!怎么会发生这种情况?现在,重新排序MyClass的静态变量,如下所示:
public class MyClass
{
public static string Const1 = "Constant 1";
public static string Const2 = "Constant 2";
public static string[] SomeAmazingConsts = { Const1 };
}
Run Code Online (Sandbox Code Playgroud)
事情开始正常运作.任何人都可以对此有所了解吗?
我对静态类和静态方法有疑问.从MSDN我理解"静态类和类成员用于创建可以在不创建类实例的情况下访问的数据和函数."
因此,如果我们不想将一个类与一个实例相关联,我们将把它作为静态.这是唯一的优势吗?任何人都可以指导我在静态类的实时场景.
类中的一些时间(不是静态的)我发现了静态方法.静态方法在实际中为实例提供了多少优势/性能优势.
我的代码需要有一个内部类,我想创建这个内部类的实例而不创建外部类的实例.
如何在python中这样做?在java中我们可以将内部类定义为静态但我不知道如何在python中使内部类静态化.我知道对于方法我们可以使用@staticmethod装饰器.
class Outer:
def __init__(self):
print 'Instance of outer class is created'
class Inner:
def __init__(self):
print 'Instance of Inner class is created'
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码
class A {
static class B{
int a = 0;
}
public static void main(String argc[]) {
B var1 = new B();
B var2 = new B();
var1.a = 5;
var2.a = 6;
System.out.println(var1.a+" and "+var2.a);
}
}
Run Code Online (Sandbox Code Playgroud)
它输出5和6.静态成员只加载一次.但输出与该语句相矛盾.所以静态类的概念肯定与静态数据成员不同.那么静态类的静态意味着什么