Java (vi)

Source: Internet
Author: User
Tags character classes modifier modifiers variable scope stringbuffer

Interface


A. Syntax:
Public interface Interface Name {
public void foo ();
}

Two. Features
1. Interfaces cannot be serialized
2. The implementation class must implement all methods of the interface
3. Implementation classes can implement multiple interfaces
4. The variables in the interface are static constants, which are public modifiers by default
5. There is no construction method in the interface
6. The methods in the interface are all public-modified abstract methods

Three. Steps:
1. Writing the interface--interface
2. Implement Interface--implements
3. Using the interface

Four. interface-oriented programming
1. What is the ability of the relationship implementation class, without concern for implementation details
2. Interface-oriented conventions, without regard to the specific implementation of interfaces
3. Program design is interface-oriented about without regard to specific implementation

Five. Benefits of interfaces over abstract classes
1. can be multiple inheritance
2. Design and achieve complete separation
3. More natural use of polymorphic
4. Easier to build a program framework
5. Easier to implement

Polymorphic

Polymorphic
I. Concept
1. The same thing, due to different conditions, produces different results
2. The same reference type, different actions are performed using different real columns
Parent class referencing child class object

Two. Steps
1. Writing the parent class
2. Writing subclasses, subclasses reusing parent class methods
3. Run-time, using the parent class's type, the object of the child class

Three. Type conversion
1. Upward transformation (automatic type conversion)
Parent class A=new sub-class ();
2. Downward transition (forced type conversion)--on the basis of upward transformation
Parent class A=new sub-class ();
Subclass B= (parent class) A;

Four. Two ways to achieve polymorphism
1. Using the parent class as a method parameter to implement polymorphism
2. Using the parent class as the method return value to implement polymorphism

Five. instanceof operator
Object instanceof class or interface
Instanceof commonly used in conjunction with forced type conversions

Inner class
1. Depending on the location of different Java internal classes are divided into: static inner class, member inner class, local inner class, Anonymous inner class
2. Any internal classes are compiled into a separate class file
3. The biggest role: encapsulation
4. Anonymous classes can encapsulate internal concepts

Static Inner class
Declaring in a class with the static decoration
Static members of external classes can be accessed in static inner classes
Column: public class Foo () {External call:
Static Class woo{}-----------foo.woo=new Foo.woo ();
}
The scope of the static inner class is the same as the static variable, which is when the class is loaded.

member Inner class
Declared in a class, without static, with the member characteristics of the class, that is, the class must have a real column to create

Class, the inner class real column can access the members of the external class
Columns: public class yoo{external calls:
Class moo{}----------Moo Moo=yoo.new moo ();
}

Local inner class
Declaring a class in a method is a local inner class, scoped like a local variable
In a method of a local inner class, such as to access the properties of an external class, it must be a final decorated constant

