JAVA language basis (an array of object-oriented)

JAVA language basis (c)

  1. Array
    1.1 one-dimensional array
    1.2 two-dimensional array
    1.3 Exercises
  2. Recursion
  3. Object Oriented
    3.1 Overview of Object Oriented
    3.2 Class and Object
    3.3 objects stored

1. Array

  • Concept: An array is a collection of the same data type (basic data types and the reference data type)
  • The array must be initialized (memory space is allocated in the memory array and the assignment of its elements) can be carried out using
  • Dynamic Initialization: Only specified length, is given by a system initialization value
  • Static initialization: the initialization value is given, the system is given by the length

1.1 one-dimensional array

  1. Definition Format:
    Format 1: (recommended)
    data type + [] array name +
    int [] a;
    Format 2:
    Data type array name + + []
    int A [];
  2. Define different initialization:
    dynamic initialization: Data Type [] = new Array name Data type [array size];
    Example: int [] arr = new int [6]; ( the number of elements in the array represents the length of the array)
    static initialization: Data type [] = new array name data type [] {element 1, element 2, ...};
    Example: int [] arr = new int [] {1,2,3,4,5,6,7}; // int [] arr = {1,2,3,4,5,6,7} ;

1.2 two-dimensional array

  1. Two-dimensional array is an array of array elements.
  2. Definition Format:
    Format 1:
    Data type [] [] = new variable name Data type [m] [n-];
    int [] [] ARR = new int [. 3] [2];
    // which defines a two-dimensional array there are three elements (array) element has two elements each
    format 2:
    data type [] [] = new variable name data type [m] [];
    int [] [] ARR = new int [. 3] [];
    ARR [0] = new new int [2];
    ARR [. 1] = new new int [3];
    ARR [2] = new new int [. 1];
    // define three arrays
    format 3:
    data type [] [] variable name = new type of data [] [] {{element ...}, {element ...}, {element ...} ...};
    data type [] [] variable name = {{element ...}, {element ...}, {element ...} };
    int [] [] ARR = {{l, 2,3}, {4,5,6}, {7,8,9}};

1.3 Exercises problem

  1. Array traversal
		package org.westos.practice;
		
		//一维数组遍历
		
		public class MyTest {
		    public static void main(String[] args) {
		        int arr[]={1,3,5,2,4,6};
		        for(int i=0;i<arr.length;i++)
		        {
		            System.out.println("arr["+i+"]"+"="+arr[i]);
		        }
		    }
		}  //循环输出数组中每一个元素的值
		package org.westos.practice;
		
		//二维数组遍历
		public class MyTest2 {
		    public static void main(String[] args) {
		        int [][]arr={{1,3,5},{2,4,6},{1,2,3}};
		        int i,j;
		        for(i=0;i<arr.length;i++){
		            for(j=0;j< arr[i].length;j++){
		                System.out.println("arr["+i+"]"+"["+j+"]"+"="+arr[i][j]);
		            }
		        }
		    }
		}

  1. Within the array elements flip
		package org.westos.practice;
		public class MyTest2 {
		    public static void main(String[] args) {
		        int []arr={1,2,3,4,5,6};
		        int i,j;
		        for(i=0;i<arr.length/2;i++){
		            int t;
		            t=arr[i];
		            arr[i]=arr[arr.length-i-1];
		            arr[arr.length-i-1]=t;
		        }
		        for(i=0;i<arr.length;i++){
		            System.out.println(arr[i]);
		        }
		    }
		}

  1. Triangle printing (the number of rows keyboard input)

    1
    1 1
    1 2 1
    1 3 3 1
    1 4 6 4 1
    1 5 10 10 5 1

			package org.westos.practice;
			
			import java.util.Scanner;
			
			public class MyTest2 {
			    public static void main(String[] args) {
			        Scanner sc = new Scanner(System.in);
			        int i,j;
			        System.out.println("Please input the rows: ");
			        int n=sc.nextInt();
			        int [][]arr=new int [n][n];    //定义有n个元素的n个数组    n*n
			        for(i=0;i<arr.length;i++){
			            arr[i][0]=1;
			            arr[i][i]=1;
			        }
			        for(i=2;i<arr.length;i++){
			            for(j=1;j<=i;j++)
			            arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
			        }
			        for(i=0;i<arr.length;i++){
			
			            for(j=0;j<=i;j++)
			            {
			                System.out.print(arr[i][j]+"\t");
			            }
			            System.out.println( );
			        }
			    }
			}


