2017.12.1T19_B2_2
package com.xdf.test;
public class Student {
/**
* 全局变量 在任何方法中都可以访问和使用
*/
int age = 50;
String name = "小黑";
public void showInfo() {
/**
* 局部变量名如果和成员变量冲突,默认使用局部变量
*/
int age = 10;
String name = "小白";
System.out.println(age);
System.out.println(name);
}
/**
* 让用户传递过来一个age和name
*
* (int age, String name) 参数列表 (参数个数,参数的类型,参数的顺序)
* 01.int age , String name 都称之为 形参
* 02.必须有数据类型
* 03.实参的参数列表和形参必须一致
*
*/
public void showInfo(int age, String name) {
System.out.println(age);
System.out.println(name);
}
}
package com.xdf.test;
public class StudentTest {
public static void main(String[] args) {
Student stu = new Student(); // 实例化对象
stu.showInfo();
System.out.println("**********************************");
/**
* 500 大白" 实际传递的参数 ==》实参
*/
stu.showInfo(500, "大白");
}
}
package com.xdf.parameter;
public class ExamDemo {
public static void main(String[] args) {
int num = 5;
int result = changeNum(num);
System.out.println(result); // 55
int[] nums = { 10, 20, 30 };
nums[0] = changeNum(result);
System.out.println(nums[0]); // 105
changeNumbers(nums);
System.out.println(nums[0]); // 5000
}
private static void changeNumbers(int[] nums) {
nums[0] = 5000;
}
private static int changeNum(int num) {
return 50 + num;
}
}
package com.xdf.parameter;
public class ExamDemo2 {
public static void main(String[] args) {
String name = "小黑";
String[] names = { "小白", "小蓝", "小粉" };
names[0] = changeName(name);
System.out.println(names[0]);
changeNames(names);
System.out.println(names[1]);
}
private static void changeNames(String[] names) {
names[1] = "小懒";
}
private static String changeName(String name) {
return "小黑521";
}
}
package com.xdf.parameter;
public class ExamDemo3 {
public void a() {
System.out.println("a");
b();
}
public void b() {
System.out.println("b");
c();
}
public void c() {
System.out.println("c");
// a(); StackOverflowError
}
public static void main(String[] args) {
ExamDemo3 demo = new ExamDemo3();
demo.a();
}
}
package com.xdf.parameter;
/**
* 参数的传递:
* 01.值传递 ===》基本数据类型+String 作为参数传递 传递的都是值
* 02.引用传递 ===》引用数据类型 作为参数传递 传递的内存地址
*/
public class ParameterDemo {
public static void main(String[] args) {
int num = 5; // 局部变量
changeNum(num);
System.out.println(num);
System.out.println("**********************");
String name = "哈哈";// 局部变量
changeName(name);
System.out.println(name);
}
private static void changeName(String name) {
name = "嘿嘿";// 局部变量
}
private static void changeNum(int num) {
num = 5000; // 局部变量
}
}
package com.xdf.parameter;
/**
* 02.引用传递 ===》引用数据类型 作为参数传递 传递的内存地址
*/
public class ParameterDemo2 {
public static void main(String[] args) {
int[] nums = { 10, 20, 30 }; // 引用传递
changeNums(nums);
System.out.println(nums[0]);
}
private static void changeNums(int[] numbers) {
numbers[0] = 888;
}
}
package com.xdf.parameter;
public class Student {
int age;
String name;
public static void main(String[] args) {
Student stu = new Student();
stu.age = 50;
stu.name = "小黑";
changeStudent(stu);
System.out.println(stu.age);
System.out.println(stu.name);
}
private static void changeStudent(Student stu) {
stu.age = 888;
stu.name = "小白";
}
}