当前位置: 代码迷 >> Java相关 >> 怎么回事,明明已经返回了,说没有返回..
  详细解决方案

怎么回事,明明已经返回了,说没有返回..

热度:734   发布时间:2007-02-08 16:20:25.0
怎么回事,明明已经返回了,说没有返回..

import java.util.Random;
class Shape{
void draw(){}
void erase(){}
}

class Circle extends Shape{
void draw(){
System.out.println("Circle.draw()");
}
void erase(){
System.out.println("Circle.erase");
}
}

class Square extends Shape{
void draw(){
System.out.println("Square.draw()");
}
void erase(){
System.out.println("Square.erase");
}
}

class Triangle extends Shape{
void draw(){
System.out.println("Triangle.draw()");
}
void erase(){
System.out.println("Triangle.erase");
}
}

class RandomShapeGenerator{
private Random random = new Random();
public Shape next(){ //error, 此方法必须返回 Shape 类型的结果
switch(random.nextInt(3)){
//default:
case 0:return new Circle();
case 1:return new Square();
case 2:return new Triangle();
}
}
}
public class Duotai{
private static RandomShapeGenerator gen = new RandomShapeGenerator();
public static void main(String[] args){
Shape[] s = new Shape[4];
for( int i = 0; i< s.length; i++ ){
s[i] = gen.next();
for( int j = 0; j< s.length; j++ ){
s[j].draw();
}
}
}
}

搜索更多相关的解决方案: void  import  

----------------解决方案--------------------------------------------------------
[QUOTE] [/QUOTE]
不知道是怎么回事?
----------------解决方案--------------------------------------------------------
class RandomShapeGenerator{
private Random random = new Random();
public Shape next(){
Shape s=null;//改成这样。
switch(random.nextInt(3)){
//default:
case 0:return s=new Circle();
case 1:return s=new Square();
case 2:return s=new Triangle();
}
return s;
}
}

这个类这样写试下~
----------------解决方案--------------------------------------------------------
因为编译器不可能完全智能判断你的switch语句已经包含所有情况了.
所以要么在switch中加个default分支,要么在switch之外加个return null;
----------------解决方案--------------------------------------------------------

必须确定函数有返回值.


----------------解决方案--------------------------------------------------------

class RandomShapeGenerator{
private Random random = new Random();
public Shape next(){ //error, 此方法必须返回 Shape 类型的结果
switch(random.nextInt(3)){
//default:
case 0:return new Circle();
case 1:return new Square();
case 2:return new Triangle();
default : return null;
}
}
}


----------------解决方案--------------------------------------------------------
根据楼上几位的指点,我调试发现都是正确的,可是仍有一件事情不态明白.
解决方案1:加一个defaule分支.
class RandomShapeGenerator{
private Random random = new Random();
public Shape next(){
switch(random.nextInt(3)){
case 0:return new Circle();
case 1:return new Square();
case 2:return new Triangle();
default:return new Shape();//添加了一个分支.
}
}
}
解决方案2: 在switch之外加个Shape s = null;
class RandomShapeGenerator{
private Random random = new Random();
public Shape next(){
Shape s = null;
switch(random.nextInt(3)){
case 0:return s = new Circle();
case 1:return s = new Square();
case 2:return s = new Triangle();
}
return s;
}
}
对于2我能够理解,返回的s能送给public Shape next()函数返回..
但是1返回的应该只能停留在switch()中,怎么好好的却送到了外层public Shape next()函数中了......
----------------解决方案--------------------------------------------------------

return就是返回要返回的值并退出方法

[此贴子已经被作者于2007-2-9 0:24:40编辑过]


----------------解决方案--------------------------------------------------------
谢谢楼上的....
----------------解决方案--------------------------------------------------------
  相关解决方案