【问题标题】:Java Activity: InheritanceJava 活动:继承
【发布时间】:2021-05-31 17:17:55
【问题描述】:

我有一个稍后会通过的活动。我需要帮助不是通过此活动,而是要了解更多信息并拥有用于以后编程活动的方法。

所以,这个活动是关于我理解的关于继承的练习,但我的问题不止于此。

import java.util.*;
public class sdisp extends sInfo
{
    private String cor;
    private int year;
    private String so;
    
    public String getCor()
    {
        return cor;
    }
    public void setCor(String cor) 
    {
        this.cor = cor;
    }
    public int getYear()
    {
        return year;
    }
    public void setYear(int year)
    {
        this.year = year;
    }
    public String getSo()
    {
        return so;
    }
    public void setSo(String so) 
    {
        this.so = so;
    }
    
    public static void main(String[] args)
    {
        sdisp obj=new sdisp();
        Scanner input=new Scanner(System.in);
        sInfo n1=new sInfo();
        sInfo s1=new sInfo();
        sInfo g1=new sInfo();
        sInfo n2=new sInfo();
        sInfo s2=new sInfo();
        sInfo g2=new sInfo();
        sInfo n3=new sInfo();
        sInfo s3=new sInfo();
        sInfo g3=new sInfo();
        sInfo n4=new sInfo();
        sInfo s4=new sInfo();
        sInfo g4=new sInfo();
        sInfo n5=new sInfo();
        sInfo s5=new sInfo();
        sInfo g5=new sInfo();
        sdisp c1=new sdisp();
        sdisp y1=new sdisp();
        sdisp so1=new sdisp();
        sdisp c2=new sdisp();
        sdisp y2=new sdisp();
        sdisp so2=new sdisp();
        sdisp c3=new sdisp();
        sdisp y3=new sdisp();
        sdisp so3=new sdisp();
        sdisp c4=new sdisp();
        sdisp y4=new sdisp();
        sdisp so4=new sdisp();
        sdisp c5=new sdisp();
        sdisp y5=new sdisp();
        sdisp so5=new sdisp();
        
        System.out.print("Student's Name: ");
        n1.setName(input.next());
        System.out.print("Student Number: ");
        s1.setSnum(input.nextInt());
        System.out.print("Gender: ");
        g1.setGen(input.next());
        System.out.print("Course: ");
        c1.setCor(input.next());
        System.out.print("Year: ");
        y1.setYear(input.nextInt());
        System.out.print("Student Organization: ");
        so1.setSo(input.next());
        
        System.out.println("");
        
        System.out.print("Student's Name: ");
        n2.setName(input.next());
        System.out.print("Student Number: ");
        s2.setSnum(input.nextInt());
        System.out.print("Gender: ");
        g2.setGen(input.next());
        System.out.print("Course: ");
        c2.setCor(input.next());
        System.out.print("Year: ");
        y2.setYear(input.nextInt());
        System.out.print("Student Organization: ");
        so2.setSo(input.next());
        
        System.out.println("");
        
        System.out.print("Student's Name: ");
        n3.setName(input.next());
        System.out.print("Student Number: ");
        s3.setSnum(input.nextInt());
        System.out.print("Gender: ");
        g3.setGen(input.next());
        System.out.print("Course: ");
        c3.setCor(input.next());
        System.out.print("Year: ");
        y3.setYear(input.nextInt());
        System.out.print("Student Organization: ");
        so3.setSo(input.next());
        
        System.out.println("");
        
        System.out.print("Student's Name: ");
        n4.setName(input.next());
        System.out.print("Student Number: ");
        s4.setSnum(input.nextInt());
        System.out.print("Gender: ");
        g4.setGen(input.next());
        System.out.print("Course: ");
        c4.setCor(input.next());
        System.out.print("Year: ");
        y4.setYear(input.nextInt());
        System.out.print("Student Organization: ");
        so4.setSo(input.next());
        
        System.out.println("");
        
        System.out.print("Student's Name: ");
        n5.setName(input.next());
        System.out.print("Student Number: ");
        s5.setSnum(input.nextInt());
        System.out.print("Gender: ");
        g5.setGen(input.next());
        System.out.print("Course: ");
        c5.setCor(input.next());
        System.out.print("Year: ");
        y5.setYear(input.nextInt());
        System.out.print("Student Organization: ");
        so5.setSo(input.next());
        
        System.out.println("");
        
        String [] gender = new String []
                {
                        g1.getGen(), g2.getGen(), g3.getGen(), g4.getGen(), g5.getGen(),
                };
        int mcount = 0;
        int fcount = 0;
        for (int i = 0; i < gender.length; i++)
        {
                if (gender[i].contains("Male"))
                {
                    mcount++;
                }
                if (gender[i].contains("Female"))
                {
                    fcount++;
                }
        }
        System.out.println("Male Count: " + mcount);
        System.out.println("Female Count: " + fcount);
        
        if (mcount > fcount)
        {
            System.out.println("The majority is Male");
        }
        if (mcount < fcount)
        {
            System.out.println("The majority is Female");
        }
    }
}

我的代码中的问题是:

  • 有多个变量。我不知道使用什么循环来创建至少 5 名学生的信息。
package gsetter;
public class sInfo 
{
    private String name;
    private int snum;
    private String gen;
    

    public String getName()
    {
        return name;
    }
    public void setName(String name) 
    {
        this.name = name;
    }
    public int getSnum()
    {
        return snum;
    }
    public void setSnum(int snum)
    {
        this.snum = snum;
    }
    public String getGen()
    {
        return gen;
    }
    public void setGen(String gen) 
    {
        this.gen = gen;
    }
    

}

对于父类。

编辑:我发现了如何计算男性和女性并找到多数。

【问题讨论】:

  • 为什么你使用getters-setters 而不是constructor 来初始化对象的值?另外,为什么courseyearso 属性定义在一个单独的类中,而不是与sInfo 在同一个类中?
  • @salazarin。我们还没有/尚未了解constructor,我们本周的课程是继承和我们的教授。使用了 getters-setter,所以她希望我们使用 getters-setter。 courseyearso 应该在主显示方法中,我们的教授。指示。

标签: java inheritance methods


【解决方案1】:

从根本上说,您需要使用循环并请求保留对象 sInfo 的信息。

循环你可以使用一个简单的while循环和循环五次。以下是您如何进行的示例

List<sinfo> list = new ArrayList<sinfo>()
Scanner input=new Scanner(System.in);
int i = 0;
while( i < 5)
{
    sInfo n1=new sInfo();
      System.out.print("Student's Name: ");
        n1.setName(input.next());
        System.out.print("Student Number: ");
        n1.setSnum(input.nextInt());
        System.out.print("Gender: ");
        n1.setGen(input.next());
        System.out.print("Course: ");
        n1.setCor(input.next());
        System.out.print("Year: ");
        n1.setYear(input.nextInt());
        System.out.print("Student Organization: ");
        n1.setSo(input.next());
       list.add(n1);
       i++;
}

稍后您可以使用列表检索 5 个对象

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 2012-10-30
    • 2017-12-27
    • 2012-04-12
    • 2010-10-28
    • 1970-01-01
    • 2011-03-31
    相关资源
    最近更新 更多