• 对象数组


    对象数组

    任何数据类型都可以作为数组中的元素类型

    数组有一个缺点:一旦创建程序运行期间,长度不可改变

    代码如下:

    public class Person {
        private String name;
        private int age;
    
        public Person(){
    
        }
        public Person(String name,int age){
            this.name = name;
            this.age = age;
        }
        public void setName(String name){
            this.name = name;
        }
        public String getName(){
            return name;
        }
    
       public void setAge(int age){
            this.age = age;
       }
       public int getAge(){
            return age;
       }
    }
    

    测试类:

    public class Demo01Array {
    
        public static void main(String[] args) {
            //首先创建一个长度为3的数组,里面用来存放Person类型的对象
            Person[] array =new  Person[3];
    
            Person one = new Person("迪丽热巴",3);
            Person two = new Person("古力娜扎",6);
            Person three = new Person("马尔扎哈",9);
    
            //将one当中的地址值赋值到数组的0号元素位置
            array[0] = one;
            array[1] = two;
            array[2] = three;
    
            System.out.println(one);//地址值
            System.out.println(two);//地址值
            System.out.println(three);//地址值
    
    
            System.out.println(array[1].getName());//古力娜扎
        }
    }
    
    
  • 相关阅读:
    day15 web框架和Django基础
    activiti5.22 获得mybatis sessionFactory
    activiti 视图
    activiti 任意退
    spring cloud &&spring boot
    JPA 一对多关联
    webstorm 快捷键
    web storm 破解
    Excel通用导出
    activiti 小计
  • 原文地址:https://www.cnblogs.com/anke-z/p/12356490.html
Copyright © 2020-2023  润新知