Java Classes and Objects (b)

1. The code block

  • Common code block
  • Building blocks
  • Static block
  • Synchronization code blocks (associated with the thread)

Common code block 1.1

  • In the definition process of code block
  • Code variables, only valid in the current block
  • Generally longer codes, in order to prevent the variable name is not the same name, the use of the block
public class Test{
	public static void main(String[] args){
		{
			int x = 10;
			System.out.println(x);	
		}
		System.out.println(x);         //编译错误,找不到符号x
	}
}

Building blocks 1.2

  • Defined in the class block of code (without modifier)
  • Building block structure in preference to execute a method, an object for each new new, executed once building blocks
class Person{
	{
		System.out.println("这是一个构造块");
	}
	public Person(){
		System.out.println("这是一个构造方法");
	}
}
public class Test{
	public static void main(String[] args){
		new Person();         //每new一个对象,执行构造块-->构造方法
		System.out.println("--------");
		new Person();
	}
}
/*
这是一个构造块
这是一个构造方法
----------
这是一个构造块
这是一个构造方法
*/

1.3 static code block

  • Using static definition of the code block
  • Classification: 1 in the main category; non main class 2

1.3.1 static block of code in the non-primary type

  • Static block to execute -> building block -> Constructor
  • Instantiate an object for each new, executed only once static block
  • Static block effect: to initialize static properties
class Person{
	static{
		System.out.println("这是一个静态块");
	}
	{
		System.out.println("这是一个构造块");
	}
	public Person(){
		System.out.println("这是一个构造方法");
	}
}
public class Test3{
	public static void main(String[] args){
		System.out.println("------start------");
		new Person();
		System.out.println("-----------------");
		new Person();
		System.out.println("------end--------");
	}
}
/*
-------start--------
这是一个静态块
这是一个构造块
这是一个构造方法
--------------------
这是一个构造块
这是一个构造方法
-------end----------
*/

1.3.2 static code block main class

  • Static method takes precedence in the main method
public class Test{
	static{
		System.out.println("这是一个静态块");
	}
	{
		System.out.println("这是一个构造块");
	}
	public Test(){
		System.out.println("这是一个构造方法");
	}
	public static void main(String[] args){
		System.out.println("-------start------");
		new Test();
		System.out.println("------------------");
		new Test();
		System.out.println("------end---------");
	}
}
/*
这是一个静态块
------start------
这是一个构造块
这是一个构造方法
-----------------
这是一个构造块
这是一个构造方法
------end--------
*/

2. The use as defined within the class

  • Definition: Internal nested classes that have other types of structures operating inside a class
  • Inner class action:
    • 1. The data access method within the class where the class definition of the scope, including the private modifier private data
    • 2. The inner class can be hidden to other classes in the same package
    • 3. java class can implement internal defect single inheritance
    • 4. When we want to define a callback function, but do not want to write a lot of code we can choose to remain anonymous inner classes to implement
      , for example: the use of inner classes to achieve "multiple inheritance"
class A{
	private String name = "A的私有域";
	public String getName(){
		return this.name;
	}
}
class B{
	private int age = 20;
	public int getAge(){
		return this.age;
	}
}
class Outer{       //Outer利用内部类继承了两个类A与B
	private class InnerClassA extends A{
		public String name(){
			return super.getName();
		}
	}
	 private class InnerClassB extends B{
  		public int age(){
  			 return super.getAge();
  		}
 	}
 	public String name(){
		return new InnerClassA().name();
	}
	public int age(){
		return new InnerClassB().age();
	}
}
public class Test{
	public static void main(String[] args){
		Outer out = new Outer();
		System.out.println(out.name());
		System.out.println(out.age());
	}
}
/*
A的私有域
20
*/

2.1 relations with the outside inner class class

  • For non-static inner class, create inner class instance object depend on outside class, in the absence of external class instance can not be created inside the class
  • Internal class can directly access the external element class (including private domain), but outside the class can not directly access the internal earth element
  • External indirectly access the internal class reference-earth element by an internal class

Example: INNER direct access to the external earth element

