Java learning history (I)

1、 Classes and objects

1. Create class

Create a student class and create member variables and methods

public class student{
  String name;
  int age;
  public void study(参数列表){
    …………
  }
}

2. Use of objects

Note: everything from new is in the pile

类名 对象名 = new 类名();
例:student stu = new student();

3. Accessing members using objects

访问成员方法:   对象.成员方法();
例: stu.study();
访问成员变量:   对象.成员变量;
例: stu.name;

2、 Encapsulation

1. Private keyword

private 数据类型 变量名;

2. How to access member variables

提供getXxx、setXxx方法
例:
  public class student{
    private String name;
    private int age;
    
  public void setName(String n){
    name = n;
  }
  public String getName(){
    return name;
  }
  public void setAge(int a){
    age = a;
  }
  public int getName(){
    return age;
  }
}

3. Package optimization (I) this keyword

Note: This represents the reference (address value) of the current object of the class; that is, whoever is calling represents who

public class student{
  private String name;
  private int age;
  
  public void setName(String name){
  this.name = name;
}
  public String getName(){
    return name;
  }
  public void setAge(int age){
    this.age = age;
  }
  public int age(){
    return age;
  }
}

4. Construction method of package optimization (2)

Note: when an object is initialized, the constructor is used to initialize the object and assign an initial value to the member variable of the object.

The class name of the constructor is the same as that of the class where it is located, and there is no return value type, and void is not required.

public class student{
  private String name;
  private int age;
  
public student(){}
public student(String name,int age){
  this.name = name;
  this.age = age;
} 
}

5. Construction method considerations and standard codes

matters needing attention:

Standard code - JavaBean:

It is a standard specification for classes written in Java language. Classes conforming to JavaBean must be concrete and public, have parameterless construction methods, and provide getxxx and setXXX methods for operating member variables.

public class student{
  //成员变量
  private String name;
  private int age;
  //构造方法
  public student(){}
  public student(String name,int age){
    this.name = name;
    this.age = age;
  }
  //成员方法
  public void setName(String name){
    this.name = name;
  }
  public String getName(){
    return name;
  }
  public void setAge(int age){
    this.age = age;
  }
  public int getAge(){
    return age;
  }
}

3、 Scanner class

1. Concrete implementation

//导包
import java.util.Scanner;
public class student{
  public static void main(String[] args){
    //创建一个键盘录入数据的对象
    Scanner sc = new Scanner(system.in);
    //system.in 表示通过键盘录入数据
    
    int a = sc.nextInt();
    //表示通过对象来调用成员方法
    //nextInt 表示将下一条标记为int
    String b = sc.next();
    //查找并返回扫描仪的下一个完整令牌
  }
}

2. Anonymous object

new Scanner(system.in).nextInt();

matters needing attention:

Create an anonymous object and call method directly with theout variable name

Once the method is called twice, two objects are created, resulting in a waste of space

Anonymous objects can also be used as a list of parameters and return values of methods

4、 Random class

1. Concrete implementation

//导包
import java.util.Random;
public class student{
  public static void main(String[] args){
    Random r = new Random();
    //产生一百以内的随机数
    for(int i=0;i<100;i++){
      int num = r.nextInt();
      //每调用一次nextInt方法,就会出现一个新的随机数
      System.out.println("随机数:"+ num );
    }
  }
} 

5、 ArrayList class

1. Concrete implementation

//导包
import java.util.ArrayList;
public class student(){
  public static void main(String[] args){
    //创建一个数组
    ArrayList<String> list = new ArrayList<>();\
    //从JDK7之后,右侧泛型可以留空,但“<>”必须写上
    
    //创建学生对象
    String n1 ="小杜";
    String n2 ="小王";
    String n3 ="小张";
    
    //把创建的学生元素添加到集合当中,利用ArrayList这个类的方法
    list.add(n1);
    list.add(n2);
    list.add(n3);
    System.out.println(list);
    //最后打印输出
    
  }
}

2. Common methods

public boolean add(E e); Adds the specified element to the tail of this collection

public E remove(int index); Remove the element at the specified position in this collection and return the deleted element

public E get (index); Returns the element at the specified position in this collection and the obtained element