2. Recursion

  1. Method parameter to process itself

  2. Examples:
    Example 1:
    factorial

     ```
     package org.westos.practice;
     import java.util.Scanner;
    
     public class MyTest2 {
         public static void main(String[] args) {
             System.out.println("Please input the number:");
             Scanner sc = new Scanner(System.in);
             int n=sc.nextInt();
             System.out.println(digui(n));
         }
         public static int digui(int n){
             if(n==1){
                 return 1;
             }else{
                 return n*digui(n-1);
             }
         }
     }
    

    Example 2:
    Fibonacci number
    rabbit problem (Fibonacci number)
    after a pair of rabbits from the first 3 months after birth were born one pair of rabbits each month, bunnies grow up to the third month of each month She gave birth to one pair of rabbits, if the rabbit is not dead, asked the rabbit ten months of the second number is how much?
    Data shows that the rabbit is an object: 1, 1, 2, 3, 5, 8 ...

		package org.westos.practice;
		import java.util.Scanner;
		
		public class MyTest2 {
		    public static void main(String[] args) {
		        System.out.println("Please input the number of the month:");
		        Scanner sc = new Scanner(System.in);
		        int n=sc.nextInt();
		        System.out.println(Dl(n));
		    }
		    public static int  Dl(int n){
		        if(n==2||n==1){
		            return 1;
		        }else{
		            return Dl(n-1)+Dl(n-2);
		        }
		    }
		}

3. Object-oriented thinking

Overview 3.1 Object-oriented thinking

  1. Depending on the function, when the direct use of the same attributes and functions packaged together to achieve a good effect encapsulation process, eliminating the need for each step of the call, to improve efficiency. (Direct call encapsulated object)
  2. Object-oriented features:
    encapsulation
    inheritance
    polymorphism

3.2 Classes and Objects

  1. Class is a collection of features and attributes. It is the smallest unit in Java.
  2. Object is an object class, i.e. an individual having a similar function and class attributes.
    Categories: Animals
    Object: one bird dog
  3. The definition and use of classes:
    modified keywords (public final etc.) class class name
    to use class time, you need to instantiate a class, that is, to create objects of this class, the class to call the object properties and methods through the use of
    objects create:
    class name = new class name object name ();
    using the attribute object:
    object name attribute name;
    using the method of object:
    Object name method name (parameter);
    Example:
				public class Person(){       //创建Person类
					 String name;				//创建name属性
					 int age;						//创建age属性

				public void show(String name,int age){		//定义show 方法传进参数(name,age)
					System.out.println("name:"+name);			//打印姓名
					System.out.println("age:"+age);				//打印年龄
				}
				}
				
				public class Test(){
				public static void main(String []args){
					Person person1=new Person();			//定义一个Person类的对象person1
					Person person2=new Person();         	//定义一个Person类的对象person2
					
					person1.name="店小二";					//修改person1的name属性为店小二
					person1.age=18;									//修改person1的age属性为18
					person1.show(name,age);					//调用person1中方法show
					
					person2.name="掌柜的";					//修改person2的name属性为掌柜的
					person2.age=40;									//修改person2的age属性为40
					person2.show(name,age);					//调用person2中方法show
					
				}
				}
  1. In one .java files in parallel can define multiple classes, requirements are only applied to a public method of the main class
		 public class MyTest {
		    public static void main(String[] args) {
		    
		    }
		
		}
		
		
		 class Cat{
		
		
		}
		
		
			class Dog{
		
		
		}
		
			class Tiger{
		
		
		}
  1. When the shape parameter of a method of the type when a class object of this class to pass
  2. Basic data types, passed as a parameter, the parameter changes, does not affect the argument.
    Reference data types (classes array of interfaces, etc.), passed as a parameter, the parameter changes, will affect the argument.
  3. private key:
    is a privilege modifier
    can modify member variables and member methods
    were modified its members can only be accessed in this class
    the most common applications private:
    1. The private member variable with modifications
    2. Provide corresponding getXxx ( ) and setXxx () method
  4. this keyword:
    when the same our local variables and member variables when using this key distinction between local variables and member variables
    it represents an object of the current class. Who call this method, then this method on the inside who represents

3.3 storage object

  1. Member variable differences and local variables
    in different positions:
    member variables: define an outer class method of
    local variables: define or method in the definition of the method
    in different storage positions:
    member variables: stored in the heap memory
    local variables: stack stored in memory
    there is a time difference:
    member variables: with the creation of the object is created, with the destruction of the object and the destruction of
    local variables: with the method call is generated, call the complete method is deleted
    different initialization:
    member variables: you can not give initialization value, by the system default initialization
    of local variables: value must be initialized before you can use
    Notes: when the member variables and local variables of the same name, visit our principle variables, follow the principle of proximity
    local scope (the method declaration, method) if found using , if not, go look for membership, if found using
  2. Anonymous Object: anonymous object is no name of the object
    class for the Person when
    new Person (); anonymous objects
    a: calling method, called only once only when
    b: `anonymous objects can be passed as an actual argument
		class Person(){
			int age=10;
			public void show(){
				System.out.println("123");
		}
		}
		则主方法里写 new Person().age;
		            new Person().show;进行调用
  1. Variables are stored:
    Here Insert Picture Description
    Two-digit group of storage

Create Object

Guess you like

Origin blog.csdn.net/lqw1372712775/article/details/92246161