Basic java tutorial-Java object-oriented

Source: Internet
Author: User
Tags class definition define local exception handling getmessage modifier throw exception

01. Object-oriented (Polymorphism concept)

Definition: multiple forms of a certain type of thing. It can be understood as multiple forms of things.

02. Object-oriented (polymorphism-scalability)

Polymorphism: the reference of the parent class points to its own subclass object. The reference of the parent class can also accept its own subclass object. For example, Father father = new Sun ();
Benefits of polymorphism: the appearance of polymorphism greatly improves the scalability of the program.
The premise of polymorphism: the relationship between classes must be inherited or implemented. There is usually another premise that there is coverage.
Disadvantages of polymorphism: it improves scalability, but can only access members in the parent class by referencing the parent class.

03. Object-oriented (polymorphism-transformation)

What should I do if I want to operate the method unique to the subclass? Forcibly convert the reference of the parent class to the subclass type.
For example:
Father father = new Son ();
Son son = (Son) father;
But you cannot do this: (convert the parent class object to a subclass type)
Father father = new Father ();
Son son = (Son) father;
What we can convert is that when the parent class reference points to its own subclass object, the reference can be promoted or forced conversion. The beginning and end of polymorphism are all sub-class objects being changed.
When determining whether an instance is of a certain type: a instanceof Son; a is the instance name; Son is the class name

04. Object-oriented (polymorphism-example)

05. Object-oriented (characteristics of members in polymorphism)

Features of member functions (non-static) in polymorphism:
During the compilation period: see whether the class to which the referenced variable belongs has called methods. If so, the compilation passes. If not, the compilation fails.
During runtime, see whether the class to which the object belongs has a call method.
Summary: when a member function is called for multi-state compilation, the member function is compiled to the left and the running to the right.

Interview:

Features of member variables and static functions in polymorphism:
Regardless of compilation and running, refer to the left side (reference the class to which the variable belongs)

06. Object-oriented (multi-state motherboard example)

Pci interface
Pci interface for main board
The main board has the UsePci method. The parameter is of the interface type and the NIC instance is actually passed in.
Pci interface for Nic

07. Object-oriented (example of multi-state extension)

Requirement: database operations. Data is user information.
(1) connect to the database,
(2) operate databases and add, delete, modify, and query
(3) close the database connection.

08. Object-oriented (Object class)

Object is the direct or indirect parent class of all objects, the legendary god.
This class must be defined as a feature of all classes.
The Object already provides a comparison method for whether the Object is the same. If the user-defined class also has the same functions, there is no need to re-define the Object. As long as the features in the parent class are followed, set up your own comparative content, which is covered.

08. Object-oriented (Object class ToString)

 

Definition: define a class in another class. The class inside is called an internal class (built-in class ).

Internal features:
The internal class can directly access members in the external class, including private.
To access an internal class, an external class must create an internal class object.
Access the internal class in another class: external class. Internal class name = new external class (). new internal class ();
Private can be used to modify the class before the internal class.
If the internal class has the same variable name as the external class, the method for accessing the internal class variable is as follows: external class. this. Variable name;

02. Object-oriented (static internal class)

Summary access format:
1. When an internal class is defined as a member location of an external class and is not private, an internal class object can be directly created in another external class.
Format: external class name. Internal class name variable name = external class object. Internal class object;
For example, Outer. Inner in = new Outer (). new Inner ();
2. When the internal class is in the member position, it can be modified by the member modifier, such as private, which encapsulates the internal class in the external class. Static: Internal classes have static features.
After the internal class is modified as static, you can only directly access static members in the internal class, and the access permission is displayed.
In other external classes, how do I directly access static internal classes?
Method: new external class name. Internal class (). Function name (parameter); for example, new Outer. Inner (). Function ();
If the member functions of the internal class are static, the access method is Outer. Inner. Function ();

Note: When a static member is defined in an internal class, the internal class must be static.
When the static methods in the external Class access the internal class, the internal class must also be static.

03. Object-oriented (internal class definition principles)

When describing a thing, there is something inside it. It is described by an internal class. Because internal transactions use the content of external transactions.

04. Object-oriented (anonymous internal class)

You can also define local classes in the Member methods, but the department classes cannot be modified by private or static.

When an internal class is defined locally, it cannot be modified by a member modifier. It can directly access members in an external class because there are references in an external class, but it cannot access the variables in its local part, only local variables modified by final can be accessed.