public int size(); Returns the number of elements in this collection. When traversing the collection, you can control the index range to prevent out of bounds.

6、 String class

1. Characteristics

2. Common methods

Methods of judging function: equals and equalsignorecase

Methods to obtain functions: length, concat, charat, indexof, substring

Methods for converting functions: tochararray, getbyte, replace

Method of dividing function: split

7、 Static keyword

Static method

Static principle

Static code block

8、 Array class

1. Methods of manipulating arrays

public static String toString; Returns a string representation of the contents of the specified array.

public static void sort(); Sort the specified array in ascending order;

9、 Math class

1. Basic operation method

public static double abs(); Returns the absolute value of a double value

public static double ceil(); Returns the smallest integer greater than or equal to the parameter

10、 Inherit

1. Overview

: multiple classes can be called subclasses, and a single class can be called parent class, superclass or base class

2. Representation:

Class subclass extends parent class {...}

3. Member method override

: @override

4. Characteristics after inheritance -- construction method

5. Super and this keywords

6. Characteristics of inheritance:

11、 Abstract class

1. Representation

Note: attention should be paid to the modifier and parameter list

Abstract class: public abstract class class name {}

Abstract method: public abstract void method name ();

2. Precautions

12、 Interface

1. Definition and Implementation

Public interface interface name {}

Class name Implments interface name{

//Override abstract methods in interfaces (required)

//Override the default method in the interface (optional)

}

2. Basic and multiple implementations

Note: the relationship between class and interface is implementation relationship, that is, class implements interface. This class can be called implementation class or subclass of interface

Abstract method

Default method

Static method

Private method

13、 Polymorphism

1. Reflect

Parent type variable name = new subclass object;

2. Upward and downward transformation

Upward Transformation: parent type variable name = new subclass object;

Downward Transformation: parent type variable name = (subclass type) subclass object;

14、 Final keyword

15、 Permission modifier

16、 Inner class

1. Concept

: if you create a new class B in class A, class B is called an internal class and class A is an external class

2. Access characteristics

3. Anonymous inner class (key)

17、 Reference type usage

1. Class as a member variable

2. Interface as member variable

3. Interface as method parameter and return value type

18、 Object class, period time class

1. Object class

2. Date class

Common methods:

public long getTime() ; Convert the date time to the corresponding millisecond value

3. Dateformat class

Common methods:

public String format(); Format date as string

public Date parse(); Converts a string to a date object

4. Calendar Class

Common methods:

Get and set methods

Add and gettime methods

19、 System class

1. Common methods:

Currenttimemills method

: get the millisecond difference between the current system time and 00:00 on January 1, 1970

Arraycopy method

: copies the data specified in the array to another array.

20、 StringBuilder class

1. Common methods

Append method: adds string form of any type of data and returns the current object itself

ToString method: converts the current StringBuilder object to a string object

21、 Packaging

Note: from jdk5, packing and unpacking are automatically completed

22、 Collection interface

1. Inheritance system of collection classes

@H_ 403_ 879@

2. Collection common functions

List collection

1. Common methods

: get, add, remove, set methods

2. List subclass

Set interface

1. Two sets

Colletions collection tool class

23、 Map collection

1. Common collections

2. Common methods in map interface

24、 Generics

1. Define format

Modifier class class name < variables representing generics > {}

25、 Iterator iterator

1. Common methods

public E next(); Returns the next element of the iteration.

public boolean hasNext() ; Returns true if any element is to be returned

26、 Abnormal

1. Abnormal system

: the root class of the exception is Java Lang. throwable, which has two subclasses Java Lang. exception and Java lang.Erro

@H_ 403_ 879@

2. Exception handling

使用格式: 在方法内——thorw new 异常类名(参数)
public static <T> T requireNonNull(T obj):查看指定引用对象不是null
修饰符 返回值类型 方法名(参数) throws 异常类名1,异常类名2…{   }	
try{
     编写可能会出现异常的代码
}catch(异常类型  e){
     处理异常的代码
     //记录日志/打印异常信息/继续抛出异常
}

@H_ 403_ 879@

3. Abnormal precautions

4. Custom exception

27、 Thread

1. Threads and processes

2. Concurrency and parallelism

Successor

Lambda expression, reflection, annotation, IO stream

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>