Java中的编译时多态性
多态性是在Java环境中对象能力的声明。它允许我们以不同的方式执行相同的过程。Java中存在两种类型多态性:
编译时多态性方法
运行时多态性方法
今天,我们将讨论如何使用方法重载和运算符重载来实现编译时多态性。
编译时多态性示例
这是一个例子:
void ARBRDD() { ... }
void ARBRDD(int num1 ) { ... }
void ARBRDD(float num1) { ... }
void ARBRDD(int num1 , float num2 ) { ... }
show the value of ( char a )
show the value of ( char a ,char b )
show the value of ( float a float b)
show the value of ( int a, int b )
show the value of ( int a, float b )
show the value of ( float a, int b )
int sum value of (int, int);
String sum value of (int, int);
执行编译时多态性的算法
在这个可能的算法中,我们将向您展示如何在Java环境中执行编译时多态性。通过使用此算法,我们将构建一些Java语法,以有效的方式解释该过程。
步骤1 - 开始进程。
步骤2 - 导入并声明运行方法所需的Java包。
步骤3 - 声明一个公共类。
步骤4 - 提及字符串参数。
步骤5 - 创建并声明两个函数参数。
步骤6 - 定义函数参数一。
步骤7 - 定义函数参数二。
步骤8 - 显示两个列表。
步骤9 - 比较两个列表。
步骤10 - 如果评估结果为真,则打印相等消息。
步骤11 - 如果评估结果为假,则阻止执行过程并打印不相等文本。
步骤12 - 插入另一个元素并覆盖方法。
步骤13 - 显示两者。
步骤14 - 再次比较两者。
步骤15 - 获取结果。
步骤16 - 终止进程。
执行编译时多态性的语法
class SimpleCalculator{
int add(int a, int b){
return a+b;
}
int add(int a, int b, int c){
return a+b+c;
}
}
public class DemoCal{
SimpleCalculator obj = new SimpleCalculator();
System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20, 30));
}
}
class SimpleCalculator{
int add(int a, int b){
return a+b;
}
int add(int a, int b, int c){
return a+b+c;
}
}
public class DemoCal{
SimpleCalculator obj = new SimpleCalculator();
System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20, 30));
}
}
class MethodOverloading {
private static void display(int a){
System.out.println("Got Int data as a value.");
}
private static void display(String a){
System.out.println("Got String object as a value.");
}
public static void main(String[] args) {
display(4);
display("XYZ");
}
}
class Student{
public void stuIdentity(String name, int id){
System.out.println("stuName :" + name + " "
+ "Id :" + id);
}
public void stuIdentity(int id, String name){
System.out.println("Id :" + id + " " + "stuName :" + name);
}
}
class Main {
Student stu= new Student();
stu.stuIdentity("Mohit Roy", 1);
stu.stuIdentity(2, "Mohini Basu");
}
}
在上面这个可能的语法中,我们试图向您展示如何构建一个函数以在多态性方法中使用它。通过使用这些Java语法,我们正在朝着一些与编译时多态性相关的Java方法前进。
遵循的方法
方法1 - Java程序通过更改参数数量来演示方法重载的工作原理,以演示编译时多态性
方法2 - Java程序使用render()类型方法实现编译时多态性
方法1:使用参数数量执行编译时多态性
con_str方法的使用
在这种可能的方法中,我们将应用con_str方法,通过更改参数数量来演示编译时多态性的工作原理。
String con_str = s1 + s2;
System.out.println("Concatenated strings :"+ con_str);
示例
//Java program to demonstrate the working of method overloading by changing the number of parameters
public class ARBRDD {
void show(int num1){
System.out.println("number 1 : " + num1);
}
void show(int num1, int num2){
System.out.println("number 1 : " + num1 + " number 2 : " + num2);
}
public static void main(String[] args){
ARBRDD obj = new ARBRDD();
obj.show(3);
obj.show(4, 5);
}
}
输出
number 1 : 3 number 1 : 4 number 2 : 5
数据类型方法的使用
在这种可能的方法中,我们将应用数据类型模式方法,通过更改参数数量来演示编译时多态性的工作原理。
示例
//Java program to demonstrate the working of method overloading by changing the Datatype of parameter
public class ARBRDD {
static void show(int a, int b){
System.out.println("This is the integer function here");
}
static void show(double a, double b){
System.out.println("This is the double function here");
}
public static void main(String[] args){
show(1, 2);
show(1.2, 2.4);
}
}
输出
This is the integer function here This is the double function here
序列参数方法的使用
在这种可能的方法中,我们将应用序列参数方法,通过更改参数数量来演示编译时多态性的工作原理。
示例
//Java program to demonstrate the working of method overloading by changing the sequence of parameters
public class ARBRDD {
static void show(int a, char ch){
System.out.println("integer : " + a + " and character : " + ch);
}
static void show(char ch, int a){
System.out.println("character : " + ch + " and integer : " + a);
}
public static void main(String[] args){
show(6, 'G');
show('G', 7);
}
}
输出
integer : 6 and character : G character : G and integer : 7
方法2:render()方法的使用
在这种可能的方法中,我们将应用render方法,通过使用编译时多态性来解释运算符重载。
String s1 = sc.next();
System.out.println("Enter another string: ");
String s2 = sc.next();
System.out.println(s1+' '+s2);
System.out.println("Enter a number:");
int x = sc.nextInt();
System.out.println("Enter another number:");
int y = sc.nextInt();
示例1
//Java program to the use of the render() method for compile time polymorphism
class Polygon {
public void render() {
System.out.println("Rendering Polygon Value...");
}
}
class Square extends Polygon {
public void render() {
System.out.println("Rendering Square Value...");
}
}
class Circle extends Polygon {
public void render() {
System.out.println("Rendering Circle Value...");
}
}
public class ARBRDD {
public static void main(String[] args) {
Square s1 = new Square();
s1.render();
Circle c1 = new Circle();
c1.render();
}
}
输出
Rendering Square Value... Rendering Circle Value...
在这种可能的方法中,我们将应用显示信息方法,通过使用编译时多态性来解释运算符重载。
示例2
//Java program to the use of the overriding method for compile time polymorphism
class Language {
public void displayInfo() {
System.out.println("Common English Language");
}
}
class Java extends Language {
@Override
public void displayInfo() {
System.out.println("Java Programming Language");
}
}
public class ARBRDD {
public static void main(String[] args) {
Java j1 = new Java();
j1.displayInfo();
Language l1 = new Language();
l1.displayInfo();
}
}
输出
Java Programming Language Common English Language
在这种可能的方法中,我们将应用display()方法,通过使用编译时多态性来解释运算符重载。
示例3
//Java program to the use of the class contains a method named display() for compile time polymorphism
class Pattern {
public void display() {
for (int i = 0; i < 10; i++) {
System.out.print("@");
}
}
public void display(char symbol) {
for (int i = 0; i < 10; i++) {
System.out.print(symbol);
}
}
}
public class ARBRDD {
public static void main(String[] args) {
Pattern d1 = new Pattern();
d1.display();
System.out.println("\n");
d1.display('%');
}
}
输出
@@@@@@@@@@ %%%%%%%%%%
在这种可能的方法中,我们将应用一些多态变量和方法,通过使用编译时多态性来解释运算符重载。
示例4
//Java program to the use of the class contains a method named Polymorphic Variables for compile time polymorphism
class ProgrammingLanguage {
public void display() {
System.out.println("I am a programmer. I Am From India!");
}
}
class Java extends ProgrammingLanguage {
@Override
public void display() {
System.out.println("I am Java and I Am An Object-Oriented Programming Language.");
}
}
public class ARBRDD {
public static void main(String[] args) {
ProgrammingLanguage pl;
pl = new ProgrammingLanguage();
pl.display();
pl = new Java();
pl.display();
}
}
输出
I am a programmer. I Am From India! I am Java and I Am An Object-Oriented Programming Language.
结论
编译时多态性是一个早期绑定过程,通过它我们可以解决程序在执行模式下的重载问题。在今天的文章中,我们学习了关于编译时多态性的各种方法。通过使用算法和语法,我们还构建了一些Java代码,以有效的方式解释问题陈述。
另请阅读: Java面试问题及答案
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP