Java Basics-Class

Source: Internet
Author: User
Tags modifiers

1. Related concepts of the class:

  • The class name must be the same as the file name when there is a public adornment

  • If there is no public keyword decoration, it can be different

  • A class is an abstraction of something with the same characteristics and behavior.

  • Features can be understood as static

  • Behavior can be understood as dynamic

  • *package the current file into the corresponding package

  • *package behind usually write URLs upside down

  • *package must be the first line of the current file

  • *import introduce the classes that you need to use in this file

2. Variables define three elements:

* Type modifier variable name = initial value; * Variables created using a class can be called Object * Class Name Object name = new class name ();

3. Properties of the Action object

* Object name. property name to modify it

* Object name. Method name to modify it

4. Method: The behavior of the class is the method

* The method is written as follows: * Modifier return value type method name (parameter list) {}

4.1 Modifiers:

* There are three types of modifiers that can be written: * Public Private protected * not written as default

4.2 Return value type

* No return value type: void * has return value: int  double String Car ... * return value needs to use Return statement within method  * No return value type can also use return to stop the current method

4.3 Depending on the return value and parameters, there are four methods

* 1. No parameter no return * 2. No parameter is returned: A method with a return value can be understood as an expression, and the result is the return value of the method * 3. There is no return: The parameter method needs to be declared according to the method, pass in the corresponding parameter * * * parameters passed in Parameter type * * method declaration is called parameter * method * 4. There is a parameter return: The passed argument has no parameter type

5. Methods can be called in the method

* Method Overloading: * Method name is the same, but parameter list is different: number-order-type Different *---------method overload and return value type Independent---------

6.setter methods and Getter methods

* Setter Method: Setup * Format: public void Set Property name () * Getter method: Accessor * Format: Public property type Get property name () * Setter and getter method is not required to write * as required to determine whether the property needs to be set The device or accessor * set and accessor are not required to appear in pairs * This represents the object of this class * this. Property---Represents the property of the class, not the parameter

Construction Method:

* Initialization when creating an object * when there is no constructor method in the class, the compiler creates an parameterless constructor method * In the format: Access modifier class name (parameter list) {} * constructor method can also overload * constructor method cannot have a declaration of a return value type

7. Set directly within the construction method

* ****** do not use return within the constructor method to return a value ****** * as long as there is any one constructor method in the class * The compiler will not generate an argument-less construction method

8. Inheritance

*  new classes derived from existing classes *  keywords are: extends *  only single inheritance is allowed in Java

9.super ()

* SUPER () represents the constructor method that calls the parent class * super () must be written in the first row of the method * First assign the properties of the parent class integrated * before assigning the properties of the class to the value

10.final decorated class, which means that the class cannot be inherited

    • The final modification method, which means that the method cannot be overridden by a quilt class

    • Final decorated property, which means that the property can no longer be assigned a value

    • ---The final decorated property, the variable is named plain uppercase---

Common classes

1.String class

     * (1). String comparison method:     * The return value type is Boolean and the argument is another string     * object. Equals (Object)
     * (2). How to find a String 1 indexOf (parameter)
     * The return value is int, which is the index of the character in the string
     * Returns the first qualifying character position, no return-1
     * Parameters are characters that need to be searched
     * Find Method 2 indexOf (parameter 1, parameter 2)
     * Parameter 2 is the location to start the search, and the incoming position will be judged
* Returns the position of the first character that meets the requirement from the start position, no return-1
     * (3). String substitution method IndexOf (parameter 1, parameter 2)     * The return value is the new string after substitution     * Parameter 1 is the substituted character (string), parameter 2 is the substituted character (string)     * If the parameter 1 does not exist in the original string, it does not replace     * Replace all parameters in the original string 1
* (4). Determines whether a string starts with the specified string Startwith ()
* Return value type is Boolean
     * Parameters are strings that need to be judged
