JAVA classes and interfaces

Source: Internet
Author: User

 

Java is an object-oriented programming language (OOP). in Java, object-oriented occurs in the form of classes, while objects are class instances, therefore, to learn Java classes, you must first understand object-oriented. Object-Oriented Programming is different from process language or structured programming design. It has the concept of object. Objects in object-oriented programming are not only data structures, but also methods related to data structures.
I. Class
1. Class declaration and instantiation
Class is the key to object-oriented, so there is no object if there is no class; Class has two advantages: First, it allows encapsulation; encapsulate data and methods to effectively separate the information in this field from other programs. The advantage of encapsulation is that if this class is modified, as long as the methods in this class are not changed, no other programs need to be moved; the second advantage of the class is that inheritance is allowed.
The class consists of two parts. The first part is the declaration of the class variable to the method in the binary classification. The following is a class named test:
Public class test {// declare a class
String name; // declaration of class variables
Changtest () {// Class Method
Name = "text ";
}
}
The modifier "public" is used to declare a class. When declaring a class, you can use the three Annotations: public, final, and abstract.
The public class means that this class can be used and extended by any object and has nothing to do with its package;
The final class indicates that this class does not have any subclass;
Abstract class indicates that this class is an abstract class;
If no modifier exists before the class declaration, the default value is the protected class, that is, only the objects in the same package can use this class.
As mentioned above, a class has the advantage that inheritance is allowed. This shows that the newly created class can use the variables and methods of the created class, in this way, you can build a large class method based on a simple class, which can reduce the compilation of a lot of code and reduce the work of searching and modifying errors in the program; the following example creates a class MyClass that inherits all the variables and methods in the class test:
Public class MyClass extends test {}
The class MyClss is called a subclass, and the class test is called a parent class or a superclass.
After a class is created, you can use new to create an instance of the class. Not all classes need to create their instances, if the methods or variables in the class to be used are static, you do not need to create an instance of this class, however, static methods can only reference static class variables (the out variable of the System class is an exception). The following example creates an instance of the test class:
Test mytest = new test ();
An instance of the class is created to reference the variables or methods in the class. An Instance mytest has been created for the class test. If the class test contains a method changtest (), you can reference it as follows:
Mytest. changtest ();
2. class variables
The variables of the Declaration class are similar to those of the Declaration class. You can follow the modifier before the Declaration class. The variables have the following modifiers:
Public -- variables are visible in all classes; including classes in the package and all subclasses.
Protected-variables can be accessed by all methods in the current class, but are invisible to classes other than the current package.
Private -- variables can only be accessed by all methods in the current class.
Static -- declare this variable as a static variable.
Final -- the modifier indicates that the variable value cannot be modified during running.
Why not declare all variables as global variables? There are two reasons: the first is that this will waste a lot of memory; the second is that if all variables are declared as global variables, it will make the program difficult to read.
There are two special variables in the class: this and super;
You need to use this variable in the following two cases:
There are two variables with the same name in the class, one of which belongs to the current class and the other belongs to a specific method.
When a class needs to pass the class as a parameter to a method.
The super variable must be used in the following cases:
When overwriting a method, you sometimes want to use the code in the old method. This is to use the super variable to obtain the original code.
3. sometimes a lot of classes need to be created for a project. These class files are as messy as hard disks without subdirectories or folders. To facilitate management, a package is introduced, in this way, you can put related classes in a package, and the package is like a folder or subdirectory. To make a Class A member of a package, you must use package to declare it. Note that the package statement must be the first line of the file, the package statement must contain no space or comments. For example:
Package packagename;
To reference the class in packagename, use the following method:
Import packagename .*;
The preceding command references all classes in packagename. If you reference only a class in this package, you can use the following method:
Import packagename. classname;
Maybe someone will ask if it is not good to reference the class in the entire package? Why do we need to specify a class? The reasons are as follows:
First, when you reference the entire package that is not on the local machine, the program will drag all the files in the package over the network. If this package has thousands of classes, and you only use a few of them, it will waste a lot of resources.
If the referenced two packages contain shared file names (for example, pack1 contains a class named classname, and pack2 also has a class named classname, if you use import pack1 .*; import pack2. *), your system will crash.
4. The following uses an example to describe how to create an instance and a class:
**************************************** **************************************** **************
// Declare that the class tools belongs to the package mypack.
Package mypack;
// Declare a public class tools
Public class tools {
Public tools (){
}
/**
* Statement class method replace ()
* This method searches for a specific string in a string. If yes, replace the string with another string.
* If the replaced string is found, the original string is returned if not found.
* Replace (entire string, replaced string, replaced string)
* It is worth noting that because I use while, if the original string is "ababcdcd", to replace the string "AB" with "e", the output is "eecdcd"
* If you only want to replace the first string found in the original string, replace "while" with "if ".
*/
Public String replace (String allstr, String replstr1, String replstr2 ){
While (allstr. indexOf (replstr1 )! =-1)
Allstr = allstr. substring (0, allstr. indexOf (replstr1) + replstr2 + allstr. substring (allstr. indexOf (replstr1) + replstr1.length ());
Return allstr;
}
Public static void main (String [] args ){
// Generate a tool-like instance t
Tools t = new tools ();
// Use the tools-like method replace () to replace the string "abcd" with "adcd" and output it to the console
System. out. print (t. replace ("abcd", "B", "d "));
}
}
**************************************** **************************************** ***************
Ii. Interface
1. as mentioned above, an important feature of a class is inheritance, but a class can only have one parent class. To implement multiple inheritance in C ++, interfaces must be used; interfaces are very similar to classes. interfaces are used to define functions that are not defined in these classes. By setting these methods in interfaces, you can describe common features, as for how to implement it, it is a matter of the class itself. Because of this feature of the interface, it means that only abstract methods can be defined in the interface, instead of method bodies. This is in violation of the class definition, this causes the interface to be unable to expand the class. Another difference between an interface and a class is that the variable declared by the interface must be a static variable. The following statement defines an interface:
Public interface interf
It is worth noting that if an extended interface is used, all methods of the old interface and the new interface must be overwritten. The method name and parameter must be consistent with the old method when overwriting A method. If

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.