Java:“类重载" [英] Java: "Class Overloading"

查看:45
本文介绍了Java:“类重载"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,假设我有一个名为Orange的类文件,然后有两个分别为Color和Fruit的类文件.

Okay, so let's say I have a class file called Orange, and then two separate class files called Color, and Fruit.

在Orange内部,有一些颜色,大小,成熟度等属性,以及setSize(int size)方法.

Inside Orange, there are some properties for color, size, ripeness etc. and the method setSize(int size).

在Fruit里面,有一些属性,包括名称,水果类型,是否有种子,从何处导入等等.然后是setName(String name),eat(),wash()和Give的方法.(人).

Inside Fruit, there are some properties for name, type of fruit, whether or not it has seeds, where it was imported from etc. and then the methods setName(String name), eat(), wash() and give(Person person).

内部颜色中有一些属性,包括名称,RGB值,十六进制值,Alpha值等.然后是方法setName(),setAlpha(int alpha),setRGB(int r,int g,int b)和setHex(十六进制).

Inside Color there are some properties for name, RGB values, Hex values, Alpha values etc. and then the methods setName(), setAlpha(int alpha), setRGB(int r, int g, int b) and setHex(int hex).

上面提到的大多数信息都是无关紧要的,仅仅是为了帮助您理解.Orange Class文件中包含一个名为父对象"的对象.我需要知道的是,是否可以将对象父对象"设置为水果"或颜色"?稍后在主类文件中出现myOrange.Parent.setName("Bob")发生的情况,并且在两种情况下(无论Parent是Fruit还是Color)都是可行的,因为Fruit具有名为setName的方法,颜色也一样.

Most of the above mentioned information is irrelevant, and solely there to assist you in understanding. The Orange Class file has an object in it called "Parent". What I need to know is, is it possible for the object "Parent" to be set to either a Fruit, or a Color? Later on in the main class file there comes a point where myOrange.Parent.setName("Bob") happens, and in both cases (whether Parent is a Fruit or a Color) it would be possible, because Fruit has a method called setName, and Color does as well.

谢谢

推荐答案

类型继承(类重载"?)表示是"关系.橘子是一种水果.不需要其他变量;继承的成员可通过 this 访问.

Type Inheritance ("class overloading"?) represents an "is-a" relationship. The Orange is a Fruit. No additional variables are needed; inherited members are accessed via this.

组成表示具有"关系.橙色有颜色.使用此模式时,将声明一个单独的变量,因此很明显,这就是 Parent 所使用的.

Composition represents a "has-a" relationship. The Orange has a Color. A separate variable is declared when using this pattern, so this is clearly what is being used for Parent.

因此 Parent (一个可怕的命名变量)是Color的一个实例.

Thus Parent (a horribly named variable) is an instance of Color.

这篇关于Java:“类重载"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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