Java uses classes to construct its own data types, which are data encapsulation of a class of data and behavior, and can achieve low-coupling function;
Java
Note: The use of classes is also a way for us to define our own data types, so the structure, the common body is the same, is to deal with the data and the method used!
Class storage problem: Java source code files are class-centric, a class of the definition of the source must be in only one file implementation; a "file name. Java" file name must match the class name that is decorated in the public class in the file. Java syntax also stipulates that there must be only one public class in a source file , and public is an access modifier, and public means that my class can be used by other classes, anywhere.
There is a new understanding of classes from a data type perspective: Classes represent a completely new type of complex data by encapsulating other types of data into their own classes as their own properties.
How to construct a class:
The method name and class name are the same
Construction method The Java platform automatically calls the construction method when the object is created
Construction method does not return a value
class variables and Class methods
A class variable is a variable shared by all objects of that class, and any object of that class accesses it by the same memory address, and the same variable is modified when any object of that class modifies it. Class variables can be maintained by ordinary methods (non-static methods) of the object;
Class variables are public properties related to the class
Instance variables are attributes that belong to each individual object
Class variables can be accessed directly through the class name. Class variable name
Definition syntax
Access modifier static data type variable name
How to access class variables
Class name. class variable name or object name. Class variable name (not advocated)
Program has code area and stack area
Class method
class method Definition Form
Access modifier static return value type function name (parameter list)
A non-static variable cannot be accessed in a class method.
public static int Gettotalfee ()
{
age++; Object properties, non-static variables
return totalfee;
}
But normal methods can access static variables (class variables) and Object properties (member variables)
Invocation mode
Accessed directly through the class name, class method name.
Class methods are class-related, public methods
Method of instance method belonging to each individual object
Default rules in Java: Class variables are maintained by class methods;
Method overloading
1, the same way
2, need parameter type, number of parameters, parameter order at least one different
3, the parameter return value and method modifier can be different, but not as the basis for determining overloading:
For example, two functions, parameter types, numbers, or order are consistent, the method name is the same, simply the return value is different or the method modifier is different then this does not constitute a method overload. The compiler will make an error.
Method overrides
A method override means that a subclass has a method and a method of the parent class that returns a value type + method name + parameter list; Then the subclass method overrides this method of the parent class.
The inheritance of a class can be used to reuse code, but there is also a problem, the method of implementing the same function in the class, the parent class and the subclass implementation form is not the same, if simply inheritance cannot be personalized.
A method override is a re-implementation of a parent class method in a subclass, calling the method of a subclass directly after calling it in a subclass instead of calling the parent class method. Method overrides require access modifiers + return type + method name + parameter list all the same;
In general, the method covers two points
1, the return type of the subclass method, the method name, the parameter list and the method of the parent class are exactly the same in these respects
2. The subclass method cannot narrow the access rights of the parent class method
Java Naming conventions
Local variable name (locally Variable name)
1, the local variable name generally contains more than one word, generally does not contain verbs; the first letter is lowercase ; for example, a Mycar
2, the variable name can highlight the role of variables, you should highlight the variable type; for example, a reference to a car type variable can be named car
3, the array name is best followed by the S, indicating that many
4, for the Boolean type variable is used to start with IS
Member variable (Member Variable Name)
It is customary to add the M letter to the variable name to represent the member variable. Represents member
Class name
The class name is customarily composed of one or several words, with the first letter of each word capitalized
Methods Name (method name)
The method name represents the function of the method, usually beginning with a verb, with the first letter lowercase:
Parameter name (parameter name)
Parameter naming rules and local variable naming rules are similar, but in order to distinguish a variable is not a parameter, you can precede the parameter name with a P prefix, this p represents parameter
Package Name
The package name is usually lowercase
The package name is usually composed of a word, its fully qualified name should be able to express the function of the class in the package, for example, the common package is best placed in the call car package, so the fully qualified name Car.common can represent the common package is stored in the ordinary car class
Abstract classes and Interfaces
abstract class: Resolves the uncertainty of the parent class method, such as a method instance of the parent class, but the subclass has a different way of implementing the method and needs to overwrite the method. Then the method of the parent class will never be called. In this case we can use abstract to modify the parent class method, then this method is an abstract method, if the class is decorated with abstract, then the class is called an abstract class.
Abstract classes are the more important classes in Java,
Precautions
1. Classes that are modified with abstract so that class is abstract class
2, with the method of abstract modification, then this method is an abstract method, the implementation of this abstract method must be implemented in the subclass, can not be defined in the parent class abstract method implementation. must also be implemented.
3, the abstract method in the actual programming with not much, but the interview asked more:
4. Abstract class cannot be instantiated
Animal a =new Animal (); This is wrong.
5, abstract class can have no method of abstract modification
6. Once the class contains a method modified with the abstract modifier, the class must be declared as an abstract class
7, abstract class inside the abstract method can not have the body: for example
Abstract public void Cry () {}
This is wrong.
When the parent class inherits from a class is an abstract class, then we need to implement all the abstract methods within the parent class.
abstractclass Animal{intabstractpublicvoid cry();}class Cat extends Animal{publicvoid cry() { }}
Interface:
First illustrate:
Real-Life USB interface Slots: We do not have to worry about any one device USB interface and the computer is not connected, because the USB slot device manufacturers and peripheral equipment manufacturers adhere to a common protocol, such as USB size, cable, etc., but the inside of the device is not the same.
Use the background: Java is a single-inheritance object-oriented programming language, and class implementation interface programming can implement multiple interfaces, which to a certain extent, to compensate for the limitations of class expansion.
Defined:
Interface is to give some methods without content, encapsulated together, to a class to use the time, and then according to the specific circumstances of the implementation of these methods.
Grammar:
class 类名 implements 接口{ 实现所有接口方法; 变量;}
An important principle: When a class implements an interface, it requires that the class implement all the methods of this interface,
Summary: interface is a more abstract class than abstract class, abstract class can have method body (for example, there is no method body with abstract modification method), but all methods of the interface have no method body, the interface realizes the design idea of multi-state and high cohesion and low coupling of programming.
Considerations for Interfaces
Interfaces cannot be instantiated, such as USB USB = new USB (); All of these are wrong for interface instantiation because interfaces are encapsulation of methods that have no content.
None of the methods in the interface can have method bodies, such as public void KK () {}, which are all wrong.
A class can implement multiple interfaces,
You can use variables in an interface, there are two points to note about the variables in the interface:
The variables in the interface are essentially static,
In Java practical development, used variables are often defined in the interface and used as global variables. Access form: interface name. variable Name
An interface cannot inherit a class, but can inherit other interfaces
Abstract class and interface comparison
Different:
Abstract classes can have a method of the body of a function, but if the method is modified with an abstract, then there is no method body.
None of the interfaces can have an implemented body,
Same:
Methods that are modified with the abstract and the methods inside the interface must be implemented in subclasses or implementing interface classes
Inheritance and implementation interface comparison
Java is a single inheritance, that is, a class can only inherit a parent class, such a mechanism can guarantee the purity of the class, more than C + + in the multi-inheritance mechanism is simple, the problem is the extension of sub-class function has some impact, but the implementation of interfaces can implement multiple interfaces, to a certain extent, to compensate for the sub-class extension
Another point is that inheritance is hierarchical, if Class 1 changes, then the other classes under Class 1 will be affected, and the interface can avoid this problem, because the interface is only for the implementation of the interface class, of course, if the implementation of the interface class is inherited by other subclasses, then implement interface class implementation of the interface method in the subclass is also available. For example, in Class 1 I implemented an interface, then Class 2 3 4 5 can be my Class 1 implementation of the interface class method;
Final
Final can modify variables and methods
A variable that uses the final modifier must be assigned an initial value at the time of creation/declaration, and is deterministic, and cannot be changed after the assignment.
For reference type variables that are final decorated, Java also requires that the initial value must be given at the time of declaration.
Final modified variables are called constants, and final variable names are named
Xxx_xxx_zzz
Possible use scenarios
When you do not want a method of the parent class to overwrite the quilt class, use final to modify the method
When you do not want the variables in the class to be modified, the final decoration, and must be initialized
When you do not want the class to be inherited from final class akk{}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Understanding of Java Classes