【问题标题】:Implementing Static Methods In A Class在类中实现静态方法
【发布时间】:2013-11-29 03:24:53
【问题描述】:

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

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

……呜呜呜……

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

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

第二个,我假设这只是为了显示结果而不实际设置值和更改对象?所以我可以只做 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)???

???

【问题讨论】:

  • 你的类有一些静态方法,问题到底是什么??
  • 什么是object.isEven()?另外,请发布您的代码。
  • 首先,如果你不明白实例方法和静态方法的区别,请说出来。我假设您确实了解这种区别。第二组和第三组方法的参数形式不同。第二组采用普通的 int 值,而第三组采用 MyInteger 类的实例(其中可能包含一个数值)。
  • 您调用类的静态方法,例如MyInteger.isEven(27)
  • 您的方法应返回 boolean 指定的值。这就是他们应该做的一切。

标签: java class methods static


【解决方案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");
        }

    }

【讨论】:

    【解决方案2】:

    这似乎是一个令人困惑的问题

    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】:

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

    MyInteger myInteger = new MyInteger(100);
    

    你可以通过这个调用方法

    MyInteger.isEven(50);
    

    myInteger.isEven(50);
    

    与对象中设置的100无关。

    【讨论】:

    • 应该注意的是,第二种形式纯粹是编译器调配。 myInteger 引用可以为 null,它仍然可以工作,因为它基于引用的类型,而不是引用所寻址的对象(如果有)的类型。
    【解决方案4】:

    将此视为一个指针,然后您可能想查看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;
      }
    }
    

    【讨论】:

      【解决方案5】:

      您将在 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 类。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-10
        • 1970-01-01
        • 2015-09-10
        • 2010-10-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多