美文网首页
集合与方法的合并

集合与方法的合并

作者: 李霖神谷 | 来源:发表于2017-02-12 11:37 被阅读6次
    import java.util.ArrayList;
    
    class People {
        private String name;
        private int age;
    
        public People(String string, int i) {
            // TODO Auto-generated constructor stub
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "People [name=" + name + ", age=" + age + "]";
        }
    
    }
    
    public class JiHeDemo {
        public static void main(String[] args) {
            People p = new People("lisio", 48);
            p.setName("lishuai");
            p.setAge(18);
            ArrayList al = new ArrayList();
            al.add(p);
            al.add(new People("zhangsan", 22));
            System.out.println(al);
        }
    
        @Override
        public String toString() {
            return "JiHeDemo [getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString()
                    + "]";
        }
    }
    

    eclipse中的source帮我写好了set get toString方法,我只需要创建对象,赋值,将对象放到集合中即可。

    相关文章

      网友评论

          本文标题:集合与方法的合并

          本文链接:https://www.haomeiwen.com/subject/ovlaittx.html