一威彰、引用傳遞
【案例-1】
引用傳遞-1.png
代碼
class Ref1{
int temp = 10;
}
public class Demo08 {
public static void main(String[] args) {
Ref1 r1 = new Ref1();
r1.temp = 20;
System.out.println(r1.temp);
tell(r1);
System.out.println(r1.temp);
}
public static void tell(Ref1 r2) {
r2.temp = 30;
}
}
結(jié)果:
20
30
【案例-2】
引用傳遞-2.png
代碼
public class Demo09 {
public static void main(String[] args) {
String str1 = "hello";
System.out.println(str1);
tell(str1);
System.out.println(str1);
}
public static void tell(String str2) {
str2 = "jike";
}
}
結(jié)果:
hello
hello
【案例-3】
引用傳遞-3.png
代碼
class Ref2{
String temp = "hello";
}
public class Demo10 {
public static void main(String[] args) {
Ref2 r1 = new Ref2();
r1.temp = "jike";
System.out.println(r1.temp);
tell(r1);
System.out.println(r1.temp);
}
public static void tell(Ref2 r2) {
r2.temp = "xueyuan";
}
}
結(jié)果:
jike
xueyuan
二、this關(guān)鍵字
使用
- 表示類中的屬性和調(diào)用方法掘宪。
- 調(diào)用本類中的構(gòu)造方法蛾扇。
- 表示當(dāng)前對象。
代碼一
class People{
private String name;
private int age;
public People(String name,int age){
this();//通過this調(diào)用構(gòu)造方法魏滚,必須方法構(gòu)造方法的首行
//通過this調(diào)用本類中的屬性
this.name = name;
this.age = age;
}
//無論什么情況下都要執(zhí)行此方法
//需要在其他構(gòu)造方法中調(diào)用此方法
public People(){
System.out.println("無參數(shù)構(gòu)造方法");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void tell() {
//通過this調(diào)用當(dāng)前類中的方法
System.out.println("姓名:"+this.getName()+" "+"年齡:"+this.getAge());
}
}
public class Demo11 {
public static void main(String[] args) {
People p = new People("張三", 30);
p.tell();
}
}
結(jié)果:
無參數(shù)構(gòu)造方法
姓名:張三 年齡:30
代碼二
package cn.sec.ch02;
class People1{
public void tell() {
System.out.println(this);
}
}
public class Demo12 {
public static void main(String[] args) {
People1 p1 = new People1();
System.out.println(p1);
p1.tell();
//通過People1實例化的對象和this得到的對象是一個對象
//通過this可以表示當(dāng)前對象
}
}
結(jié)果:
cn.sec.ch02.People1@7852e922
cn.sec.ch02.People1@7852e922
三镀首、static關(guān)鍵字
- 使用static聲明屬性:static聲明全局屬性
- 使用static聲明方法:直接通過類名調(diào)用
- 注意點:使用static方法的時候,只能訪問static聲明的屬性和方法鼠次,而非static聲明的屬性和方法是不能訪問的更哄。
示例一
代碼
class PersonA{
String name;
private static String country = "北京"; //聲明成靜態(tài)的,是大家公有的屬性
public PersonA(String name){
this.name = name;
}
public static String getCountry() {
return country;
}
public static void setCountry(String country) {
PersonA.country = country;
}
public void tell() {
System.out.println("姓名:"+name+" 出生地:"+country);
}
}
public class Demo13 {
public static void main(String[] args) {
// TODO Auto-generated method stub
PersonA.setCountry("上海");//靜態(tài)的方法通過類名調(diào)用 腥寇,可以在實例化之前調(diào)用
PersonA pA1 = new PersonA("張三");
// PersonA.country = "上海"; //靜態(tài)的屬性通過類名調(diào)用
pA1.tell();
PersonA pA2 = new PersonA("李四");
// pA2.country = "上海";
pA2.tell();
PersonA pA3 = new PersonA("王五");
// pA3.country = "上海";
pA3.tell();
}
}
結(jié)果:
姓名:張三 出生地:上海
姓名:李四 出生地:上海
姓名:王五 出生地:上海
示例二
代碼
public class Demo14 {
private static int i = 10;
public static void main(String[] args) {
// TODO Auto-generated method stub
//使用static方法的時候成翩,只能訪問static聲明的屬性和方法,而非static聲明的屬性和方法是不能訪問的花颗。
System.out.println(i);
tell();
}
public static void tell() {
}
}