标签: static-methods

java中的静态块如何工作?

据我所知,静态字段和块是在类中从上到下处理的。我的意思是,必须先声明一个字段(静态字段),然后才能在静态块中使用它。必须首先声明静态字段,然后使用静态块修改该静态字段。正确的?

像这样:

private static Map<String, Object> map = new HashMap<>();
static {
    map.put("key", "value");
}
Run Code Online (Sandbox Code Playgroud)

它可以编译。如果我们像这样颠倒顺序:

static {
    map.put("key", "value");
}
private static Map<String, Object> map = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)

它无法按预期编译。

但是,如果我这样写。

static {
    map = new HashMap<>();
}
private static Map<String, Object> map;
Run Code Online (Sandbox Code Playgroud)

就顺利通过了?!有人知道发生了什么事吗?它不介意我是否为map分配一个新对象,但如果我在地图上放置一些东西,它就无法编译。

请给我一个合理的答案。

java static-methods

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

静态方法中的 if/else 语句问题

package geometrypack;

public class Calc {
    public static double areaOfCircle(int radius) {
        if (radius <= 0) {
            System.out.println("Input cannot be a negative number.");
        }
        return (Math.PI * (radius * radius));
        
    } // areaOfCircle method
    
    public static double areaOfRectangle(int length,int width) {
        if (length <= 0 || width <= 0) {
            System.out.println("Input cannot be a negative number.");
        }
        return length * width;
        
    } // areaOfRectangle method
    
    public static double areaOfTriangle(int base, int height) {
        if (base <= 0 || height <= …
Run Code Online (Sandbox Code Playgroud)

java static-methods conditional-statements

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

这个PHP静态方法有什么问题?

我在课堂上声明了一个静态方法 category

public static function getPrefixFromSubCategoyId($subCategoryId) {
    $prefix = $this->fetch(array('table' => 'subCategories', 'id' => $subCategoryId));
    return $prefix[0]['prefix'];
}
Run Code Online (Sandbox Code Playgroud)

我确信我正在使用正确的代码片段,因为当我在类范围之外使用相同的代码并使用以下代码时,它可以正常工作

$category = new Category($dbh);
$subCategoryId = 6;
$prefix = $category->fetch(array('table' => 'subCategories', 'id' => $subCategoryId));
echo $prefix[0]['prefix'];
Run Code Online (Sandbox Code Playgroud)

但是当我使用以下语法初始化静态方法时.

$prefix = Category::getPrefixFromSubCategoyId(4);
Run Code Online (Sandbox Code Playgroud)

它给了我以下错误.

Fatal error: Using $this when not in object context
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?或者我是以错误的方式宣布它?

谢谢..

php static static-methods

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

调用静态方法后重用类

假设我有一个带有几个静态void方法的类,例如:

class MyClass {
    public static void doJob() {
        // ...
    }
    public static void doSmthElse() {
         // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

如何修改它来调用我的静态方法:

MyClass.doJob().doSmthElse().doJob();
Run Code Online (Sandbox Code Playgroud)

代替

MyClass.doJob();
MyClass.doSmthElse();
MyClass.doJob();
Run Code Online (Sandbox Code Playgroud)

我知道如何使用非静态方法(只返回这个),但如何使用静态字段?

java static static-methods method-chaining

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

是否有可能在C++中的静态成员方法中调用非静态成员方法?

我怀疑这是可能的,但值得一提的是:我想从静态成员函数中调用非静态成员函数.我想为班级的每个当前实例做这件事.可能吗?

当我在测试类中尝试这个时,我收到以下错误:

"无法调用成员函数'void TestClass :: NonStaticMethod()'没有对象"

c++ static-methods

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

private static ClassName ClassInstanceVariableName = null; .....这是什么?

那么这里有一个静态实现,我不明白.我以前使用静态但不广泛,任何人都可以帮助我理解代码.这是代码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class Connection_Class {
String driver_ClassName="com.mysql.jdbc.Driver";
String URL_connection="jdbc:mysql://localhost:3306/vendor";
String user="root";
String password="lifesuckzz";
   //can anybody explain what the following line means, especially the static part.......
    private static Connection_Class connectionclass=null;

private Connection_Class(){
    try{
        Class.forName(driver_ClassName);

    }catch(ClassNotFoundException e){
        e.printStackTrace();
    }
}

public Connection getConnection() throws SQLException{
    Connection con=null;
    con=DriverManager.getConnection(URL_connection,user,password);
    return con;
}

public static Connection_Class getInstance(){
    if(connectionclass==null){
                 //I know its returning an instance here
        connectionclass=new Connection_Class();
    }
    return connectionclass;
}


}
Run Code Online (Sandbox Code Playgroud)

java methods static static-methods

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

是否存在命名空间单例类的快捷方式

我目前的代码如下:

namespace Libraries;

class_alias('Libraries\ORM', 'ORM');

class ORM 
{
    public function __construct() {}

    static public function someMethod()
    {
        // do something
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为我可以快速命名空间,如上所示,所以我只需要调用ORM::someMethod();而不是\Libraries\ORM::someMethod();

(我在另一个命名空间中使用ORM类,让我们说"项目")

这是可能的还是什么是正确的解决方案?我知道我可以在类存储在一个全局命名空间,但我仍然需要使用全球斜线,如:\ORM::someMethod();.

谢谢!

php static-methods namespaces

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

使用泛型的静态方法的正确使用方法是什么?

我试图制作一个使用泛型的静态方法.

我希望该方法只采用实现可比较的类型的参数.

我试图这样做:

public static <T extends Comparable<T>> ArrayList<T extends Comparable<T>> foo(ArrayList<T extends Comparable<T>> bar)
Run Code Online (Sandbox Code Playgroud)

但它会导致语法错误.

java generics static-methods extends

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

在用于构造对象的静态方法中使用"this"

这就是我的意思.

我有一个像这样的构造函数的类:

Class() : base(GetConstructorArgument1(), GetConstructorArgument2())
{
}
Run Code Online (Sandbox Code Playgroud)

因为尽可能在基数调用中创建参数会非常麻烦和粗糙.必要时,这两种GetConstructorArgument方法都是静态的.

GetConstructorArgument2()创建一个lambdas数组.在那些lambdas中,我想做"这个",我正在构建的对象.

有没有办法做到这一点?

c# lambda constructor static-methods

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

使用静态方法泛型时不兼容的类型

当我尝试编译以下代码时,编译失败并出现以下错误.我不知道为什么它应该,因为我只返回一个实现合同的类

public interface Contract {
  static <T extends Contract> T get() {
    return new ConcreteContract();
  }
}

class ConcreteContract implements Contract {
}
Run Code Online (Sandbox Code Playgroud)
Contract.java:3: error: incompatible types: ConcreteContract cannot be converted to T
    return new ConcreteContract();
           ^
  where T is a type-variable:
    T extends Contract declared in method <T>get()
1 error
Run Code Online (Sandbox Code Playgroud)

有没有人知道为什么java表现这种方式(或)我错过了一些明显的东西

PS:在发布此查询之前,我已在SO中阅读了超过10个热门搜索

java generics static-methods

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