class Outer{
	private String msg ="hello,world";
	class Inner{
		private String msg = "hello,xi'an";
		public void print(){
			System.out.println(Outer.this.msg);    //如果写成this.msg/msg 访问的是本类属性
		}
	}
}
public class Test{
	public static void main(String[] args){
		Outer.Inner in = new Outer().new Inner();     //在外部类的外部实例化一个内部类
		in.print();
	}
}
// hello,world

External Internal External created class class : 外部类.内部类 内部对象 = new 外部类().new 内部类();
Example: class external reference (target address) through indirect access to the internal earth element-based internal

class Outer{
 	private String msg ="hello,world";
 	class Inner{
  		private String msg = "hello,xi'an";
  		public void print(){
   			System.out.println(this.msg);    //如果写成this.msg/msg 访问的是本类属性
  		}
 	}
 	public void fun(){
		Innner in = new Inner();     //在外部类内部实例化内部类对象
		in.print();     //内部类对象调用实例化方法
	}
}
public class Test{
 public static void main(String[] args){
  	Outer out = new Outer();
  	out.fun(); 
 	}
}
//hello,xi'an

Create inner classes inside the outer class (the same as ordinary objects created) :内部类 内部类对象 = new 内部类();

2.2 Internal category classification

classification:

  • Members of the inner class
  • Static inner classes
  • Method inner class
  • Anonymous inner classes

2.2.1 members of the inner class

1. not exist any static inner class member variables and methods
2. members of the inner class is attached to the periphery of the class, so only create a class to be able to create an internal peripheral class

2.2.2 static inner classes

  • Definition: The modified using static inner class as a static class
  • The static keyword can be modified fields, methods, code blocks

1. Create a static inner classes do not need to rely on peripheral class, you can create a direct
2. static inner classes can not use any non-static member variables and methods of the enclosing class. The inner class can
create an external class syntax: 外部类.内部类 内部类对象 = new 外部类.内部类();
For example: the use of static create static inner classes

class Outer{
	private static String msg = "hello,world";

	static class Inner{
		public void print(){
			System.out.println(Outer.msg);
		}
	}
}
public class Test{
	public static void main(String[] args){
		Outer.Inner in = new Outer.Inner();
		in.print();
	}
}
//hello,world

2.2.3 Method inner class

  • Definition: Method (partial) inner class defined in the method outside the class
  • Methods inside classes and members of inner classes are basically the same, just different scopes. Method Internal class used internally by the method, the method will fail

1. Method inner classes are not allowed access modifiers public, Private, protected
2. local inner class outside completely hidden , except to create a method of this class can access, other places do not allow access
3. Local inner class you want to use form parameter, the parameter must be declared final (JDK8 final parameter becomes implicit declaration)
example: use internal class

class Outer{
	private int num = 3;
	public int getNum(){
		return this.num;
	}
	public void display(int test){
		class Inner{
			private void fun(){
				num++;
				System.out.println(num);
				System.out.println(test);
			}
		}
		new Inner().fun();
	}
}
public class Test{
	public static void main(String[] args){
		Outer out = new Outer();
		out.display();
		System.out.println(out.getNum);
	}
}
/*
4
20
4
*/

2.2.4 anonymous inner classes

  • Definition: anonymous inner class is a class inside the method no name

1. anonymous inner class does not have access modifier
2. anonymous inner class must inherit an abstract class or interface
does not exist any static members and methods 3. anonymous inner class
4. no anonymous inner class constructor, because it does not have the class name
5. with the same local inner classes, anonymous inner classes can also refer to a method parameter, parameter must be declared as final

Example: Use anonymous inner classes

interface MyInterface{
	void test();
}
class Outer{
	private int num;
	public void display(int test){
		new MyInterface(){
		 	@override 
			public void test(){
				System.out.println(test);
			}
		}.test();
	}
}
public class Test{
 	public static void main(String[] args){
		Outer out = new Outer();
		out.display(20);
	} 
}
//20

Guess you like

Origin blog.csdn.net/mi_zhi_lu/article/details/88144023