Anonymous inner class
1. Can be used anywhere, just like a general statement
2. Syntax like create object------Date date=new date{//...}
3. An anonymous class is an inheritance of the original class, and a real column is created, and {} is the class body after inheriting
4. Syntax for all classes can be used in the class body
5. Anonymous classes cannot write construction methods
6. Anonymous classes can inherit from an abstract class or interface and must provide an implementation of an abstract method


Object-oriented three features: encapsulation, inheritance, polymorphism
Packaging:
Syntax: Property privatization (private), providing a way to provide relative get/set to access (public),

The judgment of the business logic corresponding to the data of the attribute in the method of Set/get
Idea: Packages outside of the program cannot directly access the properties of this class, and need to pass through the methods provided by this class

Access to the properties of this class, thereby improving the security of the code.
Other points of knowledge related to:
Construction method: Used to describe the object creation process, you can create objects quickly by constructing the method.
This keyword: represents this class of objects, calling the properties, methods, and construction methods of this class.
Method Overloading: Method name is the same, parameter list is not the same
Inherited:
Syntax: Subclass extends Parent class
Thought: A common feature or behavior described by multiple classes can
The common parts of the class are extracted to create their parent class.
Benefits of Inheritance: Subclasses share the code of the parent class, which improves the reusability of the Code;
Later extension and maintenance, just add the corresponding subclass.
Other points of knowledge related to:
Super Keyword: An object that represents the parent class, calling the parent class's properties, methods, and construction methods.
Abstract keyword: abstraction, adornment class, adornment method.
Static keyword: statically, decorated property, method, code block, inner class
Final keyword: Finally, cosmetic properties, methods, classes
Method overrides: The same method name, the same argument list, the same return value type,

modifier greater than or equal to the access modifier of the parent class
Polymorphic:
Syntax: Parent class object name =new subclass ();
Thought: A reference to a parent type can point to the memory space of a child class object in memory,
It is convenient to design the parameters and return value types of the method, you can
Designed to be a parent object, such a method is relatively concise when implemented, and in late
The method is easier to extend.
In code, polymorphic use is more of a parent class reference to a subclass object,
Automatically type conversions (up), but sometimes you need to call subclasses
In their own things, this time you need to also atomic class, called coercion type conversion
(downward transition), use the instanceof operator to do the type of
Judge.

1.final keywords can be decorated with properties, methods, classes
2.final keyword-decorated classes cannot be inherited, such as: String class
3.final keyword decoration method, cannot be overridden in quilt class
4.final keyword-modified variable, value cannot be changed after initialization
Variables that are decorated with static and final keywords in Java are called constants, and constant names are all uppercase
such as: public static final int age=20;
A variable of the reference type of the final modification, the reference address cannot be changed, but the first level of the value is qualified,

Property values for reference data types can change

The 1.abstract keyword represents an abstract meaning that can be used to modify methods, classes
The modified class is called abstract class, and the method of decoration is called abstract method.
2.abstract keyword Decoration class
Access modifier abstract class class name () {}
Abstract classes are used to represent some abstract concepts
Features: A. Abstract classes cannot be serialized
B. Abstract classes can have properties, methods, and construction methods that are used to inherit from, invoke
C. Methods in abstract classes are not necessarily abstract methods, and abstract methods must be in an abstract class
3.abstract keyword Retouching method
Access modifier abstract return value type method name (parameter list);
Abstract methods do not need to be implemented by themselves and are implemented by subclasses
Features: A. Abstract method without method body
B. Abstract methods must appear in abstract classes
C. A class that inherits an abstract class must implement all of the abstract methods inside
4. Abstract classes can inherit abstract classes

Method overrides
1. Method names are the same
2. Same parameter list
3. The return value is the same or its sub-class
4. Access modifiers are not strictly for character classes
Note: @override detection method is rewritten correctly
Construction method cannot be overridden

Eight. Method overrides differ from method overloads

Location Method Name parameter list return value access permission
Method overrides subclass the same as same or its subclasses are not strict with the parent class
Method overloading this class with the same different unrelated unrelated

1. Before creating the class, check that the class is loaded (check that the class file on the hard disk is loaded into memory).

If you do not load the parent class file, load the file of this class first
Load strategy used by Java: Lazy loading (load as needed), load when used, load only once
2. Allocating object space recursive allocation of property spaces for all parent classes and subclasses
property is automatically initialized to the "0" value
3. Assigning a value to a property
4. Call the Parent class construction method (default call to the parent class without a parameter constructor method)
5. Call this class construction method

Access decoration into similar bun class other
Private y-n n N
Default y y n n
Protected y y y N
Public y-y y y

Note: In a class, there can be only one class that is modified by the public

Y:yes N:no

I. Advantages of inheritance
1. Improve the reusability of the code (after inheritance, subclasses share the parent code)
2. Improved maintainability of code (late code additions, deletions)

Two. Prerequisites for succession
A child class is a parent class that must satisfy the logical relationship between the subclass is a and the parent class to inherit

Three. Steps/Syntax
1. Extracting attributes and methods from multiple classes to create a parent class
Access Modifier class Parent class {

}
2. Complete the inheritance using the extends keyword after the subclass
Access modifier class Subclass extends parent class {

}
3. You can call the code of the parent class in the subclass--use the Super keyword to represent the parent class object
A. Accessing the parent class construction method
Super ();--called in the subclass constructor method and must be the first, mutually exclusive with this
Super (parameter list);
B. Accessing the parent class Property--provided that the property cannot be private
Super.name;
C. Methods to access the parent class
Super.print ();

Note: 1. Construction methods in the parent class are not inherited, but can be called
2. Subclasses are not in the same package as the parent class, and members that use the default Access adornment permission cannot be inherited
3.private members cannot be inherited

Four. Initialization order of multiple-inheritance relationships
Parent class---parent class construction Method--child class---child class construction method

Benefits of object design and development programs
1. Communicate more smoothly
2. Improve design and development efficiency
The process of object-oriented design
1. Discovery Class
2. Discovering the properties of a class
3. Methods for discovering classes

1.static decorated resources belong to class-level resources, static resources, and resources shared for all real columns of the class
2.static keywords can be used to modify properties, methods, code blocks, inner classes
A.static Modifier Properties:
The static decorated property belongs to the resource shared by all the real columns of the class
Access static properties: Class name. property name ();
B.static Silk Fibroin Method:
Static methods are statically modified and can be called directly from the class name. Method Name (), which is generally used as a static method

Tool methods A resource (object, property, object method) of an object cannot be called in a static method
C.static Modifying code blocks
Executes when the class is loaded, because the class is loaded only once, so the static code block is executed only once, typically

Use a static block of code to load some statically-resourced resources such as configuration files
static{{
Static code block//code block, which executes when the object is created
} }
3.static decorated resources that are executed during class loading
4.

Static, non-private modified non-static, non-private decoration

Attribute class properties, class variable real-column properties, real-column variables

Method class method Real-column method

Class name. Attribute ()
The invocation mode class name. Method () object. Property ()
Object. Property () object. Method ()
Object. Method ()

Attribution class Single Object ()

Packaging


1. Concept: To hide some of the class information inside the class, not to allow direct access to external programs, but through the method provided by the class to implement the operation and access to the hidden information,
2. Benefits: A. Access to data only through prescribed methods
B. Method modification implementation
C. Hiding the implementation details of a class
D. Easy to join control statements
3. Steps to use:
A. Modifying the visibility of a property----set to Private
D. Creating a public Get/set method----for reading and writing properties
c. adding attribute control statements to the Get/set method---to judge the legality of the property value

this keyword


1. Call Properties: this.health=100;
2. Calling method: This.print ();
3. Call the constructor method:
A.this ()--if used, must be the first statement in the constructor method
B.this ("Little Black", 100,100, "female")

1. Construction method is used to describe the process of object creation
Construction methods are used in the process of object creation
2. Syntax:
Access modifier (no return value type) constructor Method Name (= class name) {
Initialize Code
}
3. If there is no write construction method, the system will automatically provide a non-parametric construction method;
If a constructor is present in the class, the system does not provide a default parameterless construction method;
4. Method overloading
A. Method name is the same
B. Different parameters
C. Regardless of return value, access modifier
5. Method signature
A. Syntax: Method signature = Method name + parameter list such as Add (Int,int)
Two methods with the same method signature cannot appear in B.java

Class diagram


1. Benefits: A. For analysis and Design classes
B. Intuitive and easy to understand

2. Dog class name
+ name:string Public Property name: type
+health:int Public Property Name: type
-love:int Private Property Name: type
+print (): Void public method Name: return value type

StringBuffer

A.stringbuffer:string Enhanced Version
Using the StringBuffer class can improve program execution efficiency when you frequently modify strings (such as String links)
B.stringbuffer statement
StringBuffer sb=newstringbuffer ();-----Create an empty StringBuffer object
StringBuffer Sb=newstringbuffer ("AAA");------Create a variable store string AAA
C.stringbuffer use
Convert to String type: sb.tostring ();
Append string: Sb.append ("* *");
Insert string: Sb.insert (i, "* *");

1. Storing strings using String objects
String s= "Helloword"
String S=new string ();
String S=new string ("Helloword")

The 2.String class is located in the package Java.lang package and has a rich approach
Calculates the length of a string, compares a string, joins a string, extracts a string

3. String length
Syntax: Method principle: Public int length () {}
Call method: string identifier. length ();

4. String comparisons
The string class provides the Equals () method to compare whether the content stored in the two-character object is consistent (the return value of the Boolean type)

The difference between equals and = =
= =: Compare whether in the same memory space or compare numbers
Equals: Compare content consistency

5. String Letter Conversion
Use Equalslgnoercase () method--Ignore case when string comparison
Using the Tolouercase () method-Returns the lowercase form of a string
Use the toUpperCase () method--Returns the string uppercase form

6. String continuous
A. Use "+"
B. Using the Concat () method
A.concat (B);
b string will be contiguous to the A string after

7. String Extraction Method
A. Searching for the first occurrence of a character ch (or string value)
public int indexOf (int ch)
public int indexOf (String value)

B.. Search for the last occurrence of the character ch (or string value)
public int lastindexOf (int ch)
public int lastindexOf (String value)

C. Looking for a character or string from a specified location, returning the subscript
S.indexof (look for characters. Specify location)

8. Determine whether a string ends in a specified format (returns a Boolean type)
S.endswith (String)
Determines whether a string starts with a specified format (returns a Boolean type)
S.startswith (String)

9. Common methods of extraction
A.public string substring (int index)
Extracts the string part starting from the position--index
B.public String substring (int begininelex,int endindex)
Extracting a string part between Begininelex
C.public String Trim ()
Returns a copy of a call string with no spaces before or after it

10. String splitting
The string class provides the split () method, splits a string into substrings, and returns the result as an array of strings

member variables and local variables


1. The position of the variable declaration determines the scope of the variable
2. Variable scope determines the area of the variable that can be accessed by variable name in the program
3. Member variable: A variable defined in a class (can be called in a class or other class)
4. Local variables: Variables defined in a method (method)
5. Difference: A. Scope is different
The scope of a local variable is limited to the method that defines it
The scope of a member variable is visible within the entire class
B. Different initial values
Java will give the member variable an initial value
Java does not give initial values to local variables
6. In the same method, local variables with the same name are not allowed
In different methods, you can have local variables with the same name
Local variables have higher precedence in the same class when the member variable and local variable have the same name

Method

I. Defining class Methods
Syntax: Public return value type method name () {---Step one: Define method name and return type
Here you write the body of the method---step two: Write the method body
}

Two. Method return value
1. If the method has a return value, the method must return the value using the keyword return, the type of the return value type
Syntax: return expression;
Function: Jump out of method, return result
2. If the method does not return a value, the return value type is void;

Objects and classes

I. Characteristics of the Object
1. Properties--objects have various characteristics
2. Each property of each object has a specific value
3. Method--action performed by the object
4. Object: An entity used to describe an objective thing, consisting of a set of properties and methods

Two.. Class
A class is a mold that defines the characteristics (attributes) and behaviors (methods) that an object will have.
Class is the type of the object--unlike the int type: Has a method

Three. Relationship of classes and objects
A class is an abstract concept, just a template
object is a tangible entity that can be seen and touched.


Four. How to create objects and use objects
Steps for working with objects
A. Creating an Object
Class Name Object name =new class name ();
B. Referencing an object member: using "." Do the following
Properties of the Reference class: Object name. Properties
Methods for referencing classes: object names. Methods

Java (vi)

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.