一、构造器
- 构造器与类同名
- 每个类可以有一个以上的构造器
- 构造器可以有0个、1个或多个参数
- 构造器没有返回值
- 构造器总是伴随着new操作一起调用
例如有一个Employee类,构造器有0个、1个或多个参数,且构造器没有返回值,以及构造器总是伴随着new操作一起调用。
public Employee() {
}
public Employee(String name) {
this.name = name;
}
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
Employee employee = new Employee();
二、类的方法
类方法有:私有方法、静态方法,公有方法,工厂方法,main方法
1、私有方法:可能与掐灭的实现机制非常紧密,或者需要一个特别的协议以及一个特别的调用次序。用关键字:private来修饰私有方法
私有方法不会被外部的其他类调用,或者将其删除。通俗的讲,私有方法相当于自己情感,别人无法拿去,也无法将其消除。
2、 静态方法:用static修饰的方法。是一种不能向对象实时操作的方法。例如,Math类的ppow方法就是以恶静态方法。表达式::Math.pow(x,a),表示计算幂 x的a次。
先要了解静态域
静态域:将域定义为static,每个类中只有一个这样的域
例如: Employee类添加一个实力域id和一个静态域nextId。
public class Employee {
private static int nextId = 1;
private int id;
}
实力域和静态域的区别:如果有1000个Employee类的对象,则有1000个实力域id。但是,只有一个静态域nextId。静态域属于类,不属于任何的独立的对象。
静态方法的两种使用情况:
1)、一个方法不需要访问对象状态,其所需参数都是通过显示参数提供。(例如:Math.pow)
2)、一个方法只需要访问类的静态域(例如:Employee.getNextId)
3、工厂方法
工厂方法是静态方法的另外一种常见的用途。类似LocalDate和NumberFormat的类使用静态工厂方法来构造对象。
已经见过的工厂方法:LocalDate.now和LocalDate.of。
NumberFormat类也有类似的构造方法:
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
NumberFormat percentFormatter = NumberFormat.getPercentInstance();
double x = 0.1;
System.out.println(currencyFormatter.format(x));//prints $0.10
System.out.println(percentFormatter.format(x));//prints $10%
为什么 NumberFormat类不利用构造器完成这些此操作呢?有两个主要的原因:
- 无法命名构造器。构造器的名字必须域类名相同。但是,这里希望将得到的货币实例与百分比实例采用不同的名字
- 当使用构造器时,无法改变所构造的对象类型。而Factory方法将返回一个DecimalFormat类对象,这是NumberFormat的子类。
4、main方法
main方法也是一个静态方法。
public class ParamTest {
public static void main(String[] args) {
//construst objects here
}
}
main方法不对任何对象进行操作。事实上,在启动程序时还没有任何一个对象。静态的main方法将执行并创建程序所需要的对象。
三、方法参数
方法中可以有0个、1个或多个参数。
Java中方法参数的使用情况:
1)、一个方法不能修改一个基本数据类型的参数(即数值型或布尔型)
2)、一个方法可以改变的一个对象参数的状态
3)一个方法不能让对象参数引用一个新的对象
例如:有一个Employee类和ParamTest类
package ThreeDay;
import java.time.LocalDate;
public class Employee {
private static int nextId = 1;
//instance fields
private String name;
private double salary;
private LocalDate hireDay;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public void setId() {
id = nextId;
nextId++;
}
public static int getNextId() {
return nextId;
}
public int getId() {
return id;
}
private int id;
//constructor
public Employee(String n, double s, int year, int month, int day)
{
name = n;
salary = s;
hireDay = LocalDate.of(year,month,day);
}
//a method
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public LocalDate getHireDay() {
return hireDay;
}
public void raiseSalary(double byPercent){
this.salary = salary * (1+byPercent/100);
}
}
package ThreeDay;
public class ParamTest {
public static void main(String[] args) {
/*
Test1:Methods can't modify numeric parameters
*/
System.out.println("Testing tripleValue");
double percent = 10;
System.out.println("Before: percent="+percent);
tripleValue(percent);
System.out.println("After:percent="+percent);
/*
Test2: Methods can change the state of object parameters
*/
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());
/*
Test 3: Methods can't attach new objects to object parameters
*/
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)//doesn't work
{
x = 3*x;
}
public static void tripleSalary(Employee x)//works
{
x.raiseSalary(200);
System.out.println("End of methos: 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());
}
}