java引用传递 java this关键字 java static关键字

package com.jk.ref;

class Ref{
	int temp=10;
}
public class RefDemo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Ref re=new Ref();
		re.temp=20;
		System.out.println(re.temp);
		Ref1(re);
		System.out.println(re.temp);
	}
	public static void Ref1(Ref t){
		t.temp=30;
	}

}

162348_gFh0_2356966.png

package com.jk.ref;


public class RefDemo02 {


public static void main(String[] args) {

// TODO Auto-generated method stub

String st1="hello";

System.out.println(st1);

tell(st1);

System.out.println(st1);

}

public static void tell(String st2){

st2="world";

}


}

163701_tI4A_2356966.png

package com.jk.ref;

class Person{
	private String name;
	private int age;
	
	public Person(String name,int age){
		this();
		this.name=name;
		this.age=age;
	}
	public Person(){
		System.out.println("无参数构造方法");
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}
	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}
	public void tell(){
		System.out.println("name:"+getName()+"  "+"age:"+getAge());
	}
	
}
public class ThisDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Person per=new Person("zhangsnan",20);
		per.tell();
		
	}

}

171549_nZ7g_2356966.png

package com.jk.ref;

class Person{
	public void tell(){
		System.out.println(this);
	}
	
}
public class ThisDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Person per=new Person();
		System.out.println(per);
		per.tell();
	}

}

171851_f3UY_2356966.png

 java static关键字

1、使用static声明属性

            static声明全局属性

2、使用static声明方法

            直接通过类名调用

3、注意点

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

package com.jk.ref;

class People{
	String name;
	private static String country="中国";
	
	public People(String name){
		this.name=name;
	}
	public void tell(){
		System.out.println("name:"+name+"  "+"country:"+country);
	}
	/**
	 * @return the country
	 */
	public static String getCountry() {
		return country;
	}
	/**
	 * @param country the country to set
	 */
	public static void setCountry(String country) {
		People.country = country;
	}
	
}
public class StaticDemo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		People.setCountry("shanghai");
		People ps1=new People("zhangsan");
		//People.country="上海";
		
		ps1.tell();
		
		People ps2=new People("lisi");
	//	ps2.country="上海";
		ps2.tell();
		
		People ps3=new People("wangwu");
	//	ps3.country="上海";
		ps3.tell();
	}

}

181340_Fsnz_2356966.png

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

package com.jk.ref;

public class StaticDemo02 {
	private static int i=10;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(i);
		tell();
	}
	public static void tell(){
		System.out.println("hello");
	}

}


转载于:https://my.oschina.net/u/2356966/blog/408922

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值