你能用Java编写虚函数/方法吗?
2022-08-31 06:30:50
是否有可能用Java编写虚拟方法,就像在C++中所做的那样?
或者,有没有一种适当的Java方法可以实现,产生类似的行为?我能举几个例子吗?
是否有可能用Java编写虚拟方法,就像在C++中所做的那样?
或者,有没有一种适当的Java方法可以实现,产生类似的行为?我能举几个例子吗?
是的。实际上,默认情况下,Java 中的所有实例方法都是虚拟的。只有某些方法不是虚拟的:
以下是一些示例:
“普通”虚拟功能
以下示例来自另一个答案中提到的维基百科页面的旧版本。
import java.util.*;
public class Animal
{
public void eat()
{
System.out.println("I eat like a generic Animal.");
}
public static void main(String[] args)
{
List<Animal> animals = new LinkedList<Animal>();
animals.add(new Animal());
animals.add(new Fish());
animals.add(new Goldfish());
animals.add(new OtherAnimal());
for (Animal currentAnimal : animals)
{
currentAnimal.eat();
}
}
}
class Fish extends Animal
{
@Override
public void eat()
{
System.out.println("I eat like a fish!");
}
}
class Goldfish extends Fish
{
@Override
public void eat()
{
System.out.println("I eat like a goldfish!");
}
}
class OtherAnimal extends Animal {}
输出:
I eat like a generic Animal. I eat like a fish! I eat like a goldfish! I eat like a generic Animal.
带接口的虚函数示例
Java 接口方法都是虚拟的。它们必须是虚拟的,因为它们依赖于实现类来提供方法实现。要执行的代码将仅在运行时选择。
例如:
interface Bicycle { //the function applyBrakes() is virtual because
void applyBrakes(); //functions in interfaces are designed to be
} //overridden.
class ACMEBicycle implements Bicycle {
public void applyBrakes(){ //Here we implement applyBrakes()
System.out.println("Brakes applied"); //function
}
}
具有抽象类的虚函数的示例。
与接口类似,抽象类必须包含虚拟方法,因为它们依赖于扩展类的实现。例如:
abstract class Dog {
final void bark() { //bark() is not virtual because it is
System.out.println("woof"); //final and if you tried to override it
} //you would get a compile time error.
abstract void jump(); //jump() is a "pure" virtual function
}
class MyDog extends Dog{
void jump(){
System.out.println("boing"); //here jump() is being overridden
}
}
public class Runner {
public static void main(String[] args) {
Dog dog = new MyDog(); // Create a MyDog and assign to plain Dog variable
dog.jump(); // calling the virtual function.
// MyDog.jump() will be executed
// although the variable is just a plain Dog.
}
}