How to create a println/print method for a custom class

前端 未结 6 1392
猫巷女王i
猫巷女王i 2020-12-01 19:23

I\'m working in Java on a project that requires me to make a few \'container\' classes, if you will. Here is a simple version of one:

public class Pair{

            


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 19:29

    You could extend ArrayList and override the toString() method:

    public class MyArrayList extends ArrayList {
        @Override
        public String toString() {
            // format your print here...
        }
    }
    

    But this is overkill. I would just write a print utility method.

    public class MyUtils {
        public String toString( ArrayList ) {
            // format your print here;
        }
    }
    

提交回复
热议问题