这是一个示例程序
class abc {
void show() {
System.out.println("First Class");
}
}
class def {
void show() {
System.out.println("Second Class");
}
}
class demo {
public static void main(String args[]) {
abc a1 = new abc();
def a2 = new def();
a1.show();
a2.show();
}
}
现在我想问的是,有两个不同的类,但是它们具有相同的方法名称和相同的参数,在JAVA中这个概念叫什么?
在JAVA中,这个概念叫什么?
这不是一个概念,您已经在两个不相关的类中将方法命名为相同。如果一个类是另一个的子类,则根据方法签名的不同,该类将被重写或重载。
如果一个类是另一个的子类,则您将实现运行时多态性:
class abc {
void show() {
System.out.println("First Class");
}
}
// def extends from abc
class def extends abc {
// show() method was overridden here
void show() {
System.out.println("Second Class");
}
}
class demo {
public static void main(String args[]) {
// Use the reference type abc, object of type abc
abc a1 = new abc();
// Use the reference type abc, object of type def
abc a2 = new def();
// invokes the show() method of abc as the object is of abc
a1.show();
// invokes the show() method of def as the object is of def
a2.show();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句