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;
	}

}

package com.jk.ref;java


public class RefDemo02 {this


public static void main(String[] args) {spa

// TODO Auto-generated method stubcode

String st1="hello";get

System.out.println(st1);it

tell(st1);class

System.out.println(st1);方法

}im

public static void tell(String st2){static

st2="world";

}


}

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();
		
	}

}

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();
	}

}

 java static關鍵字

一、使用static聲明屬性

            static聲明全局屬性

二、使用static聲明方法

            直接經過類名調用

三、注意點

        使用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();
	}

}

使用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");
	}

}
相關文章
相關標籤/搜索