zoukankan      html  css  js  c++  java
  • 更改字段的值

    public class ClassInit {
        private Person person=new Person("test", "test1");
        private Person[] flock =new Person[]{ person };
    
        public ClassInit() {
            for (int i = 0; i < flock.length; i++) {
                flock[i] = new Person(i+"name",i+"Id");
            }
        }
    
        public void print() {
            System.out.println(person);
        }
    
        public void printArray() {
            for (Person person : flock) {
                System.out.println(person);
            }
        }
    
        public static void main(String[] args) {
            ClassInit self = new ClassInit();
            self.print();
            self.printArray();
        }
    
    }
    
    class Person {
        private String name;
        private String id;
    
        public Person(String name, String id) {
            super();
            this.name = name;
            this.id = id;
        }
    
        @Override
        public String toString() {
            return this.name + ":" + this.id;
        }
    
    }

    Output:

    test:test1
    0name:0Id

    现象:
    person的值没有改变


    变形1:结果和上例相同

    public class ClassInit {
        
        public static void main(String[] args) {
            Flock flock = new Flock();
            flock.print();
            flock.printArray();
        }
    }
    
    
    
    class Flock {
        private Person person=new Person("test", "test1");
        private Person[] flock =new Person[]{ person };
    
        public Flock() {
            for (int i = 0; i < flock.length; i++) {
                flock[i] = new Person(i+"name",i+"Id");
            }
        }
    
        public void print() {
            System.out.println(person);
        }
    
        public void printArray() {
            for (Person person : flock) {
                System.out.println(person);
            }
        }
    
    
    
    }
    
    class Person {
        private String name;
        private String id;
    
        public Person(String name, String id) {
            super();
            this.name = name;
            this.id = id;
        }
    
        @Override
        public String toString() {
            return this.name + ":" + this.id;
        }
    
    }

    变形2:结果和上例相同

    import java.util.ArrayList;
    import java.util.List;
    
    public class ClassInit {
        
        public static void main(String[] args) {
            Flock flock = new Flock();
            flock.print();
            flock.printArray();
        }
    }
    
    
    
    class Flock {
        private Person person=new Person("test", "test1");
        private List<Person> flock =new ArrayList<Person>();
    
        public Flock() {
            flock.add(person);
            for (int i = 0; i < flock.size(); i++) {
                flock.set(i, new Person(i+"name",i+"Id"));
            }
        }
    
        public void print() {
            System.out.println(person);
        }
    
        public void printArray() {
            for (Person person : flock) {
                System.out.println(person);
            }
        }
    
    
    
    }
    
    class Person {
        private String name;
        private String id;
    
        public Person(String name, String id) {
            super();
            this.name = name;
            this.id = id;
        }
    
        @Override
        public String toString() {
            return this.name + ":" + this.id;
        }
    
    }
  • 相关阅读:
    法语助手2010破解
    一个很简单的例子,从汇编层次理解函数调用
    ubuntu11.10配置IPV6
    linux创建 / 删除用户及用户管理
    设置gdb反汇编语法为intel
    ubuntu server 10.04 LTS(64位)装不了花生壳的解决方法
    实现windows和linux互传文件
    在ubuntu11.10中安装chrome浏览器
    poj 1755 Triathlon 半平面交判断不等式是否有解
    poj 1474 Video Surveillance 半平面交
  • 原文地址:https://www.cnblogs.com/softidea/p/4573628.html
Copyright © 2011-2022 走看看