在类中实现静态方法

时间:2013-11-29 03:24:53

标签: java class methods static

从我正在阅读的一本书中:

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

......等等,等等,等等......

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

这是我到目前为止所得到的。使用object.isEven()...

可以轻松实现top

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

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

澄清:

1

public boolean isEven(){
     // code
}

MyInteger object = new MyIntger(50);
object.isEven();

2

public boolean isEven(int num){
    // code
}

MyInteger.isEven(50)???

3

public boolean isEven(int MyInteger)???

???

5 个答案:

答案 0 :(得分:1)

    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");
        }

    }

答案 1 :(得分:0)

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

MyInteger myInteger = new MyInteger(100);

您可以通过此

调用该方法
MyInteger.isEven(50);

myInteger.isEven(50);

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

答案 2 :(得分:0)

这似乎令人困惑

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);

    }
}

答案 3 :(得分:0)

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

假设您的私有变量和构造函数看起来像这样(我们并不完全知道,因为它没有发布):

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类。

答案 4 :(得分:0)

将此视为指针,然后您可能需要查看this question

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;
  }
}
相关问题