求一个java代码。

2025-01-19 23:13:44
推荐回答(1个)
回答1:

public class Family
{
    public ArrayList  alMembers=new ArrayList();
    
    public void sports()
    {
        for(Member m:alMembers)
        {
            m.sports();
        }
    }
    
    public void addMember(Member m)
    {
        if(m!=null)
        {
            alMembers.add(m);
        }
    }
    
    public static void main(String[] args)
    {
       Family f=new Family();
       f.addMember(new Member("Father","羽毛球"));
       f.addMember(new Member("Mother","健美操"));
       f.addMember(new Member("Child","跳绳"));
       
       f.sports();
    }
}

class Member
{
    public String Name="";
    public String sportType="";
    public Member(String name, String sporttype)
    {
        Name=name;
        sportType=sporttype;
    }
    
    public void sports()
    {
       System.out.println(Name+" is playing "+sportType+".");
    }
}