标签: static-methods

Javascript - 无法对非静态函数进行静态引用

我正在引用数组上的Javascript函数splice(),我得到错误:

"无法对非静态函数splice()进行静态引用"

发生了什么 - 这是一个静态引用,不是我引用一个Array类的实例及其方法 - 这是怎么样的静态?

$(document).ready( function() {

var queryPreds = new Array();
var queryObjs = new Array();    

function remFromQuery(predicate) {
    for(var i=0; i<arrayName.length;i++ ) { 
        if(queryPreds[i]==predicate)
        queryPreds.splice(i,1);
        queryObjs.splice(i,1);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

javascript arrays static-methods

2
推荐指数
1
解决办法
386
查看次数

C#可以在main之前或之后运行代码,就像在Objective C中一样

http://gcc.gnu.org/onlinedocs/gcc-2.95.3/objc-features_1.html#SEC2

GNU Objective-C运行时提供了一种允许您在程序执行进入main函数之前执行代码的方法.代码通过特殊的类方法+加载在每个类和每个类别的基础上执行.

更新:我在下面阅读的答案并不令人满意.从主程序调用函数没什么特别之处.问题是关于HOOKING SYSTEM是SYSTEM在没有你的程序的情况下调用函数,即使在RUNTIME上也知道它.

而不是Objective C,请参阅Visual C++上的这篇文章(感谢stackoverflow的人回答我之前的问题):http: //www.codeguru.com/cpp/misc/misc/threadsprocesses/article.php/c6945

否则,Objective C Runtime不需要包含此加载方法.主要的入口点当然存在于Objective C程序中,如果只需要在主方法中调用静态方法就没什么大不了的:)

c# static-methods objective-c

2
推荐指数
1
解决办法
1653
查看次数

静态方法和变量

我知道在java中静态方法只能使用静态变量和静态方法,但非静态方法可以使用非静态变量和方法.有没有解释为什么静态方法只能访问静态变量/方法?并且无法访问非静态方法和变量?

java static static-methods static-members

2
推荐指数
1
解决办法
1709
查看次数

静态方法中的Javascript"this"

我有这样的代码:

User = function(){}

User.a = function(){
  return "try";    
}

User.b = function(){

}
Run Code Online (Sandbox Code Playgroud)

从User.b()我可以使用以下方法调用User.a():

User.b = function(){
    return User.a();
    }
Run Code Online (Sandbox Code Playgroud)

但不使用它,因为它不是用户的实例(User.a()和User.b()类似于"静态方法").

我想要做的是能够调用从User.b User.a()()不知道这是主要的功能,在这种情况下用户.

在静态方法中使用这样的东西.

javascript oop static-methods class

2
推荐指数
2
解决办法
9023
查看次数

没有静态的PHP函数仍然可以像静态一样调用

我已经注意到在PHP中我可以创建一个没有静态修饰符的函数,但仍然将其称为静态函数,只是好奇为什么这是允许/实际发生的事情.

class Foo {
   public function bar($i) {
       return $i + 1;
   }
}

Foo::bar(4); // 5
Run Code Online (Sandbox Code Playgroud)

我希望静态修饰符是必需的,如:

class Foo {
   public static function bar($i) {
       return $i + 1;
   }
}

Foo::bar(4); // 5
Run Code Online (Sandbox Code Playgroud)

php static-methods

2
推荐指数
1
解决办法
58
查看次数

无法对非静态方法进行静态引用

到目前为止,我有以下代码:

import java.util.Scanner;

public class HallLanceMemoryCalculator {
private double currentValue;

public static int displayMenu(){

    Scanner input=new Scanner(System.in);

    int choice=0;

    while(choice<1||choice>5){      
    System.out.println("1.Add");
    System.out.println("2.Subtract");
    System.out.println("3.Multiply");
    System.out.println("4.Divide");
    System.out.println("5.Clear");

    System.out.println("What would you like to do?");
    choice=input.nextInt();
    }
    return choice;
}

public static double getOperand(String prompt){
    Scanner input=new Scanner(System.in);
    System.out.println("What is the second number?");
    double secondNumber=input.nextDouble();
    return secondNumber;
}

public  double getCurrentValue(){
    return currentValue;
}

public void add(double operand2){
    currentValue+=operand2;
}

public void subtract(double operand2){
    currentValue-=operand2;
}

public void multiply(double operand2){
    currentValue*=operand2;
}

public void …
Run Code Online (Sandbox Code Playgroud)

java static-methods

2
推荐指数
2
解决办法
3万
查看次数

何时将整个类声明为静态

我有一个数学辅助类,其中每个函数都是静态的,即作为参数输入的参数,返回值.我应该将整个类声明为静态吗?将静态修改器添加到类中是否会对性能产生影响?

此外,我不确定此指南的含义:"不要将静态类视为杂项." - 我有几个类只是一堆杂项静态函数...

.net c# oop static-methods static-classes

2
推荐指数
1
解决办法
219
查看次数

我应该使用静态方法(C#)

我应该在以下两种情况下使用静态:

情况1)

public class RequestHeader
{
    private string Username { get; set; }
    private string Password { get; set; }
    private string AccessKey { get; set; }

    public string url { get; set; }
    public string pageid { get; set; }
    public string organizationid { get; set; }

    private RequestHeader()
    {
    }

    public static RequestHeader GetRequestHeader(string url, string pageid, string organizationid)
    {
        return new RequestHeader()
        {
            Username = "Some logic to fetch username",
            Password = "Some logic to fetch password", …
Run Code Online (Sandbox Code Playgroud)

c# static static-methods

2
推荐指数
1
解决办法
283
查看次数

从Rust中同一个类的另一个静态方法引用静态方法的最佳方法是什么?

在一个新的Rust模块中,我可以写:

struct myStruct {
    x : u32
}

impl myStruct {
    fn new() -> myStruct{
        myStruct{ x : other()}
    }

    fn other() -> u32 {
        6
    }
}
Run Code Online (Sandbox Code Playgroud)

来自其他OO语言,我希望other()能够参与其中new().也就是说,我希望能够从同一个类的另一个静态方法调用一个类的静态方法.但是,rustc会产生以下信息:

error[E0425]: cannot find function `other` in this scope
 --> dummy_module.rs:9:23
  |
9 |         myStruct{ x : other()}
  |                       ^^^^^ not found in this scope
Run Code Online (Sandbox Code Playgroud)

相比之下,以下Java代码编译良好:

public class myStruct{
    int x;

    public myStruct(){
        x = other();
    }

    private int other(){
        return 5;
    }
}
Run Code Online (Sandbox Code Playgroud)

我不记得在我正在使用的Rust书中看到任何提及这一点,我似乎无法在网上找到明确的答案.我可以通过显式确定对其他人的调用来修复它myStruct::other(),但这看起来很麻烦.如果我尝试use …

static-methods module rust

2
推荐指数
2
解决办法
871
查看次数

const静态成员函数:是否可以使用另一种方法?

本问答中所述,const静态成员函数在C ++中不可用。此后(2011年)有什么变化吗?

有没有其他方法可以使静态成员函数不修改其类的静态成员?

类似于(伪代码)的东西:

class C
{
   static int a;
public:
   static void Incr() { ++a; }
   static int  Ret() const { return a; }
};

int C::a = 0;
Run Code Online (Sandbox Code Playgroud)

我需要从另一个类的const成员函数中调用[const]静态成员函数。

c++ static-methods const

2
推荐指数
2
解决办法
93
查看次数