Anonymous internal class:
1. Anonymous internal classes are simply abbreviated internal classes. Is an internal class with no name.
2. Prerequisites for defining anonymous internal classes: an internal class must inherit a class or implement an interface.
3. Anonymous internal class format: new parent class or interface () {defines the content of the subclass}
4. In fact, the anonymous internal class is an anonymous subclass object, which is a bit fat and can be understood as an internal object with content.
To define an anonymous internal class in the Member method:
Public void member method ()
{
New external interface or class name ()
 {
Override or override methods or abstract methods in external classes or interfaces;
}. Name of the method that is overwritten or exclusive to itself or overwritten ();
}

05. Object-oriented (exception overview)

Exception: The exception occurs when the program is running.
Exception cause: the problem is also a specific thing in real life. It can also be described in the form of java classes and encapsulated into objects, it is actually a concrete manifestation of the abnormal situation described by java.
There are two types of problems: one is a serious problem and the other is a non-serious problem.
For serious cases, java uses the Error class to describe. For non-serious cases, java describes them through the Exception class.
Generally, no specific code is written to handle the Error.
Exception can be handled in a targeted manner.

06. Object-oriented (Try-catch)

Exception handling: java provides special statements for processing;
Try
{
Code to be detected;
}
Catch (exception variables)
{
Code for handling exceptions;
}
Finally
{
Statements that will be executed;
}
Perform common operations on the caught exception object: String getMessage ();
E. printStackTrce (); in fact, the default exception handling mechanism of jvm is to call this method.

07. Object-oriented (exception declaration throws)

08. Object-oriented (multi-exception handling)

Multi-exception handling:
1. When declaring an exception, it is recommended to declare a more specific exception so that the handling can be more specific.
2. When the other party declares several exceptions, there should be several catch blocks. If the exceptions of multiple catch blocks are inherited, the parent exception catch block should be placed at the bottom.
When performing catch processing, catch must define a specific processing method. Do not simply define e. printStackTrace (). Do not simply write an output statement.

09. Object-oriented (custom exception)

Because special problems occur in the project, and these problems are not described by java and the objects are encapsulated, these special problems can follow the java philosophy of problem encapsulation, encapsulate custom exceptions for specific problems.

Requirement: in this program, if the divisor is negative and cannot be computed, you need to customize the problem.

The code is as follows: Copy code

Class ZiDingYiException extends Exception
{
ZiDingYiException (String msg)
 {
Super (msg );
 }
}
Class Test
{
Int div (int a, int B)
 {
If (B <0)
  {
Throw new ZiDingYiException ();
  }
 }
}

When a throw exception occurs in the function, the corresponding processing action must be performed either in the function try-catch or in the function declaration.
In general, an exception occurs inside the function and must be declared on the function.

How can I customize the exception information?
Because all the operations on the exception information have been completed in the parent class, as long as the child class is constructed, the exception information is passed to the parent class through the super statement, you can use the getMessage method to obtain the custom exception information.
The custom exception must be a custom class that inherits Execption.
Why inherit Exception?
1. the exception system has a characteristic that exception classes and objects need to be thrown. They all have the throwing property, which is unique to the throwable system, only members of this system can be throw and throws.

10. Object-oriented (difference between throw and throws)

(1) throws are used in functions, and throw is used in functions.
(2) throws follow exception classes, while throws follow exception objects.

11. Object-oriented (RuntimeException)

Exception has a special subclass Exception RuntimeException, which is an Exception during runtime. If an Exception is thrown inside the function, the function does not need to be declared. The compilation is the same. If the object is declared on the function, the caller does not need to handle exceptions, and compilation is the same.

Note: You do not need to declare the function because the caller does not need to handle the exception. When this exception occurs, you want the program to stop because the operation cannot continue, if you want to stop the program, modify the code.

When a custom exception occurs and the operation cannot be continued, the custom exception inherits RuntimeException.

There are two types of exceptions:
1. Exceptions detected during compilation
2. Exceptions that cannot be detected during compilation (runtime exceptions, runtimeexceptions and their subclasses)

12. Object-oriented (abnormal exercises)

Requirements:
Instructor bi teaches on a computer
Normal

The code is as follows: Copy code
Class computer
{
Public void run ()
 {
System. out. println ("computer running ");
 }
Public void reset ()
 {
System. out. println ("computer restart ");
 }
}
Class Teacher
{
Private String name;
Private Computer cmpt;
Teacher (String name)
 {
This. name = name;
 }
Public void prelect ()
 {
Cmpt. run ();
System. out. println ("lecture ");
 }
}
Class test
{
Public static void main (String [] args)
 {
 
 }
}

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.