Class polymorphism: member variables and member method access characteristics (C++, Java)

Class polymorphism: member variables and member method access characteristics (C++, Java)

1. The polymorphic format of classes in C++ and Java languages is:

C++:父类名称 &引用对象名 = 子类名称 对象名
Java:父类名称 对象名 = new 子类名称

2. The member variables and member methods (functions) to access the same characteristics, the law as follows:
For the member variables are watching when the parent code is compiled and executed, to the basis of the parent class (parent class value and the presence or absence of the standard);
for Member method, compile based on the parent class (no parent class will compile an error), and execute based on the subclass (execute the subclass method), but the premise is that the subclass must overload the method. In C++, the parent class The function must also be a virtual function.
3. Verify the
C++ code (full version):

#include<iostream>
using namespace std;

class Animal
{
    
    
	public:
		string name="animal";
		int age=10; 
		virtual void eat()
		{
    
    cout<<name<<"吃东西"<<endl; 
		}
		virtual string getname()
		{
    
    return name;
		}
		virtual int getage()
		{
    
    return age;
		}
		int speak()
		{
    
    cout<<name<<"在说话"<<endl; 
		}
};
class Cat:public Animal
{
    
    
	public:
		string name="Tom";
		int age=3; 
		void eat()
		{
    
    cout<<name<<"吃鱼"<<endl; 
		}
		string getname()
		{
    
    return name;
		}
		int getage()
		{
    
    return age;
		}
		void work()
		{
    
    cout<<name<<"会抓老鼠"<<endl;
		}
};
class Dog:public Animal
{
    
    
	public:
		string name="旺财";
		int age=5; 
		void eat()
		{
    
    cout<<name<<"吃SHIT"<<endl;
		}
		void work()
		{
    
    cout<<name<<"会看门"<<endl; 
		}
		int speak()
		{
    
    cout<<name<<"汪汪叫"<<endl; 
		}
};

void test()
{
    
    
	Cat cat;
	Animal &animal1 = cat;	 //多态实现 
	cout<<"直接访问共有成员方法:"; 
	animal1.eat();		//虚函数重载后执行子类方法 
	cout<<"直接访问成员变量:"<<animal1.name<<"今年"<<animal1.age<<"岁啦"<<endl;	//成员变量一定返回父类的 
	cout<<"通过成员方法间接访问成员变量:"<<animal1.getname()<<"今年"<<animal1.getage()<<"岁啦"<<endl;	//可以通过虚函数的方法间接调用子类成员变量 
	//animal1.work();	//父类无此方法,不能访问,编译错误 
	cout<<"======================================================"<<endl;
	Dog dog;
	Animal &animal2 = dog;
	cout<<"直接访问共有(重载过)成员方法:"; 
	animal2.eat();
	cout<<"直接访问成员变量:"<<animal2.name<<"今年"<<animal2.age<<"岁啦"<<endl;
	cout<<"通过成员方法间接访问成员变量:"<<animal2.getname()<<"今年"<<animal2.getage()<<"岁啦"<<endl;
	cout<<"调用共有非虚函数方法:";
	animal2.speak();	//非虚函数重载后一定会执行父类方法 
}
int main()
{
    
    
	test();
	return 0;
} 

Execution result:
Insert picture description here
Java code (partially, can be verified by adding comments):
Animal class:

package 多态验证;

public class Animal {
    
    
	String name = "animal";
	int age = 10;
	public void eat()
	{
    
    System.out.print(name + "吃东西");
	}
	public String getname()
	{
    
    return name;
	}
	public int getage()
	{
    
    return age;
	}
}

Cat category:

package 多态验证;

public class Cat extends Animal{
    
    
	String name = "Tom";
	int age = 3;
	public void eat()
	{
    
    System.out.println(name + "吃鱼");
	}
	public String getname()
	{
    
    return name;
	}
	public int getage()
	{
    
    return age;
	}
}

Main function:

package 多态验证;

public class test {
    
    
	public static void main(String[] args) {
    
    
		test0();
	}
	public static void test0(){
    
    
		Animal animal = new Cat();
		System.out.print("直接访问共有成员方法:");
		animal.eat();
		System.out.println("直接访问成员变量:" + animal.name + "今年" + animal.age+ "岁啦");
		System.out.println("通过成员方法间接访问成员变量:" + animal.getname() + "今年" + animal.getage()+ "岁啦");
	}
}

Results of the:Insert picture description here

Guess you like

Origin blog.csdn.net/hyl1181/article/details/107708820