知识点1:方法的声明
* 类中方法的声明测试
* 1. 之前用过的一些方法:
* Scanner类的nextInt() \ next()
* Math类的random() \ sqrt(double d)
* Arrays类的equals(int[] arr1,int[] arr2) \ toString(int[] arr) \ sort(int[] arr)
* \ binarySearch(int[] arr,int value)
*
* 2. 类中方法声明的格式:
* 权限修饰符 返回值类型 方法名(形参列表){
* 方法体
* }
* 说明:关于方法中的特殊关键字:static\final\abstract\...暂时先不考虑。
*
* 3. 方法声明的详细说明:
* 3.1 权限修饰符:指名当前方法被调用权限的大小。
* 涉及到的权限修饰符有:private 、 缺省 、 protected 、 public。 详细细节见《封装性》
* 目前,大家在声明方法时,默认使用public即可。
* 3.2 返回值类型 :
* > 可以分为:有具体的返回值类型 vs 没有返回值类型:void
* > 有具体的返回值类型,可以使用任何的变量类型。比如:基本数据类型、引用数据类型
* > 有具体的返回值类型的方法,在调用方法结束后,一定要返回满足要求的返回值类型的变量或常量
* 补充:return的使用:
* 在有具体返回值类型的方法中,一定需要使用“return + 变量/常量”的方法给方法返回指定的数据。
* 如果方法没有返回值类型,也可以使用"return;"结构,表示结束当前方法的执行。
*
* 3.3 方法名:属于标识符,命名时满足标识符命名的规则和规范。“见名知意”
* 3.4 形参列表:
* 格式:参数类型1 参数名1,参数类型2,参数名2,...
* 方法在声明时,不要给形参列表的变量赋值。当我们调用方法时,才给形参列表赋值:实参。
* 3.5 方法体:执行方法时,主要操作的逻辑。
* 4. 如何确定定义一个方法时,要不要声明返回值类型?要不要声明形参?
* ① 看题目要求。
* ② 具体问题具体分析。
代码演示
public class PersonTest {
public static void main(String
[] args
) {
Person p
= new Person();
p
.printNumber();
int hour
= 8;
p
.sleep(hour
);
System
.out
.println("##############");
p
.eat();
}
}
class Person{
String name
;
int age
;
Account acct
;
public void printNumber(){
for(int i
= 0;i
< 100;i
++){
if(i
== 40){
return;
}
System
.out
.println(i
);
}
System
.out
.println("hello!");
}
public Account
getAccount(){
boolean flag
= true;
if(flag
){
return null
;
}
return new Account();
}
public int getValue(){
return 1;
}
public void sleep(int hour
){
System
.out
.println("昨天睡了" + hour
+ "小时");
}
public void eat(){
System
.out
.println("人需要吃饭,补充营养");
System
.out
.println("name = " + name
);
sleep(10);
}
}
class Account{
}
补充说明
* 5. > 可以在当前类的方法中使用当前类定义的属性或其他的方法
* > 方法内不能定义新的方法。
课后练习
public class CircleTest {
public static void main(String
[] args
) {
Circle c1
= new Circle();
c1
.radius
= 1.2;
c1
.findArea();
}
}
class Circle{
double radius
;
public void findArea(){
System
.out
.println(Math
.PI
* radius
* radius
);
}
}
知识点2:方法的重载
概念的理解
* 1. 什么是方法的重载?同一个类中,相同方法名,不同参数列表的方法之间构成重载。
*
* “两同一不同”:同一个类,相同方法名;参数个数 或 参数类型不同
*
* 2. 方法的重载与权限修饰符、返回值类型、形参名没有关系!
代码演示
public class OverLoadTest {
public static void main(String
[] args
) {
OverLoadTest test
= new OverLoadTest();
test
.show();
test
.show(1,2);
}
public void show(){
System
.out
.println("hello");
}
public void show(int a
){
System
.out
.println("萨瓦迪卡");
}
void show(String info
){
System
.out
.println(info
);
}
public int show(int i
,int j
){
return i
+ j
;
}
private void show(int i
,String info
){
}
public void show(String info
,int i
){
}
}
重点
* 3. 我们在调用类中的方法时,是如何确定调用的是某一个确定的方法呢?
* 通过方法名确定 ---> 进一步通过形参列表确定
知识点3:可变形参的方法
public class ArgsTest {
public static void main(String
[] args
) {
ArgsTest test
= new ArgsTest();
test
.show("abc","123","xyz");
test
.show(new String[]{"abc","def"});
}
public void show(){
System
.out
.println("show()");
}
public void show(String s
){
System
.out
.println("show(String s)");
}
public void show(int i
){
System
.out
.println("show(int i)");
}
public void show(String
... info
){
System
.out
.println("show(String ... info)");
for(int i
= 0;i
< info
.length
;i
++){
System
.out
.println(info
[i
]);
}
}
}
知识点4:方法参数的值传递机制(重难点)
1. 变量的传递机制:
* 1. 如果传递的是基本数据类型的变量,则将变量本身保存的数据值传递过去
* 2. 如果传递的是引用数据类型的变量,则将变量本身保存的地址值传递过去
public class VariableTest {
public static void main(String
[] args
) {
int m
= 10;
int n
= m
;
n
= 20;
System
.out
.println("m = " + m
);
Order o1
= new Order();
o1
.num
= 10;
Order o2
= o1
;
o2
.num
= 20;
System
.out
.println("o1.num = " + o1
.num
);
int[] array1
= new int[]{2,3,5,7,11};
int[] array2
= array1
;
array2
[0] = 0;
System
.out
.println(array1
[0]);
}
}
class Order{
int num
;
}
2. 方法形参的值传递机制
* 1. 形参:方法声明时,小括号内的参数。
* 实参:方法调用时,实际传递给形参的数据
*
* 2. 如果方法的形参是基本数据类型的变量,则将实参保存的数据值传递给形参变量。
* 如果方法的形参是引用数据类型的变量,则将实参保存的地址值传递给形参变量。
基本数据类型
public class ValueTransferTest {
public static void main(String
[] args
) {
int m
= 10;
int n
= 20;
System
.out
.println("m = " + m
+ ", n = " + n
);
ValueTransferTest test
= new ValueTransferTest();
test
.swap(m
,n
);
System
.out
.println("m = " + m
+ ", n = " + n
);
}
public void swap(int m
,int n
){
int temp
= m
;
m
= n
;
n
= temp
;
}
}
对应图示:
引用数据类型
public class ValueTransferTest1 {
public static void main(String
[] args
) {
Data data
= new Data();
data
.m
= 10;
data
.n
= 20;
System
.out
.println("m = " + data
.m
+", n = " + data
.n
);
ValueTransferTest1 test
= new ValueTransferTest1();
test
.swap(data
);
System
.out
.println("m = " + data
.m
+", n = " + data
.n
);
System
.out
.println(data
);
}
public void swap(Data data1
){
int temp
= data1
.m
;
data1
.m
= data1
.n
;
data1
.n
= temp
;
}
}
class Data{
int m
;
int n
;
}
class User{
}
对应图示:
3. 练习1
public class ValueTransferTest2 {
public static void main(String
[] args
) {
int[] arr
= new int[]{34, 4, 2, 6, 6, 4, 7, -87, 0, 55, 98};
System
.out
.println(Arrays
.toString(arr
));
ValueTransferTest2 test
= new ValueTransferTest2();
test
.sort(arr
, "ascend");
System
.out
.println(Arrays
.toString(arr
));
}
public void sort(int[] arr
, String sortMethod
) {
if ("ascend".equals(sortMethod
)) {
for (int i
= 0; i
< arr
.length
- 1; i
++) {
for (int j
= 0; j
< arr
.length
- 1 - i
; j
++) {
if (arr
[j
] > arr
[j
+ 1]) {
swap(arr
,j
,j
+1);
}
}
}
} else if ("descend".equals(sortMethod
)) {
for (int i
= 0; i
< arr
.length
- 1; i
++) {
for (int j
= 0; j
< arr
.length
- 1 - i
; j
++) {
if (arr
[j
] < arr
[j
+ 1]) {
swap(arr
,j
,j
+1);
}
}
}
} else {
System
.out
.println("排序方式错误!");
}
}
public void swap(int[] arr
, int i
, int j
) {
int temp
= arr
[i
];
arr
[i
] = arr
[j
];
arr
[j
] = temp
;
}
}
4.练习2:
public class TransferTest3 {
public static void main(String args
[]) {
TransferTest3 test
= new TransferTest3();
test
.first();
}
public void first() {
int i
= 5;
Value v
= new Value();
v
.i
= 25;
second(v
, i
);
System
.out
.println(v
.i
);
}
public void second(Value v
, int i
) {
i
= 0;
v
.i
= 20;
Value val
= new Value();
v
= val
;
System
.out
.println(v
.i
+ " " + i
);
}
}
class Value {
int i
= 15;
}
对应的图示:
补充内容:
NullPointerException的理解
public class NullPointerExceptionTest {
public static void main(String
[] args
) {
User u1
= new User();
System
.out
.println(u1
.name
);
}
}
class User{
String name
;
int age
;
public void show(){
String str
= null
;
System
.out
.println("我是一个用户");
}
}
对象数组的理解
public class StudentArrayTest {
public static void main(String
[] args
) {
int[] arr
= new int[10];
String
[] arr1
= new String[10];
Student
[] stus
= new Student[20];
for (int i
= 0; i
< stus
.length
; i
++) {
stus
[i
] = new Student();
stus
[i
].number
= i
+ 1;
stus
[i
].state
= (int)(Math
.random() * 6 + 1);
stus
[i
].score
= (int)(Math
.random() * 101);
}
for(int i
= 0;i
< stus
.length
- 1;i
++){
for(int j
= 0;j
< stus
.length
- 1 - i
;j
++){
if(stus
[j
].score
> stus
[j
+ 1].score
){
Student temp
= stus
[j
];
stus
[j
] = stus
[j
+ 1];
stus
[j
+ 1] = temp
;
}
}
}
for (int i
= 0; i
< stus
.length
; i
++) {
stus
[i
].info();
}
}
}
class Student{
int number
;
int state
;
int score
;
public void info(){
System
.out
.println("number : " + number
+ ", state : " + state
+ ", score : " + score
);
}
}
对应的内存解析