Java返回值需要注意的地方

1. 在方法重写时不能变更声明的返回类型,但可以为声明的返回类型的子类


    
    
  1. public class Foo{
  2. void go() { }
  3. }
  4. class Bar extends Foo {
  5. String go() { // 不合法
  6. return ;
  7. }
  8. }

该段代码会报如下错误


    
    
  1. Multiple markers at this line
  2. - overrides Foo.go
  3. - The return type is incompatible with
  4. Foo.go()

但是如果在继承的同时还进行了重载,则声明的返回类型可以改变,如下代码能正常运行:


    
    
  1. public class Foo{
  2. void go() { }
  3. }
  4. class Bar extends Foo {
  5. String go(int x) {
  6. return ;
  7. }
  8. }

重写的方法声明的返回类型可以为父类方法的子类:


    
    
  1. public class Foo{
  2. Foo go(){
  3. return new Foo();
  4. }
  5. }
  6. class Bar extends Foo {
  7. Bar go() {
  8. return new Bar();
  9. }
  10. }


2. 如果声明的返回类型为一个对象,则return返回的类型可以为null:


    
    
  1. public class Foo{
  2. Foo go() {
  3. return ;
  4. }
  5. }

3. return的类型可以为声明的返回类型的子类:


    
    
  1. class Bar extends Foo {
  2. Foo go() {
  3. return new Bar();
  4. }
  5. }

4. 当声明的返回类型为抽象类或者接口时,return的类型可以为继承该抽象类或者实现该接口的类:


    
    
  1. abstract class Foo{
  2. abstract Foo go();
  3. }
  4. class Bar extends Foo {
  5. Foo go() {
  6. return new Bar();
  7. }
  8. }


    
    
  1. interface runnable{
  2. runnable go();
  3. }
  4. class Bar implements runnable {
  5. public runnable go() {
  6. return new Bar();
  7. }
  8. }

5. 当声明的发挥类型为void时,可以return但是不能return任何值,也不能return null,


    
    
  1. abstract class Foo{
  2. abstract void go();
  3. }
  4. class Bar extends Foo {
  5. public void go() {
  6. return;
  7. }
  8. }



            </div>

1. 在方法重写时不能变更声明的返回类型,但可以为声明的返回类型的子类


  
  
  1. public class Foo{
  2. void go() { }
  3. }
  4. class Bar extends Foo {
  5. String go() { // 不合法
  6. return ;
  7. }
  8. }

该段代码会报如下错误


  
  
  1. Multiple markers at this line
  2. - overrides Foo.go
  3. - The return type is incompatible with
  4. Foo.go()

但是如果在继承的同时还进行了重载,则声明的返回类型可以改变,如下代码能正常运行:


  
  
  1. public class Foo{
  2. void go() { }
  3. }
  4. class Bar extends Foo {
  5. String go(int x) {
  6. return ;
  7. }
  8. }

重写的方法声明的返回类型可以为父类方法的子类:


  
  
  1. public class Foo{
  2. Foo go(){
  3. return new Foo();
  4. }
  5. }
  6. class Bar extends Foo {
  7. Bar go() {
  8. return new Bar();
  9. }
  10. }


2. 如果声明的返回类型为一个对象,则return返回的类型可以为null:


  
  
  1. public class Foo{
  2. Foo go() {
  3. return ;
  4. }
  5. }

3. return的类型可以为声明的返回类型的子类:


  
  
  1. class Bar extends Foo {
  2. Foo go() {
  3. return new Bar();
  4. }
  5. }

4. 当声明的返回类型为抽象类或者接口时,return的类型可以为继承该抽象类或者实现该接口的类:


  
  
  1. abstract class Foo{
  2. abstract Foo go();
  3. }
  4. class Bar extends Foo {
  5. Foo go() {
  6. return new Bar();
  7. }
  8. }


  
  
  1. interface runnable{
  2. runnable go();
  3. }
  4. class Bar implements runnable {
  5. public runnable go() {
  6. return new Bar();
  7. }
  8. }

5. 当声明的发挥类型为void时,可以return但是不能return任何值,也不能return null,


  
  
  1. abstract class Foo{
  2. abstract void go();
  3. }
  4. class Bar extends Foo {
  5. public void go() {
  6. return;
  7. }
  8. }



            </div>

猜你喜欢

转载自blog.csdn.net/wz568780720/article/details/80903269