JAVA 自学之路(类的封装)

版权声明:人类的最大弱点之一是自命不凡的幻想。 https://blog.csdn.net/claysystem/article/details/79703400
class Portscan
{
	private int port ;
	private String ip;
	
	void setPort(int port)
	{
		if(port < 65535 && port !=0)
		{	
			this.port = port ;
			System.out.println("Port:"+port);
		}
		else 
		{
			System.out.println("[X]Port值过大"+port);
		}
	}
	void setIp(String ip)
	{
			this.ip = ip;
			System.out.println("IP:"+ip);
	}
	
}



	public class StudayForExtends 
	{
		//主函数
		public static void main(String [] args)
		{
			Portscan s = new Portscan();
			s.setIp("192.168.1.1");
			s.setPort(60);
		}
	
	}

设置port和ip为私有成员,修改值通过函数的方式,这样可以控制输入进来的数据

猜你喜欢

转载自blog.csdn.net/claysystem/article/details/79703400