【java – 如何在任何模拟框架中模拟静态方法,如JMockit,PowerMocks?】教程文章相关的互联网学习教程文章

java – 非静态方法是googlePlayServicesAvailable,并且无法从静态上下文引用getErrorDialog【代码】

我正在编写此代码,它显示错误非静态方法GoogleApiAvailability.isGooglePlayServicesAvailable(上下文上下文)和GoogleApiAvailability.getErrorDialog(Activity activity,int errorCode,int requestCode)无法从静态上下文引用.package com.xamarin.gcmexample; import android.app.Dialog; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.google.android.gms.common.ConnectionResult; i...

java – 静态同步方法和非同步静态方法的混淆【代码】

我有一点困惑.请看下面的代码.public class ThreadDemo {//non-static synchronized methodsynchronized void a(){actBusy();}//static synchronized methodstatic synchronized void b(){actBusy();}//static methodstatic void actBusy(){try{Thread.sleep(1000);}catch(InterruptedException e) {e.printStackTrace();}}public static void main(String[] args){final ThreadDemo x = new ThreadDemo();final ThreadDemo y = ne...

Java – 能够访问静态方法的实例【代码】

我刚刚开始使用Java,正在查看嵌套类主题,并尝试了一些东西,当突然,这发生了:class Encloser {static int i;static void m1(){System.out.println(i);}static void m2(){Enclosee.accessEncloser();}static class Enclosee{static void accessEncloser(){i = 1;m1();}static void accessEncloserNew(){m2();}} }class EncloserTest {public static void main(String[] args){Encloser ee = new Encloser();Encloser.Enclosee e = n...

可以在Kotlin中调用Java静态方法【代码】

假设我们有一个Java静态方法://Java code public static void printFoo() {System.out.println("foo"); }可以在Kotlin中调用该方法吗?解决方法:是的你可以.Java代码:public class MyJavaClass {public static void printFoo() {System.out.println("foo");} }Kotlin代码:fun main(args: Array<String>) {MyJavaClass.printFoo() }这么容易=)

java – 添加静态方法以促进更清晰的单元测试 – 良好实践?【代码】

说我有这个班:class MyClass {private String s;// more attributes herepublic MyClass(String s, /*more constructor params*/) {...}public String myMethod(String s) {//complex logic here} }要对单元测试myMethod()我需要创建整个对象(需要构造许多参数等),而方法只使用s. Altenatelly我可以添加一个静态方法:class MyClass {private String s;// more attributes herepublic MyClass(String s, /*more constructor para...

如何在java中的静态方法中使用此关键字?【代码】

有没有办法在Java中的静态方法中使用此关键字?我想在我的activity类中的静态方法中显示Toast消息.我怎么做?谢谢.解决方法:您可以使用一个输入参数创建一个静态方法,该参数是您需要使用的类. 例如:public static void showMyTouch(MyActivity act, String message){Toast.makeText(act, message, Toast.LENGTH_LONG).show(); }

java – 静态方法有原因吗?【代码】

我似乎无法理解静态方法.我阅读了很多关于它的文章,并在教科书和Java文档中对它进行了研究.我知道你可以使用静态方法来访问静态变量.除了获取静态变量之外,除了类方法之外还有其他原因吗?如果还有其他原因我可以解释为什么?我也创建了这个帖子,因为我没有在SOF中找到任何关于此的内容. 这是一个示例代码:public class Bicycle {private int cadence;private int gear;private int speed;private int id;private static int numb...

新线程中的Java Run静态方法【代码】

我刚刚开始学习java,我遇到了涉及线程的轻微障碍.我有一个静态方法,我想在自己的线程中运行,这可能吗?在python我知道它看起来像这样:import thread; thread.start_new_thread(my_function,());我知道如何通过实现Runnable或扩展线程来使用非静态方法进行线程化,但这不是我想要做的.解决方法:让Thread的run方法调用静态方法:new Thread(new Runnable() {public void run() {Call.yourStaticMethod();} }).start();

java – 如果我调用静态方法,构造函数是否运行【代码】

我有一个名为Tile的类,带有静态方法public static BufferedImage grass(),类里面也是一个构造函数public Tile(),它设置一个变量. 所以我想知道的是,如果我从另一个类Tile.grass()调用grass()方法,构造函数会运行吗? 我假设不是因为我的grass()方法返回null. 这是我的代码:import java.awt.image.BufferedImage; import java.io.IOException; import javax.imageio.ImageIO;public class Tile {public static final int size = 50...

同步涉及java中的静态方法【代码】

假设我有一个Utility类,public class Utility {private Utility() {} //Don't worry, just doing this as guarantee.public static int stringToInt(String s) {return Integer.parseInt(s);} };现在,假设在多线程应用程序中,线程调用了Utility.stringToInt()方法,当操作进入方法调用时,另一个线程调用传递不同s的相同方法.在这种情况下会发生什么? Java会锁定静态方法吗?解决方法:这里没有问题.每个线程都将使用自己的堆栈,因此...

为什么java中的静态方法只接受其方法中的final或非final变量,而不是静态?【代码】

为什么java中的静态方法只接受其方法中的final或非final变量,而不是静态? 例如,我有以下方法:public static void myfunc(int somethig) { int a=10; final int b=20; static int c=30; //gives Error why? }解决方法:由于java中的每个函数都必须在一个类中,因此可以通过声明类中的字段来获得相同的效果.这是最简单的方法,而且java语言设...

Java静态方法的标准?

有人告诉我:If you are using Eclipse and don’t see any blue words (i.e. member variables) in your methods, then those methods should really be static methods, as long as the parameters (if there are any) are primitive types, or (in the case of object references) are immutable and/or thread-safe.在决定实例方法是否真的应该是静态方法时,Java开发人员应该考虑其他任何标准吗?解决方法:简单地说,如果它是纯...

Java静态方法参数【代码】

为什么以下代码返回100 100 1 1 1而不是100 1 1 1 1?public class Hotel { private int roomNr;public Hotel(int roomNr) {this.roomNr = roomNr; }public int getRoomNr() {return this.roomNr; }static Hotel doStuff(Hotel hotel) {hotel = new Hotel(1);return hotel; }public static void main(String args[]) {Hotel h1 = new Hotel(100);System.out.print(h1.getRoomNr() + " ");Hotel h2 = doStuff(h1);System.out.print(...

android – java.lang.NoSuchMethodError:没有静态方法zzb【代码】

每当我在手机上运行我的应用程序时,我都会收到错误消息:FATAL EXCEPTION: main E/AndroidRuntime(18680): java.lang.NoSuchMethodError: No static method zzb(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; in class Lcom/google/android/gms/common/internal/zzaa; or its super classes (declaration of 'com.google.android.gms.common.internal.zzaa' appears in /data/app/***/base.apk) E/AndroidRuntime(1868...

java – 无静态方法combineMeasuredStates(II)实现底部导航视图时【代码】

我正在尝试实现底部导航视图但每次运行项目时logcat都会出现此错误java.lang.NoSuchMethodError: No static method combineMeasuredStates(II)I in class Landroid/support/v7/widget/ViewUtils; or its super classes (declaration of 'android.support.v7.widget.ViewUtils' appears in /data/app/lungu.aubry.quick-2/split_lib_dependencies_apk.apk:classes28.dex)at android.support.design.internal.BaselineLayout.onMeasu...

静态方法 - 相关标签
框架 - 相关标签