首页
\
问答
\
AtomicReference的等价物,但没有不稳定的同步成本(Equivalent of AtomicReference but without the volatile synchronization cost)
AtomicReference的等价物,但没有不稳定的同步成本(Equivalent of AtomicReference but without the volatile synchronization cost)
相当于什么:
AtomicReference<SomeClass> ref = new AtomicReference<SomeClass>( ... );
但没有同步成本。 请注意,我确实想要在另一个对象中包装引用。
我已经看过扩展Reference抽象类的类,但是我在所有的选择中都有点失落。
我需要一些非常简单的,不是虚弱的,也不是幻影,也不需要其他所有参考。 我应该使用哪个课程?
What is the equivalent of:
AtomicReference<SomeClass> ref = new AtomicReference<SomeClass>( ... );
but without the synchronization cost. Note that I do want to wrap a reference inside another object.
I've looked at the classes extending the Reference abstract class but I'm a bit lost amongst all the choices.
I need something really simple, not weak nor phantom nor all the other references besides one. Which class should I use?
原文:https://stackoverflow.com/questions/8344509
更新时间:2023-09-11 09:09
最满意答案
不完全,但这里有5种模式可以让你接近:
namespace My.Namespace { using H = MyHelperClass; public class MyHelperClass { public static void HelperFunc1() { Console.WriteLine("Here's your help!"); } } public class MyHelperClass2 { public static void HelperFunc4() { Console.WriteLine("Here's your help!"); } } public interface IHelper{ } public static class HelperExtensions { public static void HelperFunc3(this IHelper self) { Console.WriteLine("Here's your help!"); } } public class MyClass : MyHelperClass2, IHelper { private static readonly Action HelperFunc2 = MyHelperClass.HelperFunc1; private static void HelperFunc5() { Console.WriteLine("Here's your help!"); } public void MyFunction() { //Method 1 use an alias to make your helper class name shorter H.HelperFunc1(); //Method 2 use a class property HelperFunc2(); //Method 3 extend an interface that has extension methods. //Note: you'll have to use the this keyword when calling extension this.HelperFunc3(); //Method 4 you have access to methods on classes that you extend. HelperFunc4(); //Method 5 put the helper method in your class HelperFunc5(); } } }
Not quite, but here are 5 patterns that get you close:
namespace My.Namespace { using H = MyHelperClass; public class MyHelperClass { public static void HelperFunc1() { Console.WriteLine("Here's your help!"); } } public class MyHelperClass2 { public static void HelperFunc4() { Console.WriteLine("Here's your help!"); } } public interface IHelper{ } public static class HelperExtensions { public static void HelperFunc3(this IHelper self) { Console.WriteLine("Here's your help!"); } } public class MyClass : MyHelperClass2, IHelper { private static readonly Action HelperFunc2 = MyHelperClass.HelperFunc1; private static void HelperFunc5() { Console.WriteLine("Here's your help!"); } public void MyFunction() { //Method 1 use an alias to make your helper class name shorter H.HelperFunc1(); //Method 2 use a class property HelperFunc2(); //Method 3 extend an interface that has extension methods. //Note: you'll have to use the this keyword when calling extension this.HelperFunc3(); //Method 4 you have access to methods on classes that you extend. HelperFunc4(); //Method 5 put the helper method in your class HelperFunc5(); } } }
相关问答
更多-
一种解决方案是在专用单例中存储在引导程序中创建的注入器的引用。 bootstrap(App, [Auth, ROUTER_PROVIDERS]) .then((appRef: ComponentRef) => { // store a reference to the injector appInjector(appRef.injector); }); 单身人士如下: let appInjectorRef: Injector; export const appInjector = (injecto ...
-
PHP静态方法(PHP static methods)[2022-07-22]
既然你声明你已经明白什么是static ,我会跳过它。 但是,引用关于static关键字的 PHP 文档可能仍然很好。 特别是以下两个警报很重要(真的很难看清楚)。 警告在PHP 5中,静态调用非静态方法会生成E_STRICT级别警告。 而这一个(斜体强调我的)。 警告在PHP 7中,静态调用非静态方法已被废弃,并将生成E_DEPRECATED警告。 支持静态调用非静态方法将来可能会被删除。 所以,简而言之就是:您的示例将运行(暂时),因为PHP解释器会尝试修复您的错误。 但是,你应该永远不要这样做 。 P ... -
参考方法(Reference of methods)[2024-05-11]
你有一些选择。 您可以使用NSStrings实际拥有方法的名称,然后执行以下操作: NSString *myMethodName = @"hellWorld"; SEL selector = selectorFromString(myMethodName); 然后,您可以将选择器添加到NSArray 。 您还可以使用NSInvocations并存储方法并在以后定义目标: NSInvocation对象包含Objective-C消息的所有元素:目标,选择器,参数和返回值。 可以直接设置这些元素中的每一个,并在 ... -
在这种情况下,对实例方法append未绑定引用具有相同的arity,参数类型甚至返回值作为对静态方法append的引用,所以不,您无法解决方法引用的消歧。 如果您不想重命名其中一个方法,则应使用lambda: collect(StringCollector::new, (sb, s) -> sb.append(s), StringCollector::concat); 或者,如果您确实想使用静态方法: collect(StringCollector::new, (sb, s) -> StringColle ...
-
真的没有理由为什么你可以这样做。 我唯一的猜测是,它可以让你覆盖静态方法,但你不能 。 如果您尝试以下情形: 香蕉有一个称为'测试'的静态方法(打印'香蕉')苹果延伸香蕉和“覆盖”静态方法称为'测试'(这打印'苹果') 你做这样的事情: public static void main(String[] args) { Apple apple = new Apple(); Banana banana = new Banana(); Banana base = new Apple(); ...
-
好吧,除了你的ClassA构造函数格式错误(你错过了括号)之外,该代码编译 ......但是在构造ClassA的实例之前, PointerToB和PointerToC都将为null。 如果您只想初始化它们一次,请在静态初始化程序中执行 - 例如使用声明: public static ClassB PointerToB = new ClassB(); public static ClassC PointerToC = new ClassC(); 即便如此,这里有两个令人讨厌的设计(IMO): 公共可变字段几 ...
-
快速参考静态方法(Quickly reference static methods)[2022-01-18]
不完全,但这里有5种模式可以让你接近: namespace My.Namespace { using H = MyHelperClass; public class MyHelperClass { public static void HelperFunc1() { Console.WriteLine("Here's your help!"); } } public class MyHelpe ... -
静态方法可以调用实例方法 - 但是你需要有一个实例来调用它们。 这个例子来自哪里并不重要,例如: int a = 10; int b = 100; Program program = new Program(); program.outTestMethod(a,out b); 实例方法与特定类型的实例相关联,而静态方法与整体类型相关 - 而其他类型的成员也是如此。 因此,要调用一个实例方法,您需要知道您感兴趣的实例。例如,具有以下内容是没有意义的: string name = Person.Name; ...
-
您可以将嵌套类设置为static 。 您将失去具有封闭父类实例的约束,但它将允许您从静态方法操作DoubleNode : // This will compile public class DoublyLinkedList
- { public static
void insertStart(DoublyLinkedList list, DoubleNode first) { // implementation } private st ... - { public static
-
静态成员的引用方法(Reference methods from static member)[2021-03-17]
PLUS = 0 MINUS = 1 OPERATIONS = [PLUS, MINUS] class Generator: operations = {} def __init__(self, state): self._state = state @classmethod def init_operations(cls): cls.operations = { PLUS: cls.plus, ...