Java总结-构造和内部类

构造函数:
	格式:
	1,函数名和类名相同。
	2,不需要定义返回值类型。
	3,函数中不需要return语句。

	作用:给对象进行初始化。
	构造函数在类中可以有多个,是以重载的形式体现的。
	特点:当定义类时,类并未定义构造函数,那么系统会加上一个默认的看参数构造函数。
		class Demo
		{
			//Demo(){}
		}
		这个默认的构造函数有什么修饰符呢?
		如果类有public修饰,默认构造函数也有public修饰。
		如果类没有public修饰,默认构造函数也没有public修饰。
		当类中定义了自定义构造后,默认的就没有了。

	什么时候写构造函数呢?
	在分析事物时,该事物的实体,是否在出现时就具有一些默认的属性值或者行为。
	如果有,就用构造函数来完成。

	------------------------------
	this关键字:
	当成员变量和局部变量同名的时候,为了进行区分。可以使用this来完成。
	class Person
	{
		private String name;
		private int age;
		Person()
		{
		
		}
		Person(String name,int age)
		{
			this.name = name;
			this.age = age;
		}
		public void setName(String name)
		{
			this.name = name;
		}
		public int getAge()
		{
			return age;
		}
		public boolean compare(Person p)
		{
			/*
			if(p.age==this.age)
				return true;
			else
				return false;
			*/

			return p.age==this.age;
		}
	}

	main()
	{
		Person p = new Person("lisi",20);
		p.setName("haha");
		Person p1 = new Person("wnagwu",21);
		p1.setName("hehe");

		boolean b = p.compare(p1);
		/*
		if(p.getAge()==p1.getAge())
			System.out.println("同龄");
		else
			System.out.println("no同龄");
		*/

	}
	this:代表的是就是一个本类对象,哪个对象调用了this所在的函数,那么this就代表哪个对象。

	什么时候使用this关键字呢?
	定义功能时,该功能又使用到了调用该功能对象。那么这是就用this来表示这个对象。

	class ThisDemo
	{
		private int num;
		ThisDemo(int num)
		{
			this.num = num;
		}

		ThisDemo()
		{
			//this.num = 100;
			this(100);
		}
		void show()
		{
			System.out.println("num="+this.num);
		}
		void method()
		{
			this.show();
		}
	}
	
	this的第二种用法:
	通过this语句,可以在构造函数间进行互相调用。
	注意:this语句,必须要写在构造函数的第一行。因为初始化动作要先完成。
		
	this在本类中可以用来调用成员变量,也可以调用成员函数,还可以调用构造函数。
	---------------------------------------


	static关键字:
	1,随着类的加载而加载,随着类的消失而消失,static的生命周期最长。
	2,优先于对象存在。
	3,static成员被所有对象所共享。
	4,static成员可以直接被类名所调用。

	静态的使用注意事项:
	1,静态方法只能访问静态成员。非静态方法可以访问静态也可以访问非静态。
	2,静态方法中不可以定义this和super语句。
	3,主函数是静态的。

	class StaticDemo
	{
		int age;
		static String country = "中国";

		public /*static*/ void show()
		{
			System.out.println(county+","+this.age);
		}
		public static void main(String[] args)
		{
			new StaticDemo().show();
		}
	}

	public static void main(String[] args)
	public :最大权限。
	static :该类不需要建立对象。就可以使用这个main方法。
	void : 该函数没有具体返回值。
	main : 固定名称。jvm认识。
	String[] args:主函数的参数。字符串数据类型的参数。
	
	那么jvm在调用主函数的时候到底传递的是什么实际参数呢?
	String[] args = new String[0];


	练习:
	class Demo
	{
		static int count = 0;//没有static前,在每一个对象中都有一个自己的count。所以输出结果是1。
					//有了static修饰,每一个对象共享同一个count。所以结果是创建的对象数。
		void Demo(){}//这是可以存在的。是一个一般函数,但是函数名不符合规范。

		Demo()
		{
			count++;
		}
	}
	main()
	{
		new Demo();
		new Demo();
		new Demo();
		new Demo();
		new Demo();
		new Demo();
		new Demo();
		new Demo();
		new Demo();
		new Demo();
		System.out.println(Demo.count);
	}



	----------------------------------------

	static代码块:随着类的加载而执行,而且只执行一次。
			优先于主函数执行。
			作用:对类进行初始化。

	class Demo
	{
		private static int num =10;
		static
		{
			for(int x=0; x<10; x++)
			{
				num++;
			}
		}

		public static void show()
		{
			System.out.println("num="+num);
		}
	}
	

	class Test
	{
		static
		{
			System.out.println("a");
		}

		Test()
		{
			System.out.println("b");
		}

		Test(int x)
		{
			System.out.println("c");
		}

		{
			System.out.println("d");
		}
	}

	class TestMain
	{
		public static void main(String[] args)
		{
			new Test();
			new Test(3);
			//adbdc
		}
	}
	

	----------------------------------


	内部类:
	在类中定义的类,称之为内部类。
	内部类可以定义在外部类的成员位置上,也可以定义在局部位置上。

	内部类的访问特点:
	内部类可以直接访问外部类中的成员,包括私有的。那是因为内部类都持有一个外部类的引用。外部类名.this

	外部类要访问内部类需要建立内部类的对象。

	class Outer
	{
		private int x = 3;
		class Inner
		{
			void show()
			{
				System.out.println(Outer.this.x);
			}
		}

		void function()
		{
			Inner in = new Inner();
			in.show();
		}
	}
	

	main()
	{
		//直接建立内部类对象,并调用内部类成员。
		//但是这种情况较为少见,因为内部类通常都会封装。外部不可以见。
		Outer.Inner in = new Outer().new Inner();
		in.show();

		//当内部类是static时。
		new Outer.Inner().show();

		//当内部类是static,而且show方式也是静态时,
		Outer.Inner.show();


	}
	当内部类定义在成员位置上,可以被成员修饰符所修饰。
	private,static。
	什么时候内部类被私有呢?内部类不需要对外暴露,在外部类中隐藏时,需要private
	什么时候内部类被静态呢?当要在内部类中定义静态成员时,内部类必须被static修饰。

	注意:内部类变成静态后,就会出现访问局限性,只能访问外部类中的静态成员。

	
	class Demo
	{
		class Inner{}

		void method()
		{
			new Inner();
		}
		public static void main(String[] args)
		{
			new Inner();//不可以。主函数只能访问静态成员。
					//如果非要访问,需要将Inner用static修饰。
		}
	}


	当内部类在局部位置时,内部类不可以被成员修饰符修饰。
	内部类不可以直接访问局部的变量。只能访问被final修饰的局部变量。


	什么时候定义内部类?
	描述事物时,该事物内部还有具体事物。可以将该事物通过内部类来描述。

	----------------------------------------





你可能感兴趣的:(java)