方法参数
一个方法不能修改一个基本数据类型的参数。
一个方法可以改变一个对象参数的状态。
一个方法不能让对象参数引用一个新的对象。
总之,调用方法时,参数的传递方式是值传递。
当参数是引用数据类型时,实际传递的是所引用内存实体的地址值。
- public class HelloJava
- {
- public static void main(String[] args)
- {
- System.out.println("Testing tripleValue:");
- double percent = 10;
- System.out.println("Before: percent=" + percent);
- tripleValue(percent);
- System.out.println("After: percent=" + percent);
-
- System.out.println("\nTesting tripleSalary:");
- Employee harry = new Employee("Harry", 50000);
- System.out.println("Before: salary=" + harry.getSalary());
- tripleSalary(harry);
- System.out.println("After: salary=" + harry.getSalary());
-
- System.out.println("\nTesting swap:");
- Employee a = new Employee("Alice", 70000);
- Employee b = new Employee("Bob", 60000);
- System.out.println("Before: a=" + a.getName());
- System.out.println("Before: b=" + b.getName());
- swap(a, b);
- System.out.println("After: a=" + a.getName());
- System.out.println("After: b=" + b.getName());
- }
-
- public static void tripleValue(double x)
- {
- x = 3 * x;
- System.out.println("End of method: x=" + x);
- }
-
- public static void tripleSalary(Employee x)
- {
- x.raiseSalary(200);
- System.out.println("End of method: salary=" + x.getSalary());
- }
-
- public static void swap(Employee x, Employee y)
- {
- Employee temp = x;
- x = y;
- y = temp;
- System.out.println("End of method: x=" + x.getName());
- System.out.println("End of method: y=" + y.getName());
- }
- }
- class Employee
- {
- private String name;
- private double salary;
- public Employee(String n, double s)
- {
- name = n;
- salary = s;
- }
-
- public String getName()
- {
- return name;
- }
-
- public double getSalary()
- {
- return salary;
- }
-
- public void raiseSalary(double byPercent)
- {
- double raise = salary * byPercent / 100;
- salary += raise;
- }
- }
复制代码 对象构造
如果多个方法有相同的名字,不同的参数,便产生了重载。
如果在构造器中没有显示地给域赋予初值,那么就会被自动地赋为默认值:数值为0,布尔值为false,对象引用为null。
如果类中没有构造器,系统会提供一个无参数的构造器,这个构造器将所有实力域设置为默认值。如果类中有构造器,系统不会提供构造器。
如果构造器的参数名与实例域名相同,可以采用this指针完成初始化,例如:
public Employee(String name, double salary)
{
this.name = name;
this.salary = salary;
}
如果构造器的第一个语句形如this(…),这个构造器将调用同一个类的另一个构造器。这种处理方式使得对公共构造器代码部分只编写一次即可。
还可以使用初始化块初始化域,初始化块先于构造器执行。
- import java.util.*;
- public class HelloJava
- {
- public static void main(String[] args)
- {
- Employee[] staff = new Employee[3];
-
- staff[0] = new Employee("Harry", 40000);
- staff[1] = new Employee(60000);
- staff[2] = new Employee();
-
- for (Employee e : staff)
- System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary=" + e.getSalary());
- }
- }
- class Employee
- {
- private static int nextId;
-
- private int id;
- private String name = "";
- private double salary;
-
- static
- {
- Random generator = new Random();
- nextId = generator.nextInt(10000);
- }
-
- {
- id = nextId;
- nextId++;
- }
- public Employee(String n, double s)
- {
- name = n;
- salary = s;
- }
-
- public Employee(double s)
- {
- this("Employee #" + nextId, s);
- }
-
- public Employee()
- {
-
- }
-
- public String getName()
- {
- return name;
- }
-
- public double getSalary()
- {
- return salary;
- }
-
- public int getId()
- {
- return id;
- }
- }
复制代码 包
标准Java包处于java和javax包层次中。使用包的主要原因是确保类名的唯一性。从编译器的角度看,嵌套的包之间没有任何关系。
使用import语句导入特定类或整个包,使用import static …导入静态方法和静态域。
使用package语句将类放入包中。
- import com.horstmann.corejava.*;
- import static java.lang.System.*;
- public class HelloJava
- {
- public static void main(String[] args)
- {
- Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);
-
- harry.raiseSalary(5);
-
- out.println("name=" + harry.getName() + ",salary=" + harry.getSalary());
- }
- }
复制代码- package com.horstmann.corejava;
- import java.util.*;
- public class Employee
- {
- private String name;
- private double salary;
- private Date hireDay;
-
- public Employee(String n, double s,int year, int month, int day)
- {
- name = n;
- salary = s;
- GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
- hireDay = calendar.getTime();
- }
-
- public String getName()
- {
- return name;
- }
-
- public double getSalary()
- {
- return salary;
- }
-
- public Date getHireDay()
- {
- return hireDay;
- }
-
- public void raiseSalary(double byPercent)
- {
- double raise = salary * byPercent / 100;
- salary += raise;
- }
- }
复制代码 没有指定public或private的类,方法,域可以被同一个包中的所有方法访问。
|
|