* (5). Determine if a string has been specified at the end of the string EndsWith () 
* Return value type is Boolean
     * Parameters are strings that need to be judged
    * (6). Remove the space at the beginning and end of the string    * TRIM ()    * method for calculating the length of a string long  ()    * return value int

2.StringBuffer

* Modifiable String Classes

     * (1). How to intercept strings substring ()     * Parameter 1 is the index from which to start     * Parameter 2 is the end of the index     * Tips: Follow the left closed right open principle [)     * (2). Delete method of string delete ()     * Parameter 1 is the index from which to start deleting     * parameter 2 to which index end delete
 


* Tips: Left closed or present
Class Set Framework

1. Arrays: Collections of the same data type

* Elements in the array are ordered * format: Data type array name [] = new data type [array length]; * Use elements in an array:     * Value or assignment by array name [element position]

Common methods of 2.ArrayList

  * ArrayList can be understood as inheriting collection  * Collection All methods ArrayList can be used (1) to create a ArrayList class  * arraylist<string> list= new Arraylist<string> ();  * <> Middle type is called generics      * temporarily understand array stored element type     (2) Add Method List.add ()      * add element List.add at specified position (parameter 1, parameter 2)      * Parameter 1 is position      * Parameter 2 for Add content (3) Get Method List.get ()     (4) Delete method List.remove () (     5) Replace method List.set (parameter 1, parameter 2)      * Parameter 1 represents the location to be replaced     * Parameter 2 for the new element    (6) Gets the position of the specified element in the collection List.indexof (parameter)     * Returns a numeric value of type int

3.MAP is a data structure for storing key-value pairs

* Map Storage data is unordered (1) Create a Map object  * hashmap<string,string> map= new hashmap<> (); (2) Add method Map.put (parameter 1, parameter 2) * need to pass in two parameters * The first parameter is key * The second parameter is value (3) Remove Method Map.Remove () * Removes the entire key value pair (4) based on the key value to  determine if a key or Valu is included E   1> Map.containskey (key value) if (Map.containskey ("Leopard Head")) {System.out.println ("the collection contains the Leopard Head");} Else{system.out.println ("No leopard head in the collection");   2>map.containsvalue (value value) if (Map.containsvalue ("Lu Junyi")) {System.out.println ("contains Lu Junyi in the Collection");} Else{system.out.println ("The collection does not contain Lu Junyi");}


IO Stream and exception

1. Exception: The error that caused the program to break

* Exceptions can be classified as run-time exceptions and non-runtime exceptions * Exceptions need to be handled, otherwise the program will be interrupted

2. Exception handling

* Method: Try...catch * Write the statement in the try block with the possibility of an exception * If an exception is thrown in a try, the catch can be caught in the capture to write multiple, but note: * If an exception has an inheritance relationship, you need to write the parent class behind the subclass. * Note: either No exception occurs and finally blocks are executed to

3.throws keywords

* Used on a method declaration, which means that calling the method may throw an exception * depending on the classification of the exception, decide whether to force the call to use try: catch* if the thrown exception is a run-time exception, the try...catch* is not enforced if the exception thrown is a non-runtime exception, forcing Try...catch

4.throw keywords

* Throws an exception in a method * Determines whether to add throws* in a method declaration if the exception thrown is a non-runtime exception * You need to use the throws declaration in a method declaration

5. How the byte stream is used

* Reference to create input stream and output stream  file src = new file ("/users/dllo/desktop/io/src.txt");  File Dest = new file ("/users/dllo/desktop/io/dest.txt");  FileInputStream  fis = new FileInputStream (SRC);  FileOutputStream fos = new FileOutputStream (dest);

6. How character streams are used

* Create references for input stream and output stream

File Src1 = new file ("/users/dllo/desktop/io/src1.txt");

File Dest1 = new file ("/users/dllo/desktop/io/dest1.txt");

FileReader FR = new FileReader (SRC1);

FileWriter FW = new FileWriter (DEST1);

Java Basics-Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.