开发者

Compilation error: missing return statement

class Employee
{
int emp_no;
String emp_name;
double emp_sal;

public Employee(int e_no,String e_name,double e_sal)
{
emp_no=e_no;
emp_name=e_name;
emp_sal=e_sal;
}

public double calBonus()
{
return (emp_sal/10);
}

public void display()
{
System.out.println("Employee 开发者_如何转开发No: "+this.emp_no);
System.out.println("Employee Name: "+this.emp_name);
System.out.println("Employee Gross Salary: "+ (emp_sal+calBonus()));
}

}

class Clerk extends Employee
{
String Grade;

public Clerk(int e_no,String e_name,double e_sal,String e_Grade)
{
super(e_no,e_name,e_sal); 
Grade=e_Grade;
this.set_Grade(e_Grade);
}
String set_Grade(String e_Grade)
{
**this.Grade=e_Grade;
}
String get_Grade()
{
return this.Grade;
}**

public void display()
{
super.display();
System.out.println("Grade: " + this.Grade);
}

}

class PaySlip
{
public static void main(String args[])
{
Employee c1;

c1=new Clerk(76,"Saman",12000.50,"Grade1");

c1.display();
}
}

**

Something is wrong in the Bolded lines.But I can not figure it out.When I compile this an error is displayed saying "missing return statement '}'.

**


String set_Grade(String e_Grade)
{
    this.Grade=e_Grade;
}

should be

void set_Grade(String e_Grade)
{
    this.Grade=e_Grade;
}

you have declared return type to String it should be void because you don't have return statement it is a setter.


String set_Grade(String e_Grade)
{
this.Grade=e_Grade;
}

You're declaring that set_Grade returns a String, but there's no return statement. Probably you want the method to "return" void instead:

void set_Grade(String e_Grade)
{
this.Grade=e_Grade;
}

By the way, the usual way to name getters and setters in Java would be without the underscore: setGrade.


Your method signature on set_Grade specifies that a String should be returned but you aren't returning anything.

Also please have a look through both http://www.oracle.com/technetwork/java/codeconvtoc-136057.html and http://www.oracle.com/technetwork/java/codeconvtoc-136057.html for the accepted ways of naming things and formatting code in java.

The more readable code is, the easier it is to fix.


Number of things I would fix.

  1. Change

    String set_Grade(String e_Grade) { this.Grade=e_Grade; }

To

public void set_Grade(String e_Grade)
{
 this.Grade=e_Grade; //also fix the variable name to start with lower case like grade.. followed by convention
}
  1. Apply this in the following constructor to prevent ambiguity.

    public Employee(int e_no,String e_name,double e_sal) {

    this.emp_no=e_no;

    this.emp_name=e_name;

    this.emp_sal=e_sal;

    }


String set_Grade(String e_Grade)
{
**this.Grade=e_Grade;
}
String get_Grade()
{
return this.Grade;
}**

You have a default method that should return a String.

public void set_Grade(String e_Grade)
{
  this.Grade=e_Grade;
}
public String get_Grade()
{
   return this.Grade;
}

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this

   private String grade;

   public void setGrade(String grade) {
     this.grade = grade
   }

   public String getGrade() {
     return this.grade; 
    }

You can also use this to set the generic parameter for method

   public <T> T doStaf(T t) {
      //method body
      return t;
   }

   public void doStaff2() {

      String s =  this.<String>doStaff("STRING");

   }

ps the formating does not work in the code block


The "set_Grade" method is not returning a "String" and therefore the compiler doesn't like it.

This type of method is known as a "setter" or "mutator" ( as opposed to a "getter" or "accessor"). Most setter methods do not return a value and therefore the return type should be "void". Therefore, replacing "String" with "void" will allow your code to compile.

On another note, Java coding conventions lay down best practice rules on how to name your methods. So your method should really be named:

void setGrade(....)

SP

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