Java_09 current object this

Java_09 current object this

//this指当前对象,不能放在static中
//this即可以调用属性,也可以调用方法。

//通过this调用对象的方法
//要求输出m,n之间所有的质数(指只能被1和自身整除的,如2,3,5,7,11),例如,求400-500之间所有的质数。
package my;

public class _21当前对象_this_2 {
	
	//判断n是否为质数,true是质数,false不是质数
	public boolean isPrime(int n)
	{
		for(int i = 2; i < n; i++)
		{
			if(n % i == 0)
			{
				return false;
			}
		}
		return true;
	}
	
	
	//输出m,n之间所有的质数
	public void showPrimes(int m,int n)
	{
		for(int i = m;i <= n;i++)
		{
			if(this.isPrime(i))
			{
				System.out.println("质数: " + i);
			}
		}
	}

	
	//注意:this不能放在static中
	public static void main(String[] args)
	{	
		
		_21当前对象_this_2 s = new _21当前对象_this_2();
		s.showPrimes(400, 500);;		
	
	}
	
	
}	

Results of the
Results of the

Published 20 original articles · Like1 · Visits 369

Guess you like

Origin blog.csdn.net/songteng2012/article/details/105688105
Recommended