Java review: class management and common tools

Java review: class management and common tools

package
  • Write in the first line of the program file
  • Only one package can be declared in a Java source file,
    and the declaration statement can only be used as the first instruction of the source file
  • Imported classes can import non-public classes, but cannot be used because the default permissions in other packages cannot be used
package Testp;
import Testpackage.*;
public class C {
	public static void main(String[] args) {
		A a=new A();
		B b=new B();
	}
}//Testpackage.*只能导入

Insert picture description here

  • At this time, do not put the two classes AB in the same .class file, otherwise one of them must be a non-public class, and the non-public class cannot be used under C and so on.
  • Note .*是对于包来说的,当把AB分成两个class文件时就可以两个全部导入the
  • The Java compiler automatically imports the package java.lang for all programs
  • Unnamed packages cannot be imported. * Classes in unnamed packages cannot be imported
  • When importing statically, if it is import aa .; all members of the class can be imported including static, if it is import static aa.A .; it is to import all the static members in A, non-static will report an error, so you can import all of A Member changed to static
  • A class file (.java) can only have one pubic class. If there are multiple public classes in a package, create multiple class files
Wrapper class
  • Byte 、Short 、Integer 、Long 、Float 、Double 、
    Character 、Boolean

  • Two types of constructors for wrapper classes

    • 包装器类名(基本类型值)
    • 包装器类名(基本类型值的字符串)
      • The Character class constructor can only be character parameters;
      • The Boolean wrapper is case-insensitive to true/false
  • Insert picture description here

  • example:

float f=(new Double(100.2)).floatValue();
int i=(new Double("100.2").intValue());//XXXvalue():包装器类-基本类型

float f=Float.parseFloat("11.3f");
int d=Integer.parseInt("25");
//字符串-基本类型

Double d=Double.valueof("25.4");//Character包装器类没有这个方法
//字符串-包装器

//基本类型、包装器类型-字符串
String s1=Double.toString(3.14);//toString有静态方法也有实例方法
String s2=new Double(3.14).toString()



  • Note that when passing parameters, if the parameter is a wrapper class, a basic type cannot be passed directly. At this time, automatic boxing and unboxing are not supported.
  • To use the constructor to automatically box
    • Double a=1.2;// Double a=new Double(1.2); automatic boxing
    • double b = a; //Automatic unboxing
enumerate
  • enum enumeration type name {list of enumeration constants}
public class Ch_5_5_2 {
enum Season {  春季,  夏季,  秋季,  冬季; }
public static void main (String[] args) {
Season [] s= Season.values();
for (int i=0; i<s.length; i++)
{ switch(s[i]) {
case 春季: //case Season. 春季 编译错
System.out.println(“ 春困”); break;
case 夏季:
System.out.println(“ 夏打盹”); break;
case 秋季:
System.out.println(“ 秋乏”); break;
case 冬季:
System.out.println(“ 睡不够的冬三月”); break; }
}
}
}
Advanced applications of arrays
  • To importimport java.util.Arrays;
  • The sort method and binarySearch premise is that the array cannot be in descending order, it can be unordered or ascending
  • int i=Arrays.binarySearch(a1,3);Returns the index of the first occurrence of this element
  • Arrays.fill(c,4); Fill the array c with 4
Advanced applications of strings
  • StringBuilder: unsynchronized threads are not safe but perform more efficiently

    • Insert picture description here
  • StringBuffer

    • Insert picture description here

    • Insert picture description here

Regular expression
  • Insert picture description here

  • Regular matching steps:

Pattern p=Pattern.compile(reg1);//reg1为模式串正则表达式
Matcher m=p.matcher(str);//str是待匹配串
m.group();m.find;m.start()
  • String[ ] split(String regex)
    regex is a delimiter string, or it can be a regular expression.
    Function: split a string and generate an array
String s="aa,bb,cc,dd";
String[] sa=s.split(","); //以 , 作为s的分隔串,提取字符串
for(String s1:sa)
System.out.println(s1);
  • String replaceFirst(String regex, String rp)
    Function: Replace the first appearance of regex with rp
  • String replaceAll(String regex, String rp)
    Role: replace regex with rp
Random number class
//要导入java.util.Random
Random r=new Random();
int x,i;
for(i=1;i<=10;i++){
x=r.nextInt(10)+1;//默认从0开始,要获取从1-10的就要+1
System.out.println(x);
}

Object-oriented programming

Memory management of objects and object references
  • Stack (automatic management mechanism)
    • The data in the stack space is automatically managed by the system, that is, the data space is automatically created when the function is called, and the data space is automatically released when the function runs.
    • All variables defined in the function, whether basic types or reference types, are stored in the stack space
  • Heap (manual management mechanism)
    • Realize on-demand allocation or release
    • All objects are stored in heap space
  • Constructor
    • Same name as the class, no need to define the return value type, and no return statement, even if there is no return value, it cannot be declared with void
    • When using new to instantiate an object, the system
      calls the corresponding constructor according to the given parameter list. This is the constructor overload
  • Structure code block
    • Role: unified initialization of all objects
    • Features:对象一建立就执行并且优于构造函数执行
class Person {
private String name;
private int age;
{ age=15; }//构造代码块
Person() {
System.out.println("A:name="+name+",age="+age);}
Person(String n) {
name=n;
System.out.println("B:name="+name+",age="+age);
}
}
Object destruction
  • Constant final
    • Constant reference cannot be changed, that is, it points to an object that cannot be changed, but the content of the pointed object can be modified
    • Note that if it is a string or an array, the reference must be in the heap and also point to a final data
    • a=b is the object pointed to by a and b
    • == Determine whether the addresses are the same
    • a.equals(b) Determine whether the content is the same
  • Object array
    • new String[10] does not create a String class object (but an array object), so the String constructor is not called.
    • sa[0] is a String reference, not a String object, the one pointed to by sa[0] is the string object
inherit
  • For private members of the superclass, but cannot be used
  • When constructing an object, the constructors of the class and the super class are automatically called
    , and the calling order is the same as the derivation order
  • If the super class is non-private 无参构造函数, then the sub class 自动调用; if the super class is 有参构造函数, then the sub class constructor 必须显式调用超类的构造函数, otherwise the compilation error
Singleton mode
class Student{
String name;
int age;
private Student(){} 
public static Student creat(){//一定是static这样可以通过类名调用
Student a=new Student();//在方法中new
return a;
}
}
Student st=Student.creat();
System.out.println(st.age); 
Polymorphism
  • Meaning: a polysemy
  • Overloading (static binding is determined at compile time) the same object and the same method have different parameters
    • Dog 1. Sniff (owner's smell q); Dog 1. Sniff (bone smell q); ……
    • Attention 重载传参, if it is a basic type of data can be forced to convert the big can be converted to small, but the small can not be converted to large
  • Rewrite (determine dynamic binding at runtime) the same behavior of different objects is substantially different
    • Dog d=new Tibetan Mastiff()/ new Husky(); d. Bite( );//Rewrite Bite this method to different new objects to call this method is different, although this method is the parent class of extents保证同名同参同返回类型权限不能缩小
    • If you want to prohibit a class from deriving subclasses, set the class为final
    • To prohibit subclasses from overriding an operation can set the operation tofinal或者static
    • Methods that do not want to be overridden can: final/static/private//The class is not privately modified

Insert picture description here

Abstract classes and interfaces
  • Abstract class: establish a contract between father and son
    • abstract cannot modify the constructor
    • abstract cannot be used with static final
    • Subclass must override abstract class
  • Interface: Establish a contract between arbitrary classes

Guess you like

Origin blog.csdn.net/Phoebe4/article/details/110339045