zoukankan      html  css  js  c++  java
  • 对象数组

    对象数组

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

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

    代码如下:

    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());//古力娜扎
        }
    }
    
    
  • 相关阅读:
    leetcode 77 组合
    leetcode 40组合总和 II
    leetcode 216 组合总和III
    弹性伸缩 AS(Auto Scaling)
    弹性计算服务(Elastic Compute Service) / 云服务器 ECS
    云计算概述
    Zabbix Proxy 分布式监控
    Zabbix 自动发现 & 自动注册
    LVS-DR 模式
    GoAccess 监控工具
  • 原文地址:https://www.cnblogs.com/anke-z/p/12356490.html
Copyright © 2011-2022 走看看