Object-oriented Java implementation

Source: Internet
Author: User

1. Object-oriented Java implementation--encapsulation

1-1: Package
A. Why encapsulation (encapsulation can be data convenient maintenance, increase practicality, easy to expand and so on. Simulate things in real life through object-oriented thinking. )
B. What is encapsulation (encapsulation is the privatization of properties, providing a common way to access private properties)
C. How to implement Encapsulation
For example:
Package s2java.sg.ch01;
public class accpteacher3{
Private String name;//teacher name
private int age;//Age

Public String GetName () {
return name;
}
public void SetName (String MyName) {
Name=myname;

}
public int getage () {
return age;
}
public void Setage (int.) {
if (age<22) {
SYSTEM.OUT.PRINTLN ("Error! The minimum age should be 22 years old! ");
this.age=22;//If the age requirement is not met, the default value is given
}else{
This.age=age;
}
}
/*
* Return to self-introduction content
*/
Public String Introduction () {
Return "Hello everyone!" I am "+name+", I Am "+age+" year "old";
}
}
1-2: Construction method
A. Why you need to construct a method (you can assign a value to a property using a construction method). Also, instantiating an object is actually a way to invoke the constructor of the object. )
B. What is a construction method (the construction method is responsible for initializing the object member, assigning the appropriate initial value to the instance variable. )
The constructor method must satisfy the following syntax rules: The method name is the same as the class name and there is no return type. Or club two with, no return type.
C. A construction method with parameters (a constructor with parameters can pass parameters in the construction method.) )
When an object is instantiated using a constructed method with parameters, the values passed and the parameters of the constructor should match each other in the number, order, and type.
For example: AccpTeacher6 teacher=new AccpTeacher6 ("li name", 23, "undergraduate", "Counselor");
By invoking the construction method with parameters, the initialization of the object is completed and the object initialization code is simplified when the object is created.
1-3: Method Overloading
A. Method overloading in life
For example:
A driver can drive a different car, although the behavior of driving varies, but these operations are called driving. Driving this behavior constitutes a way of life in the overloaded.
B. Code examples for method overloading
For example:
public static int max (int a,int b)
public static int Max (long A,long B)
public static int Max (float a,foat b)
public static int Max (double a,double b)
The Max () method of the math class is called multiple times to perform different operations that take the maximum value separately.
For example:
public class test{
public static void Main (string[] args) {
(Math.max);
Math.max (1.0F,2.F);
Math.max (1.0,2);
}
}
C. Construction method overloading (constructor overloading is a typical exception to method overloading)
For example:
Package s2Java.sg.ch02;
public class accpteacher7{
Private String name;//teacher name
Private String school= "Beijing Center";//Location Center
Public AccpTeacher7 (String name) {
this.name=name;//setting the name of the instructor
}
Public AccpTeacher7 (String name,string School) {
this.name=name;//setting the name of the instructor
this.school=school;//setting up the center of the instructor
}
Public String Introduction () {
Return "Hello everyone!" I am "+school+" the "+name";
}
}
For example:
Package s2java.sg.ch01;
public class accpteacher7test{
public static void Main (string[] args) {
AccpTeacher7 teacher1=new AccpTeacher7 ("Li Ming");
System.out.println (Teacher1.introduction ());

AccpTeacher7 teacher2=new AccpTeacher7 ("Zhang", "Tianjin Center");
System.out.println (Teacher2.introduction ());
}


2. Object-oriented Java implementations-inheritance and polymorphism
2-1: Inheritance
A. Inheritance in life (animal <--herbivores and carnivores. Herbivores <--rabbits and small white sheep, carnivorous animals <--tigers and lions. The arrow points to the parent class!
B. Why inheritance is required (use inheritance to effectively implement code reuse)
C. How to implement inheritance (in the Java language, the extends (meaning of extends for extensions) is used to denote that a class inherits another class)
For example:
public class AccpJavaTeacher2 extends accpteacher{
}
This example shows that the AccpJavaTeacher2 class inherits the Accpteacher class
Note: Java in the same C # It is not allowed to have multiple inheritance
Interpretation: Method overrides, if a method defined in a subclass, the name, return type, and argument list are exactly the same as a method, name, return type, and argument list of the parent class. This is called the rewrite of the method!
D. Ancestor--object of all classes (Java classes are inherited directly or indirectly from the Java.lang.Object class, the Object class is the ancestor of all class Java classes.)
If you do not use the Extends keyword when you define a class, the class inherits directly from the object class.
For example: public class MyObject () {}
2-2: Polymorphic
A. Why polymorphism is required (polymorphism is the core of object-oriented thinking!). The benefit of this: Code extensibility and maintainability increases)
B. What is polymorphic (polymorphism is a feature that has the ability to express multiple forms or in a professional language: the same implementation interface that uses different instances to perform different operations.)
C. How to implement polymorphism
three steps:
(1) subclasses override the parent class's Method
(2) Use the parent class type as the parameter type, the parent class and its subclass object as a parameter in the
(3) runtime, which dynamically determines which method is used based on the object type actually created
Note: Polymorphism is closely related to inheritance and method rewriting. Benefits: Not only reduce the amount of code, but also greatly improve the scalability of the program and maintainability.

3. Object-oriented Java implementation--interface
3-1:java interface
A. The interface in the life (USB interface of the computer, power plug board, etc.)
B. What is a Java interface (a Java interface is a collection of some method features, but there is no implementation of a method.) The methods defined in the Java interface are implemented in different places and can have a completely different behavior. )
C. Why Java interfaces are needed (to increase the maintainability and extensibility of code, when a method does not know how to implement, or what effect to implement. The interface can be used at this time)
Experience: Using inheritance or Java interfaces can bring us polymorphic benefits. In general, when the relationship between two classes conforms to the "is-a", and the subclass can reuse the code of part of the parent class,
We use inheritance relationships to implement. In other cases, select the Java interface first.

