1.java引用传递

1.1定义

  • 类似于C中的函数之间传递指针,直接通过指针操作内存
  • java示例
package com.hanqi.test;

class Ref1{
	int temp = 10;
}
public class test01 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		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;
	}

}
/////////////////////////////////////////////////////////////////////////////////////////////////////20
30
  • 上述功能用C实现
#include <stdio.h>

typedef struct Ref
{
    int temp = 10;
}Ref1;

void tell(Ref1 *r2)
{
    r2->temp = 30;
}
int main(void) 
{ 
    Ref1 r1;
    r1.temp = 20;
	printf("%d\n", r1.temp);
	tell(&r1);
	printf("%d\n", r1.temp);
	return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////20
30
  • 再来一个示例,发现score的值并没有变为60,因为函数没有返回值,故没改变
package com.hanqi.test;

public class test02 {

	public static void main(String[] args) {
		int score = 59;
		System.out.println(score);
		tell(score);
		System.out.println(score);
	}
	public static void tell(int score) {
		score = 100;
	}
}

////
59
59
  • 按照C的理解方式去理解会发现java的传参方式有点操蛋,同样是函数,当函数的参数为某个类的时候,可以发现,可以直接在函数中去操作实参内存,而函数参数为某个变量的时候却不能操作内存.即当函数参数为类的时候相当于参数为指针,为参数的时候与C相同,没有返回值就无法改变实参
2.this关键字

1.1this使用场景

  • 表示类中的属性与方法
package com.hanqi.test;

class People{
	private String name;
	private int age;
	public People(String name, int age) {
		this.name = name;	//this表示将参数name赋值给本类中的name变量
		this.age = age;
	}
	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() {
		System.out.println("姓名:"+this.getName()+"\n年龄:"+this.getAge());
	}
}
public class test03 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		People p = new People("hanqi", 21);
		p.tell();
	}

}
  • 调用本类中的构造方法,只有加了this()才可调用无参构造方法,且this()必须在最前面
//要求在下述场景中调用无参构造方法,此时可通过this调用
package com.hanqi.test;

class Person{
	int age;
	String name;
	public Person(int age, String name) {
         this();		//只有加了this()才可调用无参构造方法,且this()必须在最前面!!!!!!!!!!!!
		this.age = age;
		this.name = name;
		System.out.println("调用有参构造方法");
	}
	public Person() {
		System.out.println("调用无参构造方法");
	}
}
public class test04 {

	public static void main(String[] args) {
		Person p = new Person(21, "hanqi");
		
	}
}
///
调用无参构造方法
调用有参构造方法
  • 表示当前对象,可用来判断两个对象是否来自于同一个对象
package com.hanqi.test;

class People1{
	public void tell() {
		System.out.println(this);
	}
}
public class test05 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		People1 p = new People1();
		p.tell();
		System.out.println(p);
	}

}
///
com.hanqi.test.People1@15db9742
com.hanqi.test.People1@15db9742
3.static关键字

3.1特性

  • 用来修饰类的成员或者用于修饰成员变量的类称为类变量(静态变量)
package java封装;

class Data{
	int num_a;
	static int num_b;
}

public class static使用 {
	public static void main(String[] args) {
		Data.num_b = 2;		//正确,static变量可直接通过类名访问
		Data.num_a = 1;		//错误,普通变量不能直接通过类名访问
	}
}
  • 使用static声明属性,则该属性为全局属性
  • 使用static声明方法,则该方法可直接通过类名调用,例如main方法不用new就可以调用

  • 使用static方法的时候,只能访问static声明的属性和方法,而非static声明的属性和方法是不能访问的