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

前端 未结 6 1372
猫巷女王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'll have to define a toString() method. It's called automatically by System.out.println(Object o). The ArrayList class has such an overridden toString() method that provides a nicely formatted representation.

    Basically, overriding Object.toString with your own definition is all that is required.

提交回复
热议问题