Topic: 算法匹配的问题

  Print this page

1.算法匹配的问题 Copy to clipboard
Posted by: phoenixdx
Posted on: 2005-11-06 19:06

类继承关系如下: Pie类和Cake类都继承于Dessert类,ApplePie类继承于Pie类,ChololateCake类继承于Cake类.假设在某一段代码中包含下列三个重载方法:
void order(Dessert d, Cake c); // 1 void order(Pie p, Dessert d);// 2
void order(ApplePie a, Cake c);//3
假如声明了一套变量,变量anApplerPie表示一个类型为ApplerPie的数值,变量aCholocateCake表示一个类型为CholocateCake的数值,等等.请问:order(anApplerPie,aCholocateCake)将调用哪个方法.麻烦详细讲解一下原因~~~
以下是完整代码:
public class Test {
public static void main(String [] args){
ApplePie anApplePie = new ApplePie();
ChocolateCake aChocolateCake = new ChocolateCake();
new Test1().order(anApplePie, aChocolateCake);
}
}
class Test1
{    
    Test1(){}
   void order(Dessert d ,Cake c){}
void order(Pie d ,Dessert c){}
void order(ApplePie a, Cake c){}
}
class Dessert
{ Dessert(){}
}
class Pie extends Dessert
{
Pie(){}
}
class ApplePie extends Pie
{
ApplePie(){}
}
class Cake extends Dessert
{
Cake(){}
}
class ChocolateCake extends Cake
{
ChocolateCake(){}
}

2.Re:算法匹配的问题 [Re: phoenixdx] Copy to clipboard
Posted by: bluecrystal
Posted on: 2005-11-06 19:19

你最好说明一下这几个order方法都在那一个类里面
所以,你最好写一个简化的代码出来描述这个问题

3.Re:算法匹配的问题 [Re: phoenixdx] Copy to clipboard
Posted by: phoenixdx
Posted on: 2005-11-06 21:23

改好了,希望有人帮解答~

4.Re:算法匹配的问题 [Re: phoenixdx] Copy to clipboard
Posted by: ranchgirl
Posted on: 2005-11-07 15:05

It will call order(ApplePie a, Cake c)
The rule is when ambiguity exists, the jvm will call the most fit one.

Easy test would be put a println in each method.

I rewrote your code to make it simple and clear. Compile it, run it, you will see what is happening.

By comparing your code and my code, you probably can learn some good programming practice too. Make sense of your program, even it is just a toy program.

Another thing you need to learn is compiler will provide you an empty default constructor. Therefore, I get rid of all your unnecessary code...


public class EatDesertTest {
void eat(Dessert d, Cake c){
System.out.println(" eat(Dessert d, Cake c)");
}
void eat(Pie d, Dessert c){
System.out.println(" eat(Pie d, Dessert c)");
}
void eat(ApplePie a, Cake c){
System.out.println(" eat(ApplePie a, Cake c)");
}
public static void main(String [] args){
ApplePie anApplePie = new ApplePie();
ChocolateCake aChocolateCake = new ChocolateCake();
new EatDesertTest().eat(anApplePie, aChocolateCake);
}
}
class Dessert {
}
class Pie extends Dessert {
}
class ApplePie extends Pie {
}
class Cake extends Dessert {
}
class ChocolateCake extends Cake {
}


I changed your order to eat, since you never need to provide the food your order from the restaurant. Haha....

5.Re:算法匹配的问题 [Re: phoenixdx] Copy to clipboard
Posted by: 五个人
Posted on: 2005-11-09 13:06

editor: your english is very good!
how do you learn english especially in java?

6.Re:算法匹配的问题 [Re: 五个人] Copy to clipboard
Posted by: ranchgirl
Posted on: 2005-11-10 02:15

五个人 wrote:
editor: your english is very good!
how do you learn english especially in java?


Like you just did, practice!

Read Java book/documentation/API in English, you get better books, you learn both Java and English.

You hit two birds with one stone, what a deal? Cool Big Smile


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923