迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

Java 中的虚拟函数

作者:迹忆客 最近更新:2023/09/17 浏览次数:

本文介绍了什么是 Java 中的虚拟函数/方法以及如何使用 Java 中的虚拟函数。

在基类中定义并且可以在派生类中覆盖的函数称为虚拟函数。C++ 和 Java 中默认使用了虚拟函数的概念;所有非私有和非最终方法都是虚方法。

在 C++ 中,我们使用 virtual 关键字来创建虚拟函数,但 Java 没有这样的关键字。除此之外,所有非私有和非最终方法都是虚拟的。

虚拟函数的概念在面向对象编程概念和多态性方面很有用。让我们通过一些例子来理解。


Java 中的虚拟函数

在这个例子中,我们创建了一个类 Human,其中包含一个虚拟的 eat() 方法。因为它是一个虚方法,它可以在派生类/子类中被覆盖,就像我们在下面的例子中所做的那样。两个方法具有相同的签名,当我们调用函数时,只执行子类方法。请参阅下面的示例。

class Human{
	void eat(String choice) {
		System.out.println("I would like to eat - "+choice+ " now");
	}
}

public class SimpleTesting extends Human{
	void eat(String choice) {
		System.out.println("I would like to eat - "+choice);
	}
	public static void main(String[] args){
		SimpleTesting simpleTesting = new SimpleTesting();
		simpleTesting.eat("Pizza");
		simpleTesting.eat("Chicken");
	}
}

输出:

I would like to eat - Pizza
I would like to eat - Chicken

Java Bean 中的虚拟函数

bean/POJO 类的 getter 函数也是虚拟的,可以被子类覆盖。请参阅下面的示例。

public class SimpleTesting extends Student{
	public int getId() {
		System.out.println("Id : "+id);
		return id;
	}
	public String getName() {
		System.out.println("Name : "+name);
		return name;
	}
	public int getAge() {
		System.out.println("Age : "+age);
		return age;
	}
	
	public static void main(String[] args){
		SimpleTesting student = new SimpleTesting();
		student.setId(101);
		student.setName("Rohan");
		student.setAge(50);
		student.getId();
		student.getName();
		student.getAge();
	}
}
class Student{
	int id;
	String name;
	int age;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

输出:

Id : 101
Name : Rohan
Age : 50

Java 中的非虚拟函数

私有或最终的函数是非虚拟的,因此它们不能被子类覆盖。在这个例子中,我们创建了两个函数,一个是最终的,第二个是私有的。我们试图将这些覆盖到子类中,但是由于非虚拟函数,java 编译器引发了错误。请参阅下面的示例和输出。

class Human{
	// non-virtual method
	final void eat(String choice) {
		System.out.println("I would like to eat - "+choice);
	}
	// non-virtual method
	private void buy(String item) {
		System.out.println("Buy me a "+item);
	}
}
public class SimpleTesting extends Human{
	// non-virtual method
	void eat(String choice) {
		System.out.println("I would like to eat - "+choice);
	}
	// non-virtual method
	void buy(String item) {
		System.out.println("Buy me a "+item);
	}
	public static void main(String[] args){
		SimpleTesting simpleTesting = new SimpleTesting();
		simpleTesting.eat("Pizza");
		simpleTesting.buy("Pizza");
		simpleTesting.eat("Chicken");
		simpleTesting.buy("Chicken");
	}
}

输出:

java.lang.IncompatibleClassChangeError: class SimpleTesting overrides final method Human.eat(Ljava/lang/String;)
---Cannot override the final method from Human---

Java 接口中的虚拟函数

接口的函数/方法默认是虚拟的,因为它们默认是公共的,并且应该被子类覆盖。在下面的例子中,我们在接口中创建了一个方法,在一个类中重写了它,并成功调用了它。请参阅下面的示例。

interface Eatable{
	void eat(String choice);
}
public class SimpleTesting implements Eatable{
	public void eat(String choice) {
		System.out.println("I would like to eat - "+choice);
	}
	void buy(String item) {
		System.out.println("Buy me a "+item);
	}
	public static void main(String[] args){
		SimpleTesting simpleTesting = new SimpleTesting();
		simpleTesting.eat("Pizza");
		simpleTesting.buy("Pizza");
		simpleTesting.eat("Chicken");
		simpleTesting.buy("Chicken");
	}
}

输出:

I would like to eat - Pizza
Buy me a Pizza
I would like to eat - Chicken
Buy me a Chicken

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:https://www.jiyik.com/tm/xwzj/prolan_7585.html

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

在 Pandas 的列中展平层次索引

发布时间:2024/04/24 浏览次数:1782 分类:Python

在这篇文章中,我们将使用不同的函数来使用 Pandas DataFrame 列来展平层次索引。我们将使用的方法是重置索引和 as_index() 函数。

Pandas 中的 Groupby 索引列

发布时间:2024/04/23 浏览次数:89 分类:Python

本教程将介绍如何使用 Python Pandas Groupby 对数据进行分类,然后将函数应用于类别。通过示例使用 groupby() 函数按 Pandas 中的多个索引列进行分组。

Pandas 中的散点矩阵

发布时间:2024/04/23 浏览次数:125 分类:Python

本教程演示了如何使用 scatter_matrix 函数在 Pandas 中创建散点图。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便