risc-v中文社区

 找回密码
 立即注册
查看: 939|回复: 0

[原创] java for risc-v系列知识讲座(6)---对象数组及其迭代

[复制链接]

20

主题

23

帖子

96

积分

注册会员

Rank: 2

积分
96
发表于 2021-8-3 21:04:12 | 显示全部楼层 |阅读模式

public class ObjectArrayTest {
    public static void main(String[] args)
    {
        PersonCls p[] = {new PersonCls("张三",15),new PersonCls("李四",30),new PersonCls("王五",35)} ;
        for(int i=0;i<p.length;i++)
        {
            System.out.println(p[i].talk()) ;
        }
        //第二种迭代方法
        for (PersonCls pinst:p) {
            System.out.println(pinst.talk());
        }
    }
}
class PersonCls
{
    String name ;
    int age ;
    public PersonCls()
    {
    }
    public PersonCls(String name,int age)
    {
        this.name = name ;
        this.age = age ;
    }
    public String talk()
    {
        return  this.name + ",今年:"+this.age+"岁" ;
    }
}
显示:
张三,今年:15岁
李四,今年:30岁
王五,今年:35岁
张三,今年:15岁
李四,今年:30岁
王五,今年:35岁

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



Archiver|手机版|小黑屋|risc-v中文社区

GMT+8, 2024-4-27 21:03 , Processed in 0.013577 second(s), 17 queries .

risc-v中文社区论坛 官方网站

Copyright © 2018-2021, risc-v open source

快速回复 返回顶部 返回列表