• 微信公众号：美女很有趣。 工作之余，放松一下，关注即送10G+美女照片！

2周前 (04-07) 6次浏览

# 前言

Java官方教程目录2021最新版

# Passing Information to a Method or a Constructor

``````public double computePayment(
double loanAmt,
double rate,
double futureValue,
int numPeriods) {
double interest = rate / 100.0;
double partial1 = Math.pow((1 + interest),
- numPeriods);
double denominator = (1 - partial1) / interest;
double answer = (-loanAmt / denominator)
- ((futureValue * partial1) / denominator);
}
``````

## Parameter Types

``````可以将任何数据类型用于方法或构造方法的参数(parameter)，包括基本数据类型和引用数据类型。下面示例中，方法接受数组作为参数(argument)，方法创建一个新的Ploygon对象，并且从Point对象的数组对其进行实例化(假定Point是一个表示x,y坐标的类)。
``````
``````public Polygon polygonFrom(Point[] corners) {
// method body goes here
}
``````

## Arbitrary Number of Arguments 可变参数

``````public Polygon polygonFrom(Point... corners) {
int numberOfSides = corners.length;
double squareOfSide1, lengthOfSide1;
squareOfSide1 = (corners[1].x - corners[0].x)
* (corners[1].x - corners[0].x)
+ (corners[1].y - corners[0].y)
* (corners[1].y - corners[0].y);
lengthOfSide1 = Math.sqrt(squareOfSide1);

// more method body code follows that creates and returns a
// polygon connecting the Points
}
``````

``````public PrintStream printf(String format, Object... args)
``````

``````System.out.printf("%s: %d, %s%n", name, idnum, address);
``````

``````System.out.printf("%s: %d, %s, %s, %s%n", name, idnum, address, phone, email);
``````

## Parameter Names

``````public class Circle {
public void setOrigin(int x, int y) {
...
}
}
``````

## Passing Primitive Data Type Arguments

``````public class PassPrimitiveByValue {

public static void main(String[] args) {

int x = 3;

// invoke passMethod() with
// x as argument
passMethod(x);

// print x to see if its
// value has changed
System.out.println("After invoking passMethod, x = " + x);

}

// change parameter in passMethod()
public static void passMethod(int p) {
p = 10;
}
}
``````

``````After invoking passMethod, x = 3
``````

## Passing Reference Data Type Arguments

``````public void moveCircle(Circle circle, int deltaX, int deltaY) {
// code to move origin of circle to x+deltaX, y+deltaY
circle.setX(circle.getX() + deltaX);
circle.setY(circle.getY() + deltaY);

// code to assign a new reference to circle
circle = new Circle(0, 0);
}
``````

``````moveCircle(myCircle, 23, 56)
``````