[Core Java. Volume I. Fundamentals, 8th Edition]-4

C++类方法的定义位置
   C++ NOTE: In C++, you generally define methods outside the class: 
                             void Employee::raiseSalary(double byPercent) // C++, not Java 
                             { 
                                . . . 
                             } 
  If you define a method inside a class, then it is automatically an inline method. 
                             class Employee 
                             { 
                                . . . 
                                int getName() { return name; } // inline in C++ 
                             } 


关于java传参的方式的一直误解

 As you have seen, it is easily possible—and in fact very common—to implement methods that change the state of an object parameter. The reason is simple. Themethod gets a copy of the object reference, and both the original and the copy refer to the same object. 

 Many programming languages (in particular, C++ and Pascal) have two methods for parameter passing:call by value andcall by reference. Some programmers (and unfortunatelyeven some book authors) claim that the Java programming language uses call by reference for objects. However, that is false. Because this is such a common misunderstanding, it is worth examining a counterexample in detail. 

Let’s try to write a method that swaps two employee objects: 

                       public static void swap(Employee x, Employee y) // doesn't work 
                       { 
                          Employee temp = x; 
                          x = y; 
                          y = temp; 
                      } 
    If the Java programming language used call by reference for objects, this method would  work: 
                      Employee a = new Employee("Alice", . . .); 
                      Employee b = new Employee("Bob", . . .); 
                      swap(a, b); 
                      // does a now refer to Bob, b to Alice? 
      However, the method does not actually change the object references that are stored in the variables a and b. The x and y parameters of the swap method are initialized with copies of these references. The method then proceeds to swap these copies


关于变量的初始化

NOTE: This is an important difference between fields and local variables. You must always explicitly initializelocal variables in a method. But if youdon’t initialize a field in aclass, it is automatically initialized to a default (0, false, or null). 


关于函数方法的参数

使用aXX作为参数名,如function(int aInt,double aDouble)

另一种常用的方法是relies on the fact that parameter variables shadow instance fields with the same name. For example, if you call a parametersalary, then salary refers to  the parameter, not the instance field. But you can still access the instance field as this.sal ary. Recall that this denotes the implicit parameter, that is, the object that is being constructed. Here is an example:                   

     public Employee(String name, double salary) 
                       { 
                          this.name = name; 
                          this.salary = salary; 
                       } 

as3中不成立,会造成命名冲突


构造函数的相互调用

If the first statement of a constructor has the form this(. . .), then the constructor calls another constructor of the same class. Here is a typical example: 

                       public Employee(double s) 
                       { 
                          // calls Employee(String, double) 
                          this("Employee #" + nextId, s); 
                          nextId++; 
                       } 

static import

   import static java.lang.System.*; 
这样调用静态方法或静态变量的时候可以省略类名
                     out.println("Goodbye, World!"); // i.e., System.out 
                     exit(0); // i.e., System.exit 

                     import static java.lang.System.out; 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值