来自 PHP 手册:
[...] 静态方法调用在编译时解析。当使用显式类名时,该方法已被完全标识,并且不适用继承规则。如果调用是由 self 完成的,则 self 会被转换为当前类,即代码所属的类。这里也没有继承规则适用[...]
..所以我正在寻找一种使用静态单例模拟标准oop 继承的方法。
代码解释得更好:
// Normal inheritance: my goal.
class Foo{
public function test(){
echo "Foo->test()\n";
}
}
class Bar extends Foo{
public function other_test()
{
echo "Bar->other_test()\n";
}
}
$obj = new Bar();
echo get_class($obj) . "\n";
$obj->test();
$obj->other_test();
/*
Output:
Bar
Foo->test()
Bar->other_test()
*/
// How i would love to do:
class Foo2{
public static function test2()
{
echo "Foo2::test2()\n";
}
// Singleton?
public static $_instance; …Run Code Online (Sandbox Code Playgroud) 我试图在我的Android应用程序中使用单例框架.当我调用getInstance()时,它初始化很好,但是当我在另一个活动中调用getInstance()时,它总是创建一个新实例而不是返回已经创建的实例.
public class LinkedList {
private static LinkedList theList;
public static LinkedList getInstance(){
if(theList == null){
System.out.println("New List*********************");
return new LinkedList();
}
else
return theList;
}
private LinkedList(){
defaultPopulate();
}
}
Run Code Online (Sandbox Code Playgroud)
我很好奇是否因为我是Android编程新手而缺少一些小东西.任何帮助表示赞赏.
我有我的单例类,它返回指向实例的本地静态的指针以确保线程安全.现在,如果我没有将析构函数声明为私有,用户可以将其删除吗?
对于前者
class Singleton
{
Singleton();
public:
static Singleton *getInstance();
};
Singleton *Singleton::getInstance()
{
static Singleton inst;
return &inst;
}
// in user code
void foo()
{
Singleton *inst = Singleton::getInstance();
// do its stuff
//accidentally delete instance here?!
// Should I have private destructor?
delete inst;
}
Run Code Online (Sandbox Code Playgroud) 使用各种语言结构在C#中实现Singleton模式的可能的不同方式有哪些?在哪种情况下使用每种解决方案?每种实施的优缺点是什么?并且,这是考虑性能,锁定和垃圾收集影响的那些实现中最好的一个?
还有哪些其他方法可以实现相同的方法以及如何改进以下代码:
public class Singleton
{
private static readonly Singleton _instance;
private Singleon()
{
}
public static Singleton GetInstance()
{
if(_instance == null)
_instance = new Singleton();
return _instance;
}
}
Run Code Online (Sandbox Code Playgroud) 默认情况下,一个类是单身还是不单身.
public class You{
}
Run Code Online (Sandbox Code Playgroud)
public class My(){
public static void main(String a[]){
You you=new You();
}
}
Run Code Online (Sandbox Code Playgroud)
是you对象是单身..如果它是单身如何使它成为原型..如果它是原型如何使一个类作为单身..
先感谢您..
我想创建一个单例对象,我找到了3种方法,其中一种更好,为什么其他方式不好.假定构造函数是私有的.
方法1:
class ClassX{
private static ClassX objX = null;
static{
objX = new ClassX();
}
//get objX method
}
Run Code Online (Sandbox Code Playgroud)
方法2:
class ClassX{
private static ClassX objX = new ClassX();
//get objX method
}
Run Code Online (Sandbox Code Playgroud)
方法3:
class ClassX{
private static ClassX objX = null;
public ClassX getInstance(){
if(objX == null)
return new ClassX();
else
return objX;
}
}
Run Code Online (Sandbox Code Playgroud) 我最近一直在阅读单身人士,我常常看到如下代码:
private static SingletonDemo instance = null;
Run Code Online (Sandbox Code Playgroud)
在Singleton类中作为一个字段.
我不想特别了解'静态'在做什么以及为什么需要它.
我在这里阅读了Oracle网站.并且他们使用a numOfBicycles(这是你有多个自行车的时候)的例子来证明使用静态字段,但我认为我正在理解为什么在你只是实例化1个实例时使用它.
我正在通过SOOT-ECLIPSE插件作为主类设置一个类,并希望它像单例一样运行.但是我的实现似乎不起作用,因为每次运行都会得到不同的实例.
我试图使用包装器并从那里调用单例类,以避免这个类被soot的类加载器垃圾收集的情况.但我也得到了不同的实例.
我确认它在一个JVM上运行,因为我在每次运行时获得的PID与每次运行时更改的类的实例相同.
我真的很感激对这一点的任何见解.
public class MyMain{
private static boolean isFirstInstance = true;
private static class MyMainHolder {
private static final MyMain INSTANCE = new MyMain();
}
public static synchronized MyMain getInstance() {
return MyMainHolder.INSTANCE;
}
private MyMain() {
if (MyMainHolder.INSTANCE != null) {
throw new IllegalStateException("Already instantiated");
}
}
public static void main(String[] args) {
System.out.println("PID: " + ManagementFactory.getRuntimeMXBean().getName());
MyMain tmp = getInstance();
}
Run Code Online (Sandbox Code Playgroud) java version 1.7.0_65
Run Code Online (Sandbox Code Playgroud)
我有一个单例设计模式类.这将始终返回最初创建的同一实例.
但是,我遇到的问题是这个类需要从另一个类创建许多其他对象.我已经使用了这个组合(POI类的实例ArlabFacade).从这个单例实例中,客户端应该能够创建许多POI对象.而且我不想公开POI类的内部工作,一切都必须通过单例实例.
private static ArlabFacade mArlabFacade = null;
private POI mPoi; /* Should be able to create many object of this class */
private ArlabFacade(Context context) {
/* initialize properties */
mContext = context;
mPoi = null;
}
public static ArlabFacade getInstance(Context context) {
/* Create a synchronised singleton instance */
ReentrantLock lock = new ReentrantLock();
lock.lock();
if(mArlabFacade == null) {
mArlabFacade = new ArlabFacade(context);
}
lock.unlock();
return mArlabFacade;
}
Run Code Online (Sandbox Code Playgroud)
我尝试过做这样的事情,但它有两个问题.
1) I …Run Code Online (Sandbox Code Playgroud) 在这些条件下,我编写了下一个Singleton类:
1 - 我想要一个且只有一个类的实例存在并且可以从整个游戏引擎访问.
2 - Singleton被密集使用(每帧数千次)所以我不想写一个额外的GetInstance()函数,我试图避免任何额外的函数调用性能
3 - 一种可能性是让GetInstance()像内联一样这个 :
inline Singleton* Singleton::GetInstance()
{
static Singleton * singleton = new Singleton();
return singleton;
}
Run Code Online (Sandbox Code Playgroud)
但这会引起一个引用问题,每次调用时都会有一个对单例的新引用,用于修复用c ++编写的内容:
class Singleton{
private:
static Singleton* singleton;
Singleton(){}
public:
static inline Singleton* GetInstance() // now can be inlined !
{
return singleton;
}
static void Init()
{
// ofc i have to check first if this function
// is active only once
if(singleton != nullptr)
{
delete singleton;
}
singleton = new Singleton();
} …Run Code Online (Sandbox Code Playgroud)