在Java中使用抽象类进行方法重载的困惑 [英] Confusion in method overloading using abstract class in java
本文介绍了在Java中使用抽象类进行方法重载的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
abstract class Cell<Q> {
public abstract Q getValue(); // abstract method
}
class Cell1 extends Cell<Integer> {
public Integer getValue() {// abstract method implementation
return 0;
}
}
class Cell2 extends Cell<String> {
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中使用具有相同签名但返回类型不同的方法?
推荐答案
这显然不是方法重载。重载意味着您的方法具有不同的参数返回类型与重载无关。
This is clearly not method overloading . Overloading means your method having different parameters return type has nothing to do with overloading.
public void method(int a,int b);
public void method(String s,int b);
或者您可以说不同数量的参数。
Or you can say different number of arguments.
public void method(int a,int b,int c);
public void method(int a,int b);
您正在做的事情很重要。
what you are doing is overriding.
这篇关于在Java中使用抽象类进行方法重载的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文