我注意到在java-application中向包含main方法的类添加一个非静态方法然后在main-method中调用它会导致编译错误.我可以理解,因为这个类永远不会被实例化.
但是,将一个非静态方法添加到Applet类并从内部调用它,比如paint方法可以正常工作.为什么是这样?小程序类是以某种方式由appletviewer实例化的,还是有另一种解释为什么前者不被允许而后者是?
我已经看到这个问题已在这里得到解答,但是当我尝试相同的方法时,它没有用.这是我的代码:
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) 我正在尝试将AUISelectiveBordersView 移植到 MonoTouch。它基本上是通过类别的子类CALayer和集成UIView。
AUISelectiveBordersLayer 的“翻译”很容易,但集成点有点棘手。在 obj-c 中,它是这样完成的:
@implementation UIView (AUISelectiveBorder)
+(Class) layerClass {
return [AUISelectiveBordersLayer class];
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以将其转换为 MonoTouch?它看起来像重写静态方法,但我没有看到像什么layerClass或layerType在MT。
我想知道为什么不能从静态函数调用成员函数
#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)
我该如何使用它?谢谢
是否存在内存或性能差异来静态定义和使用方法而不是使用实例化类中的方法?当方法不是静态时,方法本身是否会使用类的每个实例占用内存?
也许在静态方法中声明的任何变量都是非线程安全的?
/// <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) 所以我知道在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类访问此方法.我试过一些方法,但是他们没有用.我该如何访问该方法?
像这样的主要班级;
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显示方法具有私有访问权限.
如何在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) 为什么任何人在JAVA 1.8中的接口中定义静态方法?
我需要知道静态方法会派上用场的不同示例/用例/要求.
接口中的静态方法如何对开发人员有益?
static-methods ×10
java ×6
c++ ×2
appletviewer ×1
c# ×1
inheritance ×1
interface ×1
java-8 ×1
jstl ×1
methods ×1
objective-c ×1
polymorphism ×1
static ×1
taglib ×1
xamarin.ios ×1