Topic: 请大家帮忙看一个小程序!谢谢了!(protected的用法)

  Print this page

1.请大家帮忙看一个小程序!谢谢了!(protected的用法) Copy to clipboard
Posted by: jakiey
Posted on: 2005-03-30 16:25

请尽量用准确的文字描述作为标题

read the book first and ask more specific questions...
is this example from Mr. Eckel's Thinking in Java?



class Instru{
  public void play(){
    System.out.println("pass on");
}
  
  protected void tune(Instru i){
      i.play();
  }
   
}

public class Wind extends Instru {
  

  public static void main(String[] args) {
    
    Wind flute = new Wind();
    Instru.tune(flute);
    
  }  
}

为什么程序中的protected不能用?那应该怎样用protected?能不能讲一下protected的用法!小弟在此先谢了。

2.Re:请大家帮忙看一个小程序!谢谢了!(protected的用法) [Re: jakiey] Copy to clipboard
Posted by: daniel84
Posted on: 2005-04-01 09:28

因为Instru不是静态的类,所以不能用Instru调用tune的方法,应用Wind的对象flute调用。程序如下:
class Instru{
public void play(){
System.out.println("pass on");
}

protected void tune(Instru i){
i.play();
}

}

public class Wind extends Instru {


public static void main(String[] args) {

Wind flute = new Wind();
flute.tune(flute);

}
}

3.Re:请大家帮忙看一个小程序!谢谢了!(protected的用法) [Re: jakiey] Copy to clipboard
Posted by: jakiey
Posted on: 2005-04-01 17:24

对,这样也行的,不过把protected换成static 程序也能运行,结果一样的,
请问这样的调用一样吗?

4.Re:请大家帮忙看一个小程序!谢谢了!(protected的用法) [Re: jakiey] Copy to clipboard
Posted by: wddodo
Posted on: 2005-04-02 13:52

兄弟,你想这样做吧:
class Instru{
public void play(){
System.out.println("pass on");
}

protected void tune(Instru i){
i.play();
}

}

public class Wind extends Instru {


public static void main(String[] args) {

Wind flute = new Wind();
flute.tune(flute);

}
}

5.Re:请大家帮忙看一个小程序!谢谢了!(protected的用法) [Re: jakiey] Copy to clipboard
Posted by: jakiey
Posted on: 2005-04-04 08:04

谢谢上楼的那位朋友了,我是想那样做的。


   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