java - 在开始编码之前如何确定对象的状态?

标签 java methodology

我有以下代码用于显示 ArrayList 的两个连续元素的总和,直到剩下的元素为 1。例如:-
如果我输入

1 2 3 4 5

输出
3 7 5//最后一个连续的两个相加就是这样
10 5//做同样的事情
15
代码

import java.util.*;
import java.lang.Integer;
class Substan{
     ArrayList <Integer> list = new ArrayList <Integer> ();
     ArrayList <Integer> newList = new ArrayList <Integer> ();// this will be the list containing     the next sequence.
     int index=0;
     int sum=0;
    Substan(){
        Scanner read = new Scanner(System.in);
        String choice;
        System.out.println("Enter the elements of the array");
        do{
            int element = read.nextInt();
            list.add(element);
            System.out.println("More?");
            choice = read.next();
         }while(choice.equals("y") || choice.equals("Y"));
    }
    /* precondition- we have the raw list that user has enterd.
     postcondition - we have displayed all the sublists,by adding two consecutives numbers and the last one is having one element.
     */ 
    void sublist(){         
        while(noofElementsIsNotOneInList()){
            index =0;
            while(newListIsNotComplete()){
                if(nextElementIsThere()){
                    sum = addTheConsecutive();
                }
                else{
                    sum = getLastNumber();
                }
                storeSumInNewList();    
            }
            displayTheNewList();
            System.out.println("");
            updateTheLists();
        }
       displayTheNewList(); //as we have danger of Off By One Bug (OBOB)
        System.out.println("");
    }
    private boolean noofElementsIsNotOneInList(){
        boolean isnotone = true;
        int size = list.size();
        if ( size == 1){
            isnotone = false;
        }
        return isnotone;
    }
    private boolean newListIsNotComplete(){
        boolean isNotComplete = true;
        int listSize = list.size();
        int newListSize = newList.size();
        if (listSizeIsEven()){
            if ( newListSize == listSize/2){
                isNotComplete = false;
            }
        }
        else{
            if( newListSize == (listSize/2) +1){
                isNotComplete = false;
            }
        }
        return isNotComplete;
    }
    private boolean listSizeIsEven(){
        if ( list.size()%2 == 0 ){
            return true;
        }
        else{
            return false;
        }
     }
    /*
    we are at some index.
   returns true if we have an element at (index+1) index.
    */
    private boolean nextElementIsThere(){
        if ( list.size() == index+1 ){
            return false;
        }
        else{
            return true;
        }
     }
    /* precondition-we are at index i
       postcondition - we will be at index i+2 and we return sum of elements at index i and i+1.
*/
    private int addTheConsecutive(){
        int sum = list.get(index)+list.get(index+1);
        index += 2;
        return sum;
     }
    /* we are at last element and we have to return that element.
    */
    private int getLastNumber(){
            return list.get(index);
    }
     private void storeSumInNewList(){
        newList.add(sum);
    }
     private void displayTheNewList(){
        int size = newList.size();
        for ( int i=0;i<size;i++){
            System.out.print(newList.get(i)+" ");
         }
     }
    /*precondition - we have processed all the elements in the list and added the result in newList.
      postcondition - Now my list will be the newList,as we are processing in terms of list and  newList reference will have a new object.
    */
     private void updateTheLists(){
        list = newList;
        newList = new ArrayList <Integer>();// changing the newList
     }
    public static void main(String[] args) {
        Substan s = new Substan();
        s.sublist();
    }
}     

所以我对我的代码做了很多改进,但在与其他方法共享局部变量时遇到了问题。例如,我使用 index 实例来存储索引,最初我认为我不会将其作为实例而是放在方法 sublist() 中的局部变量,但因为无法从需要使用 index 的其他方法(如 >addTheConsecutive()。因此,考虑到我将 index 放在类级别。因此,将共享的变量放在类级别而不是仅查看其状态是正确的方法吗?在编码之前最初的对象并坚持下去并且永远不会改变它?

最佳答案

考虑一下:

对象只能通过共享其属性来与其他对象进行通信。因此,如果您需要一个对象读取另一个对象的状态,唯一的方法就是授予它读取其他对象属性的“权限”。

您有两种方法可以做到这一点:

  1. 声明对象属性public,或
  2. 创建 getXXX() 方法(对于私有(private)属性有意义)

我个人更喜欢选项二,因为 getXXX() 方法返回特定属性的值(“状态”),而没有被修改的风险。当然,如果需要修改私有(private)属性,还应该编写一个setXXX()方法。

示例:

public class MyClass {
    private int foo;
    private String bar;
    /*
     * Code
     */
    public int getFoo() {
        return foo;
    }
    public String getBar() {
        return bar;
    }
    public void setFoo(int foo) {
        this.foo = foo;
    }
    public void setBar(String bar) {
        this.bar = bar;
    }
    /*
     * More code
     */
} 

这样所有的对象属性都被封装了,并且:

  1. 它们不能被任何其他对象读取,除非您专门调用适当的 getXXX() 函数,并且
  2. 不能被其他对象更改,除非您专门调用相应的 setXXX() 函数。

关于java - 在开始编码之前如何确定对象的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24349451/

相关文章:

java - 用Java读取纯文本文件

java - Oracle ADF 或 Java

methodology - 自行开发

c++ - 第三方dll丢失,等待中怎么办?

多线程情况下的Java设计模式: Factory vs Singleton?

java - 带有javascript的Struts2参数标签

java - 使用 Java 创建 Access 数据库文件(.mdb 或 .accdb)

testing - 你如何证明一个函数有效?

java - 在同一个开发团队中使用不同的 IDE

language-agnostic - TDD 对于小型项目来说是不是太过分了?