使用java中的抽象类重载方法重载

abstract class Cell { public abstract Q getValue(); // abstract method } class Cell1 extends Cell { public Integer getValue() {// abstract method implementation return 0; } } class Cell2 extends Cell { public String getValue() { // abstract method implementation return "razeel"; } } class test { public static void main(String[] a) { Cell obj=new Cell1(); int ab=(Integer) obj.getValue(); Cell objs=new Cell2(); String str=objs.getValue().toString(); System.out.println("ab=================== "+ab); System.out.println("String=================== "+str); } } 
  • 我们可以将此称为java中方法重载的示例。 如果不是为什么?
  • 是否可以在java中使用具有相同签名但返回类型不同的方法?

这显然不是方法重载。 重载意味着您​​的方法具有不同的参数返回类型与重载无关。

 public void method(int a,int b); public void method(String s,int b); 

或者你可以说不同数量的论点。

 public void method(int a,int b,int c); public void method(int a,int b); 

你正在做的是压倒一切。

上面显示的代码示例是方法覆盖的示例。 这是java实现运行时多态的方式。 在java中,如果使用超类引用调用overriden方法,则java根据调用时引用的对象类型确定要执行的方法版本,而不是取决于变量的类型。 考虑

  class Figure{ double dim1; double dim2; Figure(double dim1,double dim2){ this.dim1=dim1; this.dim2=dim2; } double area(){ return 0; } } class Rectangle extends Figure{ Rectangle(double dim1,double dim2){ super(dim1,dim2); } double area(){ double a; a=dim1*dim2; System.out.println("dimensions of rectangle are "+dim1+" "+dim2); return a; } } class Triangle extends Figure{ Triangle(double dim1,double dim2){ super(dim1,dim2); } double area(){ double a=(dim1*dim2)/2; System.out.println("base & altitude of triangle are "+dim1+" "+dim2); return a; } } class test{ public static void main(String[] args){ Figure r; Rectangle b=new Rectangle(10,10); Triangle c=new Triangle(10,10); r=b; System.out.println("area of rectangle fig "+r.area()); r=c; System.out.println("area of triangle fig "+r.area()); } 

}

输出:矩形尺寸为10.0 10.0矩形区域图100.0基础和三角形高度为10.0 10.0面积三角形图50.0

对于第二个qstn:没有。 签名意味着独特。 返回类型不是签名的一部分

我们可以将此称为java中方法重载的示例。

没有。

如果不是为什么?

重载意味着“相同的名称,不同的参数”,但你已经有子类实现方法,仅此而已。

是否可以在java中使用具有相同签名但返回类型不同的方法?

没有。