Java逻辑问题和最佳做法问题

时间:2019-05-30 15:47:58

标签: java programming-languages

我正在尝试学习Java的最佳实践。问题是我没有任何老师/老师就独自学习所有东西,因此很难理解什么是对的。

考虑到下面两种可能性,对小型项目进行编程的正确方法是什么?

案例1:创建一个对象以及指向该对象的所有变量/方法。

情况2:使用静态方法和变量,从而将所有计算都参考主类本身进行。

我了解,当您需要多个对象时,应将方法/变量应用于这些实例/将指向特定对象,但是在具有一个类/对象实例的小型项目中,最佳实践是什么?

情况1:

import java.util.Scanner;

public class BabyCalculator
{
    int firstNumber, secondNumber, result; // instance variables, belonging to the object BabyCalculator
    char operator;
    Scanner in;

    BabyCalculator() { // Constructed a BabyCalculator object, set up the logic of the program inside of it
        Scanner in = new Scanner(System.in);

         do {        
        System.out.print("First Number: ");
            firstNumber = in.nextInt();
        System.out.print("Operator: ");
            operator = in.next().charAt(0);
        System.out.print("Second Number: ");        
            secondNumber = in.nextInt();    

        calculate (firstNumber, secondNumber);

            if (firstNumber ==0)  System.out.println("exiting..");

            else  System.out.println("The result is:" + result);

      }

       while (firstNumber!=0);

    }

    public void calculate (int x, int y) { // Issued a METHOD belonging to the object BabyCalculator, not the class

            if (operator == ('+')) {
                result = x+y;
            }
            if (operator == ('*')) {
                result = x*y;
            }    
            if (operator == ('-')) {
                result = x-y;
            }        
            if (operator == ('/')) {
                result = x/y;
            }               

   }

   public static void main (String [] args) {
       BabyCalculator baby = new BabyCalculator(); // created a new object called baby, that is an instance of the BabyCalculator object

       }
    }

案例2

 import java.util.Scanner;

public class BabyCalculator2
{
     static int firstNumber, secondNumber, result; // static variables, belonging to the class, not the object
     static char operator;

    public static void calculate (int x, int y) { // static method, belonging to the class

            if (operator == ('+')) {
                result = x+y;
            }
            if (operator == ('*')) {
                result = x*y;
            }    
            if (operator == ('-')) {
                result = x-y;
            }        
            if (operator == ('/')) {
                result = x/y;
            }               

   }

   public static void main (String [] args) {
       Scanner in = new Scanner(System.in);

       do {        
        System.out.print("First Number: ");
            firstNumber = in.nextInt();
        System.out.print("Operator: ");
            operator = in.next().charAt(0);
        System.out.print("Second Number: ");        
            secondNumber = in.nextInt();    

        calculate (firstNumber, secondNumber);

            if (firstNumber ==0)  System.out.println("exiting..");

            else  System.out.println("The result is:" + result);

      }

       while (firstNumber!=0);

       }
}

0 个答案:

没有答案