Note: The Java interface cannot be instantiated, the members declared in the Java interface are automatically set to public, so the Java interface cannot declare private members,
The method body implementation and implementation of a Java interface cannot occur in Javajiek, and all methods defined in it must be implemented.
3-2: interface-oriented programming
A. What is interface-oriented programming (to properly use the Java language for object-oriented programming, thereby improving the reusability of programs, increasing the maintainability and extensibility of programs, must be interface-oriented programming)
When developing the system, the main frame uses the interface, and the interface forms the skeleton of the system. This allows you to replace the implementation of the system by replacing the class that implements the interface.
B. Examples of interface-oriented programming (printers, fans, etc.)
The implementation of interface-oriented programming can be divided into 3 steps:
(1) Abstraction of the Java interface
(2) Implementing Java Interfaces
(3) Using the Java interface
3-3: constant
A. Why constants are required (e.g.: 24 hours a day, time cannot be changed.) The version information for the project is not changed. Wait a minute!! thus using constants)
B. What is a constant (a constant is an identifier, and its value is constant during operation.) Constant identifiers can only be referenced in the program and cannot be re-assigned! )
For example:
Public final class math{
Pi
public static final double pi=3.1415926;
Convert the angdeg angle into a radial degree
public static double Toradians (double angdeg) {
return ANGDEG/180.0*PI;
}
}
C. The advantages of constants (using constants instead of information can enhance the readability and maintainability of the program.) )
The definition rule for constants in D.java (defining constants in the Java interface automatically adds the public static final modifier, so defining constants in the Java interface is the best place to be.) )

E. Declaring constants in the Java interface
For example:
public interface accpschooltype{
String accp= "ACCP affiliate Center";
String bent= "BENT Direct Center";
}
4. Using exception handler errors
4-1: Exception
A. Anomalies in life (on the street, a BMW suddenly appears, hitting the old lady.) The bridge suddenly broke when it rained. And so on abnormal phenomenon ....)
B. Exceptions in the program (in a course programming query above, prompting users to enter digital information.) However, the user input string, there is an exception, and so on! Abnormal condition ....)
C. What is an exception (an exception is an unhealthy event that occurs during the course of a program, and it interrupts a running program.) )
4-2: Exception Handling
A. What is exception handling (preprocessing: Some pre-made measures when an exception does not occur!) In the event of an abnormal program, the process is finished after the exception, the program continues to run! )
How to do exception handling in B.java
Java exception handling is implemented by 5 keywords: try, catch (capture), finally, throw (throw), and throws declaration exception
For example:
Package s2Java.sg.ch04;
public class test{
public void Method () {
try{
ThrowException (1);
}catch (Exception e) {
System.out.println ("Catch to Exception!") ");
}finally{
System.out.println ("The code in the finally is bound to execute!") ");
}
}
public void throwexception (int i) throws exception{
if (i==1) {
throw new Exception ();
}
}
}
Note: Throw throws exception, throws declaration exception
C.try-catch blocks (use the same as in C #!) )
D.try-catch-finally blocks (use the same as in C #!) )
E. Multiple catch blocks (can contain multiple catch blocks, the order of emission is from special to general, the last is generally the exception class)
Note: The exception exception class is the parent class of all subclasses (in general)
F. Throw an exception (for example: the previous example!) In the same vein ....)
G. Declaring an exception (for example: previous example!). In the same vein ....)
4-3:log4j
A. Logging information using log4j
B. What is a log (logs are primarily used to record important operational information, and valuable information can help users identify and avoid disasters in advance and find the root cause of the event.) )
C. How to use log4j logging details see P81

Object-oriented Java implementation

Related Article

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.