标签: static-methods

函数与静态方法

什么在多线程程序中使用函数或使用静态方法更快?请解释为什么它更快.

java methods static-methods

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

为什么applet中允许使用非静态方法?

我注意到在java-application中向包含main方法的类添加一个非静态方法然后在main-method中调用它会导致编译错误.我可以理解,因为这个类永远不会被实例化.

但是,将一个非静态方法添加到Applet类并从内部调用它,比如paint方法可以正常工作.为什么是这样?小程序类是以某种方式由appletviewer实例化的,还是有另一种解释为什么前者不被允许而后者是?

java static-methods appletviewer

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

如何在jstl中调用静态方法?

我已经看到这个问题已在这里得到解答,但是当我尝试相同的方法时,它没有用.这是我的代码:

package linear_programming.matrix;

import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 *
 * @author Jevison7x
 */
public final class MatrixOperations<T extends Number>
{
    /**
 * This method round's off decimal numbers to two decimal places.
 * @param d The decimal number to be rounded off.
     * @param decPlaces The number of decimal places to round off.
 * @return the rounded off decimal number.
 */
public static double roundOff(double d, int decPlaces)
{ 
        if(decPlaces < 0)
            throw new IllegalArgumentException("The number of decimal …
Run Code Online (Sandbox Code Playgroud)

java static-methods jstl taglib

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

MonoTouch - 如何覆盖静态 UIView.layerClass

我正在尝试将AUISelectiveBordersView 移植到 MonoTouch。它基本上是通过类别的子类CALayer和集成UIView

AUISelectiveBordersLayer 的“翻译”很容易,但集成点有点棘手。在 obj-c 中,它是这样完成的:

@implementation UIView (AUISelectiveBorder)

+(Class) layerClass {
    return [AUISelectiveBordersLayer class];
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以将其转换为 MonoTouch?它看起来像重写静态方法,但我没有看到像什么layerClasslayerType在MT。

static-methods objective-c xamarin.ios

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

C++无法在类中调用成员函数

我想知道为什么不能从静态函数调用成员函数

#include <iostream>

class A{
public:
  A(){}
  ~A(){}
  static void astaticFunction(){
    printHello();
  }

private:
  void printHello(){
    std::cout << "Hello" << std::endl;
  }

};

int main(int argc, char **argv){
  A::astaticFunction();
  return 0;

}
Run Code Online (Sandbox Code Playgroud)

编译器正在返回此信息

main.cpp: In static member function ‘static void A::astaticFunction()’:
main.cpp:8:16: error: cannot call member function ‘void A::printHello()’ without object
Run Code Online (Sandbox Code Playgroud)

我该如何使用它?谢谢

c++ static-methods

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

将方法定义为静态更好吗?

是否存在内存或性能差异来静态定义和使用方法而不是使用实例化类中的方法?当方法不是静态时,方法本身是否会使用类的每个实例占用内存?

也许在静态方法中声明的任何变量都是非线程安全的?

/// <summary>
/// A class using instance methods (non-static)
/// </summary>
public class Junk
{
    public int x {get; protected set;}
    public Junk(int x = 0)
    {
        this.x = x;
    }
    public void Increment()
    {
        this.x++;
    }
}
Run Code Online (Sandbox Code Playgroud)

/// <summary>
/// A class using static methods
/// </summary>
public class Junk
{
    public int x {get; protected set;}
    public Junk(int x = 0)
    {
        this.x = x;
    }
    public static void Increment(Junk thisJunk)
    {
        thisJunk.x++;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# static static-methods

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

在Java中继承静态方法?

所以我知道在Java中,当你有一个静态方法时,你应该用格式来调用它,ClassName.method()而不是使用与实例方法相同的结构,即:

ClassName myObject = new ClassName();
myObject.method();
Run Code Online (Sandbox Code Playgroud)

但是,如果你这样做,它仍然是有效的代码,并将工作.假设我决定在有问题的方法是静态的情况下执行此操作,并进行以下设置:

public SuperClass {
    public static int foo(int x) {
        return x;
    }
}

public SubClass extends SuperClass {
    public static int foo(int x) { // Overriding foo() in SuperClass
        return x + 1;
    }
}

public MyDriver {
    public static void main(String[] args) {
        SuperClass myObject = new SubClass(); // Upcasting.
        System.out.println(myObject.foo(5));  // This should polymorphically print 6
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,在屏幕上打印的是5而不是6.为什么?

java polymorphism inheritance static-methods

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

从另一个访问私有静态方法

我有一个私有静态方法的主类.我想从另一个java类访问此方法.我试过一些方法,但是他们没有用.我该如何访问该方法?

像这样的主要班级;

public class RandomGenerate {

    public static void main(String[] args) throws Exception {
        System.out.print.ln("main method");
    }

    private static synchronized void createRandom(PersonObj person, int number, List s) {
        System.out.println("deneme");
    }
}
Run Code Online (Sandbox Code Playgroud)

我想createRandom从另一个像这样的java类调用;

public class Deneme {
    RandomGenerate rg = new RandomGenerate();
    RandomGenerate.createRandom(person, number, sList);
}
Run Code Online (Sandbox Code Playgroud)

然后,netbeans显示方法具有私有访问权限.

java static-methods private-methods

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

如何从静态方法改变C++中对象的属性

如何在C++中从静态方法更改对象属性?我的方法必须是静态的.

码:

class Wrapper
{
    private:
        double attribute; //attribute which I want to change
    public:
        Wrapper();
        ~Wrapper();
        static void method(double x);
}
Run Code Online (Sandbox Code Playgroud)

我试过了:

std::string Wrapper::method(double x)
{
    attribute = x;
}
Run Code Online (Sandbox Code Playgroud)

但:

error: invalid use of member ‘Wrapper::attribute’ in static member function
Run Code Online (Sandbox Code Playgroud)

c++ static-methods

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

为什么有人会在java 1.8中的接口中定义静态方法?

为什么任何人在JAVA 1.8中的接口中定义静态方法?

我需要知道静态方法会派上用场的不同示例/用例/要求.

接口中的静态方法如何对开发人员有益?

java static-methods interface java-8

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