在类中实现静态方法



来自我正在阅读的一本书:

"设计一个类名MyInteger。该类包含:

。等等,等等,等等...

  • 方法 isEven((、isOdd(( 和 isPrime(( 如果此对象中的值分别为偶数、奇数或素数,则返回 true。
  • 静态方法 isEven(int(、isOdd(int( 和 isPrime(int(,如果指定的值分别为偶数、奇数或素数,则返回 true。
  • 静态方法是 Even(MyInteger(、isOdd(MyInteger(、isPrime(MyInteger(,如果指定的值分别为偶数、奇数或素数,则返回 true。

这是我到目前为止所得到的。顶部很容易通过object.isEven((实现...

第二个,我认为这只是为了显示结果而不实际设置值和更改对象?所以我可以只做 object.isEven(2(?

最后一个...这让我很失望。我不知道。=/请帮帮我。提前谢谢。

澄清一下:

1.

public boolean isEven(){
     // code
}
MyInteger object = new MyIntger(50);
object.isEven();

阿拉伯数字。

public boolean isEven(int num){
    // code
}
MyInteger.isEven(50)???

3.

public boolean isEven(int MyInteger)???
???
    class MyInteger {
int number;
// CONSTRUCTOR
public MyInteger(int a) {
    number = a;
}
public int getNumber() {
    return number;
}
static boolean isEven(MyInteger myint) {
    if (myint.getNumber() % 2 == 0)
        return true;
    else
        return false;
}
    }

现在主类:

    public class MainClass {
/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    MyInteger myInteger=new MyInteger(10);
    boolean result=MyInteger.isEven(myInteger);
    if(result==true)
        System.out.println("true result");
    else
        System.out.println("false result");
        }
    }

这似乎是令人困惑的

boolean odd2 = MyInteger.isOdd(new MyInteger(5));  // static call

使用MyInteger实例作为参数传递。传递MyInteger作为参数的另一种方法是:

MyInteger num = new MyInteger(5);
boolean odd2 = MyInteger.isOdd(num);  // static call 

class MyInteger{
    int num;
    public MyIntger(int num){
        this.num = num;
    }
    // Method 1
    public static boolean isOdd(int num){
        ...
    }
    // Method 2
    public boolean isOdd(){
        ...
    }
    // Method 3
    public static boolean isOdd(MyInteger num){
        ...
    }
}
public class TestMyInteger{
    public static void main(String[] args){
        // Method 1 call
        boolean odd1 = MyIntger.isOdd(5);    // static call
        // Method 3 call
        boolean odd2 = MyInteger.isOdd(new MyInteger(5));  // static call
        // Method 2 call
        MyIntger num = new MyIntger(5);  // create instance
        boolean odd3 = num.isOdd();   // instance call
        System.out.println(odd1);
        System.out.println(odd2);
        System.out.println(odd3);
    }
}

对于第二个,该方法属于类。但不是创建的对象。如果你的代码像这样:

MyInteger myInteger = new MyInteger(100);

你可以通过这个调用该方法

MyInteger.isEven(50);

myInteger.isEven(50);

它与在对象中设置的 100 无关。

将此视为一个指针,然后您可能想查看此问题。

public class MyInteger {
  private int value;
  public MyInteger(int value) {
    super();
    this.value = value;
  }
  public static boolean isPrime(int value) {
    // I would increment counter then test if the result of value modulo counter 
    // (that is if value % counter != 0) until counter >= square_root(value). 
    // Then the value is prime, otherwise 
    return false;
  }
  public static boolean isEven(int value) {
    return (value & 1) == 0;
  }
  public static boolean isEven(MyInteger m) {
    return isEven(m.value);
  }
  public static boolean isPrime(MyInteger m) {
    return isPrime(m.value);
  }
  public static boolean isOdd(int value) {
    return !isEven(value);
  }
  public static boolean isOdd(MyInteger m) {
    return isOdd(m.value);
  }
  public boolean isEven() {
    return isEven(this.value);
  }
  public boolean isOdd() {
    return isOdd(this.value);
  }
  public boolean isPrime() {
    return isPrime(value);
  }
  public int getValue() {
    return value;
  }
  public void setValue(int value) {
    this.value = value;
  }
}

您将对 MyInteger 对象执行操作,而不仅仅是直接 int。

假设您的私有变量和构造函数如下所示(我们不知道确切,因为它没有发布(:

private int myInt;
public MyInteger(int thisInt) {
    myInt = thisInt;
}

您需要实现一个在 MyInteger 类的实例中返回 myInt 值的访问器方法,然后在静态方法中使用此访问器方法来执行该操作。

因此,作为访问器方法:

public int getInt()
{
    return myInt;
}

然后,静态方法将以与在另一个程序中相同的方式引用此方法。请注意,即使在类中,您也必须指定 MyInteger 对象的使用:

public static boolean isEven(MyInteger myInteger)
{
    //Code here
}

在调用静态方法方面,它看起来像这样:

MyInteger myInteger = new MyInteger(50);
MyInteger.isEven(myInteger);

在这里,您引用的是 MyInteger 对象 (myInteger( 的实例,而不是基元 int,但由于 isEven 没有直接连接到特定对象,因此您必须告诉您的代码在哪里可以找到 isEven(( 方法,即 MyInteger 类。

最新更新