Java中的构造函数和方法

时间:2019-07-15 08:32:06

标签: java

我想创建下面的类

associatename:String
workstatus:String 
associate() :constructor
getassociatename():String
setassociatename(String):void
getworkstatus()String
tracksassociatestatus():int
setworkstatus(String):void

trackAssociateStatus方法将天数作为参数,并根据天数设置关联者的工作状态。他们前20天学习“ C”,接下来20天学习“ Java”。在Main类中,调用trackAssociateStatus方法并找到工作状态并显示输出。

输出:相关abc工作状态:项目阶段

我尝试了这个。...但是我出错了

//associate class
public class associate{
    private int associatename;
    private String workstatus;
    private int days;
    void associate()
    {
         getassociatename();
         setassociatename();
         getworkstatus();
         tracksassociatestatus();
         setworkstatus();
    }
        public int getassociatename()
        {
            return associatename;
        }
        public void setassociatename(int associatename)
        {
            this.associatename=associatename;
        }
        public String getworkstatus()
        {
            return workstatus;
        }
        public void tracksassociatestatus(int days)
        {
            if(days<20)
                setworkstatus("C");
            else
                setworkstatus("Java");
        }
        public void setworkstatus(String workstatus)
        {
            this.workstatus=workstatus;
        }

}

//main class
 associate a =new associate();
        Scanner in=new Scanner(System.in);
        int associateid=0;
        String workstatus=null;
        int days=0;
        System.out.println("Enter the associateid:");
        associateid=in.nextInt();
        a.associateid=(associateid);
        System.out.println("Enter the no of days:");
        days=in.nextInt();
        a.trackassociatestatus();
        System.out.println("The id is "+a.getassocaiteid()+" work status "+a.getworkstatus());

3 个答案:

答案 0 :(得分:2)

根据您的(看似)UML规范,您的类如下所示:

Swift

public class Associate { private String associateName; private String workStatus; public Associate() { // This constructor is optional, a no-args constructor is added by the compiler to any class not explicitly naming a constructor. } public String getAssociateName() { return associateName; } public void setAssociateName(String associateName) { this.associateName = associateName; } public String getWorkStatus() { return workStatus; } public void setWorkStatus(String workStatus) { this.workStatus = workStatus; } public int tracksAssociateStatus() { // TODO write logic here return 1; // TODO change to whatever you need to return } } int时,您为getAssociateName指定了associateName。这行不通;您需要使getter返回类型与字段数据类型相同,或者需要将数据转换为方法的返回类型。 (前者是最佳做法)。

构造函数不指定类型,使用类名,编译器将理解您要执行的操作(这将返回该类的新实例)。因此,您的String将告诉编译器“创建一个名为associate的方法,该方法不返回任何内容”。

答案 1 :(得分:0)

好吧,如果您自己为我们提供错误信息,那就很好了。

但是,与此同时,您是否注意到 tracksassociatestatus 方法接收的整数参数 days ,而您的构造方法没有向其传递任何信息?

因此,请尝试将您的构造函数更改为:

Public associate() {
     getassociatename();
     setassociatename();
     getworkstatus();
     tracksassociatestatus(10);
     setworkstatus();
}

有关更简洁的代码,请检查其他答案。

如果您仍然有错误,请分享。

答案 2 :(得分:0)

import java.util.*;

public class Associate
{
    private String associateName;
    private int workStatus;
    private int days;
    Scanner sc = new Scanner(System.in);

    public String getAssociateName()
    {
        System.out.println("Enter the Associate id:");
        associateName = sc.nextLine();
        return associateName;
    }

    public void setassociatename(int associatename)
    {
        this.associateName=associateName;
    }
    
    public String tracksAssociatename() 
    {
        return associateName;
    }

    public int getWorkStatus()
    {
        System.out.println("Enter the number of days");
        days = sc.nextInt();
        return days;
    }

    public void setWorkStatus(String workStatus)
    {
        this.workStatus=workStatus;
    }
    enter code here
    public `enter code here`int tracksAssociateStatus() 
    {
        return days;
    }

    public static void main(String args[])
    {
        Associate obj = new Associate();
        obj.getAssociateName();
        obj.getworkstatus();
        System.out.println("The Associate name "+obj.tracksAssociatename()+" work Status "+obj.tracksAssociateStatus());
    }
}