简体   繁体   English

Java:定义与类相关的方法的最佳实践

[英]Java: Best practice to define a method in relation to a class

I was given the following exercise: 我进行了以下练习:

"Given this class, “鉴于这堂课,

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

write a method called swap() that exchanges the contents of the objects referred by Two Test object references." 编写一个称为swap()的方法,该方法交换两个测试对象引用所引用的对象的内容。”

I have written three slightly different examples of the exercise: 我写了三个稍有不同的练习示例:

Example 1 例子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);
   }
}

Example 2 例子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);
   }
}

Example 3 例子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);
   }
}

In the examples 1 and 2 the swap() method was written as a member of the Test class, with the difference that in the example 2 is defined as static. 在示例1和2中,swap()方法被编写为Test类的成员,不同之处在于示例2中的定义为静态。 In the example 3 the swap() method is defined inside the main() method. 在示例3中,swap()方法在main()方法内部定义。

My question is , which one is the best practice or most professional way to define the swap() method, from design, overhead and clarity point of view? 我的问题是 ,从设计,开销和清晰度的角度来看,哪种方法是定义swap()方法的最佳实践或最专业的方法?

I do have some thoughts but I really need your opinion to confirm them as right or wrong: 我确实有一些想法,但是我真的需要您的意见以确认它们是对还是错:

  1. Between the Examples 1 and 2 I believe that the best way is to define the swap() method as static (Example 2) from overhead point of view, because the static members of a class are not included in the instances of that class. 从示例的角度来看,在示例1和2之间,我认为最好的方法是将swap()方法定义为静态方法(示例2),因为该类的实例中不包括该类的静态成员。 Is my assumption correct? 我的假设正确吗?

  2. The Example 3 is not a good practice to define the swap() method from design and clarity point of view, because first the swap() method is very related to the Test class and should defined as its member and second, in general, is better to define all methods outside main() in close related classes. 从设计和清晰度的角度来看,示例3并不是定义swap()方法的好习惯,因为首先,swap()方法与Test类非常相关,应将其定义为其成员,然后通常将其定义为最好在紧密相关的类中定义main()之外的所有方法。 Is this assumption also correct? 这个假设也正确吗?

I thank you in advance for your time to help me!!! 我预先感谢您抽出宝贵的时间来帮助我!!!

Since the Java programming language is a modern platform independent, "write once, run everywhere", object-oriented language I would use Example 1. This is because you are truly using Java as an object-oriented language as it was designed for. 由于Java编程语言是一种独立于现代平台的,面向对象的语言,即“编写一次,随处运行”,因此我将使用示例1。这是因为您确实将Java用作了其专门设计的面向对象的语言。 It takes a bit more skill also in my opinion to create a program that uses objects and object references instead of handling things all in main like an older procedural language or a simple script. 它需要更多的技巧也是在我看来,以创建使用对象和对象的引用来处理所有的事情在程序main像一个旧的程序语言或一个简单的脚本。

So in my opinion Example 1 is the most elegant and sophisticated. 因此,我认为示例1是最优雅,最精致的。 Plus, you could always extend the class and override the swap() method in a sub-class for different functionality. 另外,您始终可以扩展该类并在子类中重写swap()方法以实现不同的功能。 Overriding is not possible with static methods though. 但是,使用静态方法无法覆盖。 Overriding, encapsulation, and polymorphism is part of the power of object-oriented languages. 覆盖,封装和多态性是面向对象语言的强大功能的一部分。 Using an instance method allows some of this. 使用实例方法可以做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM