Using java ------ inner classes, and the array of the variable parameter

Inner classes

  Class category is defined inside the class. If a member of a thing is a thing, then you can use the definition of the inner class.
  Common inner classes are: members of the inner class, local inner classes, private inner classes, static inner classes, anonymous inner classes (the most important.)

  1. Internal class members
    inside the class definition at the position of the outer member of the class. Can be used as a member of a member of modifiers (public, private, default, protected , static, ( not including the final)), as a class can inherit class, you can implement the interface.
    Features:
    internal class can use all properties and methods directly outside the class, including private. Not directly defined in the class internal static content, in addition to static constants (static final int A = 5) , static content to be defined in the internal static class.

  2. Private inner class
    features: private inner class can use all of the content outside of class, including private. Private inner class can only be used in private inner class private content through the object outside the class, the other classes can not be used.

  3. Static inner classes
    (1) static inner class defines static content can define the content of members.
    (2) a non-static content outer class static inner class, by using the object to be outside the class.
    (3) You can use static inner class static content in an external class by class name, we need to use the member contents of a static inner classes through the object.
    (4) In the other class outside the class name. Static inner class name. Static content using a static internal class can be static content.
    new external class. static content category ()
    to create objects using a static inner class member static inner classes.

  4. Partial inner class
    defined in the method inside the class
    1 partial inner classes can not use the modifier modifying member (for example: public, static ... not including Final)
    2. In the method currently defined only partial internal class can be inside the object class use members of the inner class, you can use static content by category.
    3. The parameters of the method where the local internal class, after jdk1.8 default display definition required before 1.7

  5. Anonymous inner classes
    no internal class name, simplifying the subclass does not own role (function implementation).
    (1) run out of destruction, can not be used a second time.

new Smoking(){ //--匿名内部类的类体  是new后面的接口或者父类的实现类|子类的类体
			@Override
			public void smoke() {
				System.out.println("我抽的不是烟,抽的是寂寞..");
			}
			@Override
			public void cat() {
				System.out.println("吸猫...");
			}
		}.cat();	
interface Smoking{
	void smoke();
	void cat();
}

(2) address of the received anonymous inner classes referenced object - Interface polymorphism

Smoking s=new Smoking(){ //--匿名内部类的蕾体  是new后面的接口或者父类的实现类|子类的类体
			public void smoke() {
				System.out.println("我抽的不是烟,抽的是寂寞..");
			}

			@Override
			public void cat() {
				System.out.println("吸猫...");
			}
		};
		s.smoke();
		s.cat();

Parameter (3) Method

	useUSB(new USB(){
			@Override
			public void start() {
				System.out.println("开始使用移动硬盘");
			}
			@Override
			public void end() {
				System.out.println("结束使用移动硬盘");
			}
		});
		useUSB(new USB(){	
			@Override
			public void start() {
				System.out.println("开始使用键盘");
			}
			@Override
			public void end() {
				System.out.println("结束使用键盘");
			}
		});
	}
	static void useUSB(USB usb){
		usb.start();
		usb.end();
	}
}

(4) Lambda expressions
  in order to simplify anonymous inner classes.
  Syntax:
  () parameter list abstract method overridden
  -> arrow, arrows function, Lambda symbol
  method of rewriting the abstract methods {} body
provided: must be a function interface
function interfaces: only one interface abstract method is a function interface
@FunctionalInterface: mandatory testing whether a function interface

public class LambdaDemo {
	public static void main(String[] args) {
		/*Code c=new Code(){
			@Override
			public void code(int haha) {
				System.out.println("边敲代码边哄女朋友");
			}
		};*/
		//Lambda表达式写法1
		/*Code c=()->{
			System.out.println("边敲代码边哄女朋友");
		};*/
		//写法2:如果方法的语句体只有1句,前后的{}可以省略
//		Code c=()->System.out.println("边敲代码边哄女朋友");
		//写法3: 如果存在参数,参数的数据类型可以省略
//		Code c=(i,m)->System.out.println("边敲代码边哄女朋友"+i+"次");
		//写法4:如果存在参数,并且参数只有一个前后的()可以省略
		/*Code c= i ->{
			System.out.println("边敲代码边哄女朋友"+i+"次");
			return -1;
		};
		System.out.println(c.code(100));*/
		//写法5: 有返回值类型的方法,如果方法体语句只有1句,并且是return语句的时候,这时前后{},包括return都可以省略
		Code c= str -> str.length();
		System.out.println(c.code("因为"));
	}
}
//函数式接口
@FunctionalInterface
interface Code{
//	void code();
	int code(String a);
}

Array

Array [], an ordered set of data of the same type of
characteristics of the array

  1. Arrays are reference data types
  2. Array length is fixed, unchangeable length Once
  3. All data in the array type data consistent
  4. There is data in the order of the array (index: starts from 0)

Declare an array of ways

  1. Data Type [] array name;
    type of data: basic data types can be | reference data types
    the role of data types: defined data type array containing all of the data
  2. Data type array name []; - not recommended

Array initialization

  • Dynamic Initialization: after the assignment to create
    a data type [] = new Array name Data type [length];
    array of data has a default value if no assignment String-null, int-0, double-0.0, char- '', boolean- false
  • Static initialization: creating simultaneously assigned
    data type [] array name = new data type [] {value 1, value 2 and value 3 ...};
    data type [] array name = {value 1, value 2 and value 3 ...}; --recommend

Length of the array get

  • Array name .length

Array index for the last data

  • Array name .length-1

Traversing the array
1) for normal loop may be obtained may be modified, because the operation is the index (position).
2) enhanced for loop | for ... each, can only get each data can not be modified.
for (variable name Data type: array name | container name) {
  Variable -> means Algebraic Each data
}
common abnormality when using arrays

  1. NullPointerException null pointer exception
     object pointing to null, do something based on the object, may appear on the null pointer

  2. ArrayIndexOutOfBoundsException array index bounds exception
     index is negative
     index exceeds the maximum range

variable parameter

Variable parameters: the same data type 0 to a plurality of parameters

  • 1. Use a variable parameter representation ...
  • 2. implicitly compiler will automatically create an array of variable parameter, the variable parameter used by way of operation of the array
  • 3. The variable parameters must be placed in the last position in the parameter list
public class ParaDemo {
	public static void main(String[] args) {
		test(1,"","",1,false);
	}
	//至少接收一个参数,可以接收多个参数
	public static void test(int name,Object ...arr){
		System.out.println(name);
		for(Object n:arr){
			System.out.println(n);
		}
	}

Guess you like

Origin blog.csdn.net/qq_41899248/article/details/90920864