复杂的事情要简单做;   简单的事情要认真做;   认真的事情要重复做;   重复的事情要创造性地做。

信心 恒心 决心 创意 乐意

も军魂ミ 心系java

Java的魅力:跨平台、动态的Web、Internet计算 Java在Web、移动设备以及云计算方面前景广阔 我的java编程不是梦!!! 俗话说的好:“授人以鱼不如授人以渔”.
posts - 10, comments - 12, trackbacks - 0, articles - 5
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

         方法重载(Overloading):它是定义在同一个类中,并且方法名称相同,而参数的类型个数不同。
                                       eg:public class Test{
                                                  public static void main(String[] args){
                                                             Test c=new Test();
                                                             String str=c.connect("He","ll","o");
                                                             String str1=c.connect("J","a  va");
                                                             System.out.println(str);
                                                             System.out.println(str1);
                                                         } 
                                                 public String connect(String x,String y,String z){
                                                              return x+y+z;
                                                         }
                                                  public String connect(String x1,String y1){
                                                              return x1+y1; 
                                                        } 
                                               }
                            最后输出结果为: Hello
                                                          Java

             方法重写/覆写(Override):他是是发生在继承关系之中,是由子类进行覆写的,并且方法名称相同参数的类型个数需相同,而重写方法的访问权限则不能太严格。  
      优点:能够定义子类中特有的特征 
      注意:访问修饰符的限制一定要大于被重写方法的访问修饰符即 (public>protected>default>private)
                 eg:      class Father{
                             public void Speak(){
                                 System.out.println("I'm love son");
                                   }
                            }
                          class Son extends Father{
                             public void Speak(){
                                 System.out.println("I'm love father,too");
                                   }
                            }
                          public class Test1 {
                              public static void main(String[] args){
                                 Father s=new Son();
                                   s.Speak();
                                   }
                            }    
 

            最后输出结果为:  I'm love father,tooI'm love father,too      
     若将上例中Father类的speak()方法改为private时,Son类将不能重写speak()方法,此时在Son类中定义的speak()方法相当于一个新的方法,与Father中的speak()方法没有任何关系。 而若将上例中Father类的speak()方法声明为final时,无论该方法的修饰符是public,protected还是默认的,Son类根本不能重写speak()方法,试图编译代码时,编译器会报错。                                                     
                                                                        
       


只有注册用户登录后才能发表评论。


网站导航: