OOP-继承 [英] OOP - Inheritance

查看:80
本文介绍了OOP-继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一堂课,请上动物课.

If I have a class, class Animal.

我有一个Dog和Cat都继承了Animal.

And I have a class Dog and a class Cat both inheriting Animal.

Cat可以在Animal内部调用方法.但是Cat可以调用Dog的方法吗?

Cat can call methods inside Animal. But can Cat call methods of Dog?

推荐答案

在超类中定义的方法可用于任何子类.但是姐妹"类无法访问彼此的(私有)方法.

The methods defined in a superclass are accesible to any subclass. But "sister" classes cannot access the (private) methods of one another.

因此,如果您的类CatDog继承自Animal,则CatDog对象都可以从Animal调用方法,但是它们不能(必须 )互相访问方法.

So if your classes Cat and Dog inherit from Animal, then both Cat and Dog objects can call methods from Animal, but they cannot (must not) access the methods of each other.

这篇关于OOP-继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