Java笔记(4)

来源:互联网 发布:百度算法工程师 编辑:程序博客网 时间:2024/05/16 19:53

1.引用类型须与对象类型一致。


2.boolean b = null ; (错)      //布尔型只能赋值true , false

   char c = 'a'(正确)

   char c = "c" ;(错)                 


3.A.equals(B)            A与B必须都是对象类型


4.C++中对对象初始化是先对对象分配内存空间,再对对象成员初始化。


5.Java中构造方法的名称和类名一致,构造方法在对象创建时对对象完成初始化。

             例:public Rock

                 {

                  Rock(){

                               System.out.println("creating Rock");

            }

}

                    public class SimpleConstructor{

                     public static void main(String args[]){

                                for(int i = 1;i<=2; i++)

                                 Rock c = new Rock( );

             }

}                                //输出:          creating Rock

                                                        creating Rock


若:Rock(int i){

         System.out.printle("creating Rock"+i);

}

              .

              .

              .

              .

       Rock c = new Rock(i)

             .

             .

             .                   //输出:       creating Rock 1

                                                    creating Rock 2


6.Java中方法的重载(overloading),对个方法名称与类名一致,参数类型不同。

          class Tree {

                         Tree(){

                                      System.out.println("planting a seeding");

                                   }

                          Tree(int i){

                                      System.out.println("planting a" + i + "cm tree");

                                         }

                           }

            public class Overloading{

                         public static void main(String args[]){

                                                    new Tree();

                                                    new Tree(20);

                                                                      }

                             }

                 //输出:         planting a seeding

                                       planting a 20cm tree


7.Java中以参数区分构造方法方法有两种:1)参数类型不同;2)参数个数不同。




                                   

           

原创粉丝点击