class Base{
public void hello(){
System.out.print("Hello in Base");
}
}
class Sub extends Base{
public void hello(){
System.out.print("Hello in Sub");
}
public void hello(){
System.out.print("Welcome in Sub");
}
}
public static void main(String[] args)
{
Base b=new Sub();
b.hello();
b.welcome();
}
怎么改???????
有几种方法????????

解决方案 »

  1.   

    Base 的引用类型 b根本没有b.welcome(); 这个方法
      

  2.   

    你应该是想在子类里面写一个welcome方法写错了吧, 写成hello, 所以那里有两个hello如果是这样的话调用welcome将会想楼上所说的Base类中没有welcome这个方法
    因为你用一个Base类来接一个子类的实例
    就相当于把子类抽象了一层, 所以调用不到子类的方法
      

  3.   

    class Base{ 
    public void hello(){ 
    System.out.print("Hello in Base"); 


    class Sub extends Base{ 
    public void hello(){ 
    System.out.print("Hello in Sub"); 

    public void welcome(){ 
    System.out.print("Welcome in Sub"); 


    public static void main(String[] args) 

    Base b=new Sub(); 
    b.hello(); 
    b.welcome(); 

    怎么改??????? 
    有几种方法????????
      

  4.   


    class Base{  
        public void hello(){  
               System.out.print("Hello in Base");  
        }
        public void  welcome(){} 
    }  
    class Sub extends Base{  
        public void hello(){  
               System.out.print("Hello in Sub");  
        }  
        public void welcome(){  
               System.out.print("Welcome in Sub");  
        }   
        public static void main(String[] args){  
               Base b=new Sub();  
               b.hello();  
               b.welcome();  
    }  
    Base b=new Sub();
    “is a”的问题,b只能调用Base中定义的方法,当调用的方法在其派生类被覆盖,实际上是调用覆盖的方法
      

  5.   

    class Base {
    public void hello() {
    System.out.print("Hello in Base");
    } public void welcome() {
    System.out.print("Welcome in Base");
    }
    } class Sub extends Base {
    public void hello() {
    System.out.print("Hello in Sub");
    } public void welcome() {
    System.out.print("Welcome in Sub");
    }
    } public static void main(String[] args) {
    Test1.Base b = new Test1().new Sub();
    b.hello();
    b.welcome();
    }
    首先是像楼上的说的那样要在Base类中写个welcome()方法,其次是内部类的声明方式得注意下哦
      

  6.   

    主要是缺少了一个welcome()的方法,这个方法在父类和子类中都可以的.
    另外你这里父类的方法hello()被子类的方法覆盖了.这就体现了java的多态性,
    一个方法可以有多种实现形式.
    至于怎么该方法很多也不知道你具体要问什么啊?
      

  7.   

     编译都不会通过,找不到welcome方法,
       如5楼 修改 可以,但是否达到你要的那个意思???
      

  8.   

    你在父类中没有写WELCOME方法,所以子类无法继承并重写该方法