【问题标题】:Java: Best practice to define a method in relation to a classJava:定义与类相关的方法的最佳实践
【发布时间】:2014-06-18 14:04:48
【问题描述】:

我得到了以下练习:

“鉴于这个类,

class Test { int a; Test(int a) { this.a = a; } }

编写一个名为 swap() 的方法,用于交换两个 Test 对象引用所引用的对象的内容。”

我写了三个略有不同的练习示例:

示例 1

class Test {
   public int a;

   Test(int a) {
      this.a = a;
   }

   public void swap(Test otherObject) {
   int tempVar;

   tempVar = this.a;
   this.a = otherObject.a;
   otherObject.a = tempVar;
   }
}

class Chapter6_2c {
   public static void main(String[] args) {
      Test obj1 = new Test(1);
      Test obj2 = new Test(2);

      System.out.println("obj1 has value " + obj1.a);
      System.out.println("obj2 has value " + obj2.a);

      obj1.swap(obj2);

      System.out.println("\nafter swap()\n");
      System.out.println("obj1 has value " + obj1.a);
      System.out.println("obj2 has value " + obj2.a);
   }
}

示例 2

class Test {
   public int a;

   Test(int a) {
      this.a = a;
   }

   public static void swap(Test objectOne, Test objectTwo) {
   int tempVar;

   tempVar = objectOne.a;
   objectOne.a = objectTwo.a;
   objectTwo.a = tempVar;
   }
}

class Chapter6_2b {
   public static void main(String[] args) {
      Test obj1 = new Test(1);
      Test obj2 = new Test(2);

      System.out.println("obj1 has value " + obj1.a);
      System.out.println("obj2 has value " + obj2.a);

      Test.swap(obj1, obj2);

      System.out.println("\nafter swap()\n");
      System.out.println("obj1 has value " + obj1.a);
      System.out.println("obj2 has value " + obj2.a);
   }
}

示例 3

class Test {
   int a;

   Test(int a) {
      this.a = a;
   }
}

class Chapter6_2a {
   public static void swap(Test objectOne, Test objectTwo) {
   int tempVar;

   tempVar = objectOne.a;
   objectOne.a = objectTwo.a;
   objectTwo.a = tempVar;
   }

   public static void main(String[] args) {
      Test obj1 = new Test(1);
      Test obj2 = new Test(2);

      System.out.println("obj1 has value " + obj1.a);
      System.out.println("obj2 has value " + obj2.a);

      swap(obj1, obj2);

      System.out.println("\nafter swap()\n");
      System.out.println("obj1 has value " + obj1.a);
      System.out.println("obj2 has value " + obj2.a);
   }
}

在示例 1 和示例 2 中,swap() 方法被编写为 Test 类的成员,不同之处在于示例 2 中被定义为静态。在示例 3 中,swap() 方法是在 main() 方法中定义的。

我的问题是,从设计、开销和清晰度的角度来看,哪一种是定义 swap() 方法的最佳实践或最专业的方法?

我确实有一些想法,但我真的需要你的意见来确认它们是对还是错:

  1. 在示例 1 和示例 2 之间,我认为最好的方法是从开销的角度将 swap() 方法定义为静态(示例 2),因为类的静态成员不包含在该类的实例。我的假设正确吗?

  2. 从设计和清晰的角度来看,示例 3 不是定义 swap() 方法的好习惯,因为首先 swap() 方法与 Test 类非常相关,应该定义为它的成员和其次,一般来说,最好在密切相关的类中定义 main() 之外的所有方法。这个假设也正确吗?

提前感谢您抽出时间帮助我!!!

【问题讨论】:

    标签: java class methods member


    【解决方案1】:

    由于 Java 编程语言是一种独立于现代平台的“一次编写,到处运行”的面向对象语言,我将使用示例 1。这是因为您真正将 Java 用作设计时的面向对象语言为了。在我看来,创建一个使用对象和对象引用而不是像旧的过程语言或简单的脚本那样在main 中处理所有事情的程序也需要更多的技巧。

    所以在我看来,示例 1 是最优雅和最复杂的。另外,您始终可以扩展该类并覆盖子类中的 swap() 方法以获得不同的功能。但是,使用静态方法无法覆盖。覆盖、封装和多态是面向对象语言强大功能的一部分。使用实例方法可以做到这一点。

    【讨论】:

    • 非常感谢您的分析。你的回答很有帮助。
    • 不客气。感谢您的积极反馈。 ;0 我很高兴看到我的分析很有帮助。编码愉快。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    • 2016-05-21
    • 2017-05-23
    相关资源
    最近更新 更多