C/C++继承类的多态表现

学过C++和Java的人都知道,他们二者由于都可以进行面向对象编程,而面向对象编程的三大特性就是封装、继承、多态,所有今天我们就来简单了解一下C++和Java在多态这方面的不同。

首先我们各看一个案例。

C++

//测试继承与多态
class Animal {

public:
	char name[128];
	char behavior[128];

	void outPut() {
		cout << "Animal" << endl;
	}

	void outPutAnimal() {
		cout << "Father" << endl;
	}

	Animal() {
		strcpy(name, "Animal");
		strcpy(behavior, "Call");
	}
};

class Dog: public Animal {

public:
	//子类定义一个和父类相同的属性
	char name[128];
	char sex;

	//子类重写父类方法
	void outPut() {
		cout << "Dog" << endl;
	}

	void outPutDog() {
		cout << "Son" << endl;
	}

	Dog() {
		strcpy(name, "Dog");
		sex = 'M';
	}
};

以上两个类都很简单,我们来看一下其测试代码和结果。

/*
没有多态的情况下测试结果和Java相同
	Dog dog;
	cout << dog.name << endl;
	cout << dog.sex << endl;
	cout << dog.behavior << endl;
	dog.outPut();
	dog.outPutAnimal();
	dog.outPutDog();
	
	//可通过如下方式访问父类的行为
	dog.Animal::outPut();
*/

//多态的情况下:
	Animal *dog = new Dog;

	cout << dog->name << endl;
	cout << dog->behavior << endl;
	dog->outPut();
	dog->outPutAnimal();

//测试结果
Animal
Call
Animal
Father

可以看出所有的表现都是父类的行为,无论是相同的属性还是重写的方法。在这里需要说明一下,**如果子类隐藏了父类的成员函数,则父类中所有同名的成员函数(重载的函数)均被隐藏。**如果想要访问父类中被隐藏的函数,需要通过父类名称来访问(dog.Animal::outPut();)。
在多态的情况下我们访问的都是父类的行为,那怎样才能访问到子类的函数呢?答案是通过定义虚函数来实现,这个我们后面的博文讲解。

现在我们在来看一下Java的表现。

Java

//父类
public class Animal {

	public String name = "Animal";
	public String behavior = "Call";
	
	public void outPut() {
		System.out.println("Animal");
	}

	public void outPutAnimal() {
		System.out.println("Father");
	}
}

//子类
public class Dog extends Animal{
	
	public String name = "Dog";
	public char sex = 'M';

	public void outPut() {
		System.out.println("Dog");
	}

	public void outPutDog() {
		System.out.println("Son");
	}
}

子类也是定义了一个和父类相同的属性,同时也重写了父类的一个方法,我们看一下其测试方法和测试结果。

public static void main(String[] args) {

	Animal dog = new Dog();

	System.out.println(dog.name);
	System.out.println(dog.behavior);
	dog.outPut();
	dog.outPutAnimal();

}

//测试结果
Animal//父类行为
Call
Dog//表现的是子类的行为
Father

从这里可以看出,Java和C++还是有区别的,Java的属性表现的是父类的行为,但是重写的方法却是子类的行为,而C++全部都是父类的行为。

Python

class Animal:
    name = 'Animal'
    ani_b = 'Animal b'
    __ani_c = 'Animal c'

    def run(self):
        print('Animal is running')

class Dog(Animal):
    name = 'Dog'
    dog_d = 'Dog d'

    def run(self):
        print('Dog is running')

    def eat(self):
        print('Dog eat')

class Cat(Animal):
    name = 'Cat'
    __cat_c = 'Cat e'
    
    def run(self):
        print('Cat is running')

    def sleep(self):
        print('Cat sleep')

def run_twice(mal):
    mal.run()


ani = Animal()
dog = Dog()
cat = Cat()
print('--------------------')
ani.run()
dog.run()
cat.run()
dog.eat()
cat.sleep()
print('--------------------')
print(ani.name)
print(dog.name)
print(cat.name)
print('--------------------')
print(ani.ani_b)
print(dog.ani_b)
print(cat.ani_b)
print('--------------------')
# print(ani.__ani_c)
print(dog.dog_d)
# print(cat.__cat_c)
print('--------------------')
print(isinstance(dog, Dog))
print(isinstance(dog, Animal))
print(isinstance(dog, Cat))
print('--------------------')
run_twice(dog)
run_twice(cat)
run_twice(ani)
--------------------
Animal is running
Dog is running
Cat is running
Dog eat
Cat sleep
--------------------
Animal
Dog
Cat
--------------------
Animal b
Animal b
Animal b
--------------------
Dog d
--------------------
True
True
False
--------------------
Dog is running
Cat is running
Animal is running

总结

Java的属性表现的都是父类的行为,但是重写的方法却是子类的行为,
C++全部都是父类的行为,但是virtual函数会表现出子类的行为。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪舞飞影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值