Home Backend Development PHP Tutorial Polymorphic member characteristics

Polymorphic member characteristics

Nov 26, 2016 am 10:14 AM
Polymorphism

⒈Characteristics of non-static member functions in polymorphism:
①At compile time: See whether there is a calling method in the class to which the reference variable belongs. If there is, the compilation passes, otherwise the compilation fails;
② During runtime: See whether there is a method to be called in the class to which the object belongs.
------------------------------------------------- -
//In short: when a member function is called, look at the left side when compiling and the right side when running. //
-------------------------------------------------- ---
2. Characteristics of member variables with the same name in polymorphism: (ps: different names have not been verified)
Refer to the left side (the class to which the reference variable belongs) regardless of compilation or running
3. Static members in polymorphism Features of the function:
Regardless of compilation or running, refer to the left side (the class to which the reference variable belongs)
example:

Class Fu 
{ 
int num=1;
public void method1()
{
System.out.println("fu_1");
}
public void method3()
{
System.out.println("fu_3");
}
//静态方法(包括变量)不所属于对象,它绑定于所属的类,会在内存中提前加载出来
public static void method4()
{
System.out.println("fu_4");
}
}
Class Zi extends Fu
{
int num=2;
public void method1()
{
System.out.println("zi_1");
}
public void method2()
{
System.out.println("zi_2");
}
public static void method4()
{
System.out.println("zi_4");
}
}
Class Duotaitest
{
public static void main (String [] args)
{
Fu f=new Zi ();
f.method1();
//f.method2(); 此行代码若存在,则编译失败,Fu类中没此方法
f.method3();
f.method4();
System.out.println(f.num);
Zi z= new Zi();
System.out.println(z.num);
}
/* 
运行结果: zi_1
fu_3
fu_4 //静态方法不能被子类重写覆盖,若想调用子类中的静态方法(一般没这么用的,只是面试会用到)Zi.method4(类名.方法名)
1
2
*/
}
Copy after login



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C++ virtual function table and polymorphic implementation, how to avoid memory waste C++ virtual function table and polymorphic implementation, how to avoid memory waste May 31, 2024 pm 07:03 PM

Virtual base classes optimize vtable memory overhead by allowing inheritance from multiple base classes without creating additional vtables. In the optimized code, the shape base class no longer has a virtual function table, and the circle and rectangle classes share the same virtual function table, thus reducing memory consumption.

How to implement polymorphic storage and multidimensional query of data in MySQL? How to implement polymorphic storage and multidimensional query of data in MySQL? Jul 31, 2023 pm 09:12 PM

How to implement polymorphic storage and multidimensional query of data in MySQL? In practical application development, polymorphic storage and multidimensional query of data are a very common requirement. As a commonly used relational database management system, MySQL provides a variety of ways to implement polymorphic storage and multidimensional queries. This article will introduce the method of using MySQL to implement polymorphic storage and multi-dimensional query of data, and provide corresponding code examples to help readers quickly understand and use it. 1. Polymorphic storage Polymorphic storage refers to the technology of storing different types of data in the same field.

How to use polymorphism and inheritance in PHP to deal with data types How to use polymorphism and inheritance in PHP to deal with data types Jul 15, 2023 pm 07:41 PM

How to use polymorphism and inheritance to handle data types in PHP Introduction: In PHP, polymorphism and inheritance are two important object-oriented programming (OOP) concepts. By using polymorphism and inheritance, we can handle different data types more flexibly. This article will introduce how to use polymorphism and inheritance to deal with data types in PHP, and show their practical application through code examples. 1. The basic concept of inheritance Inheritance is an important concept in object-oriented programming. It allows us to create a class that can inherit the properties and methods of the parent class.

Inheritance, polymorphism and interfaces: three major object-oriented features of PHP Inheritance, polymorphism and interfaces: three major object-oriented features of PHP May 11, 2023 pm 03:45 PM

PHP is a server-side programming language that supports object-oriented programming (OOP) since PHP5. The core idea of ​​OOP is to encapsulate data and behavior in objects to improve the maintainability and scalability of the program. In PHP, object-oriented programming has three major characteristics: inheritance, polymorphism and interfaces. 1. Inheritance Inheritance means that a class can inherit properties and methods from another class. The inherited class is called the parent class or base class, and the inherited class is called the subclass or derived class. Subclasses can obtain the properties and methods in the parent class through inheritance, and can

'Introduction to Object-Oriented Programming in PHP: From Concept to Practice' 'Introduction to Object-Oriented Programming in PHP: From Concept to Practice' Feb 25, 2024 pm 09:04 PM

What is object-oriented programming? Object-oriented programming (OOP) is a programming paradigm that abstracts real-world entities into classes and uses objects to represent these entities. Classes define the properties and behavior of objects, and objects instantiate classes. The main advantage of OOP is that it makes code easier to understand, maintain and reuse. Basic Concepts of OOP The main concepts of OOP include classes, objects, properties and methods. A class is the blueprint of an object, which defines its properties and behavior. An object is an instance of a class and has all the properties and behaviors of the class. Properties are characteristics of an object that can store data. Methods are functions of an object that can operate on the object's data. Advantages of OOP The main advantages of OOP include: Reusability: OOP can make the code more

What is the difference between golang function overloading and polymorphism? What is the difference between golang function overloading and polymorphism? Apr 30, 2024 am 09:30 AM

Function overloading is not supported in Go language because it adopts duck typing and determines the value type based on the actual type. Polymorphism is achieved through interface types and method calls, and objects of different categories can respond in the same way. Specifically, by defining interfaces and implementing these methods, Go language can make objects of different types have similar behaviors, thereby supporting polymorphism.

Analyze the relationship between polymorphism and interfaces in PHP object-oriented programming Analyze the relationship between polymorphism and interfaces in PHP object-oriented programming Aug 10, 2023 pm 06:09 PM

The relationship between polymorphism and interfaces in PHP object-oriented programming In PHP object-oriented programming, polymorphism (Polymorphism) is an important concept, which allows objects of different types to be used in a unified way. Polymorphism is realized through the implementation of interface (Interface). This article will use code examples to analyze the relationship between polymorphism and interfaces in PHP object-oriented programming. In PHP, an interface is an abstract structure that defines a set of methods. Classes express themselves with certain behavioral capabilities by implementing interfaces. interface

The relationship between polymorphism and dispatch mechanism in PHP The relationship between polymorphism and dispatch mechanism in PHP Jul 07, 2023 pm 05:45 PM

The relationship between polymorphism and dispatch mechanism in PHP In object-oriented programming, polymorphism is a powerful concept that allows different objects to respond differently to the same message. As a powerful development language, PHP also supports polymorphism, and closely related to it is the dispatch mechanism. This article will use code examples to explore the relationship between polymorphism and dispatch mechanisms in PHP. First, let’s understand what polymorphism is. Polymorphism means that an object can call corresponding methods according to its actual type. By using polymorphism, a program can be based on specific objects

See all articles