Basics of Java Classes and objects (note)

Source: Internet
Author: User
Tags modifiers naming convention uppercase letter volatile

Packaging:

The encapsulation protects the object from direct access to the internal details of the object, and the next encapsulation also protects the client from the side effects of the partial change in the object implementation, that is, the change in the implementation will not affect the client's change. Private code and data can only be accessed by other parts of the object itself and cannot be accessed by any part of the program outside that object. When code or data is public, although they are defined in the object, other parts of the program can also be accessed.


Inheritance embodies a kind of (is-a) relationship between classes. The relationships between classes are also composed, correlated, and so on.

Modifiers for the class:

The access modifier for a class can be public or default. If the class is decorated with public, the class is called a common class, and the public class can be used by any class in the package. Without the public modifier, a class can only be used by other classes in the same package. If the class uses the abstract modifier, the class is abstract, and the abstract class cannot be instantiated, that is, the object of the class cannot be created. If the class is the final class with the final decoration, the final class cannot be inherited.

Extends superclass
If a class inherits a class by using extends to indicate a superclass of that class, superclass is the superclass name, which defines which class the class inherits from. If the class is defined without indicating the inherited superclass
, then it automatically inherits the object class, which is the root class of java. Because Java only supports single inheritance, a class can have at most one superclass.

Implements interfacenamelist this option defines which or which interfaces the class implements. A class can implement multiple interfaces, and if multiple interfaces are implemented, the middle of the interface name is separated by commas.

The definition of a class consists of two parts: the class declaration and the definition of the class body.

1. The general format for class declaration class declarations is: [Public][abstract|final] class ClassName [extends superclass] [implements interfacenamelist]{

Member Variable declaration

Member method declaration}

Description

(1) The access modifier for the class's modifier class can be public or default. If the class is decorated with public, the class is called a common class, and the public class can be used by any class in the package. Without the public modifier, a class can only be used by other classes in the same package. If the class uses the abstract modifier, the class is abstract, and the abstract class cannot be instantiated, that is, the object of the class cannot be created. If the class is the final class with the final decoration, the final class cannot be inherited.

(2) Class ClassName definition uses the class keyword, ClassName is the class name, and the class name is determined by the class's definition. The generic class name begins with an uppercase letter, such as Circle, Employee, MyPoint, ComplexNumber, and so on, which is the Java class naming convention (not required).

(3) extends superclass if a class is to inherit a class by using extends to indicate a superclass of that class, superclass is the superclass name, which defines which class the class inherits from. If the class is defined without indicating the inherited superclass, it automatically inherits the object class, which is the root class of java. Because Java only supports single inheritance, a class can have at most one superclass. The concept of inheritance is described in section 7.1.

(4) Implements interfacenamelist This option defines which or which interfaces the class implements. A class can implement multiple interfaces, and if multiple interfaces are implemented, the middle of the interface name is separated by commas.

2. Definition of member variables

The end of a class declaration is a pair of curly braces, and the part enclosed in braces is called the class body. The class body typically defines two parts: member variables (member variable) and member methods (member method). member variables and member methods are called members. A member variable provides the state of a class and an object, and the member method implements the behavior of the class and object. The declaration format of the member variable is: [Public|protected|private][static][final][transient][volatile] Type variablename[=value];

Description

(1) The access modifier for the variable public|protected|private is the access modifier for the variable. Public variables, which are modified with public, can be accessed by any method, and variables modified with protected are called protection variables, and protection variables can be accessed by classes or subclasses in the same package; Access modifiers are not used, and the variable is accessible only to classes in the same package A variable that is modified with private is called a private variable, and a private variable can only be accessed by the method of the same class.

(2) instance variables and class variables if the variable is decorated with static, the variable is called a class variable, and a class variable is also called a static variable. Variables that are not modified with static are called instance variables.

(3) Variable type and variable name type variablename is used to specify the types and variable names of member variables. The type of the member variable can be any Java data type, including the base data type and the reference data type.

(4) A variable that uses the final modifier is called the final variable, also known as the identifier constant. Constants can be assigned an initial value at the time of declaration, or they can be assigned the initial values, and once assigned, they cannot be changed.

(5) Variables modified with transient are called temporary variables. Temporary variables are not stored as part of a persistent state when the object is serialized.

(6) Variables modified with volatile are called shared variables. In multi-threaded programs, shared variables can be modified asynchronously.

3. Definition of Member methods

Another important ingredient in the class body is the member method. method is used to implement the dynamic characteristics of an object and is an operation that can be done on the object of the class. The definition of a member method includes the declaration of the method and the definition of the method body, the general format is as follows: [public|protected|private][static] [final|abstract][native][synchronized] ReturnType methodName ([paramlist]) [Throws exceptionlist]{//method body}

Description

(1) Method return value and method name Mehtodname are method names, each method must have a method name. ReturnType is the return value type of the method, and the return value type can be any data type, including the base data type and the reference data type. If a method does not return a value, the returntype should be void. Example: public void Setradius (double R)

(2) The method parameter after the method name is a pair of parentheses, in parentheses is the method's parameter list, the declaration format is: type paramName1 [, type paramName2 ...] types are parameters, ParamName is the parameter name, here the parameters are called formal parameters. The method can have no parameters, or it can have one or more parameters. If there are multiple parameters, the declaration of the parameter is separated by commas. For example: public void MethodA (String s, int n) The method declares two parameters and must provide the corresponding actual arguments when calling the method.

3) Access modifiers public, protected, and private are access modifiers for the method. The private method can be called only in the same class, the protected method may be called in the same class, in the class in the same package, and in subclasses, and the public-decorated method can be called in any class. A method, if the default access modifier, is said to be accessible to the package, which can be accessed by methods of the same class and classes in the same package.

(4) Instance methods and class methods are called instance methods without static modification, and static-modified methods are called class methods. For the use of the static modifier, see Section 4.4.

(5) Final and abstract methods are called final methods by final modification, and the end method cannot be overwritten. The override of a method is related to inheritance. The method of abstract modification is called an abstraction method.

(6) Synchronized and native modifiers are called synchronous methods by synchronized. The synchronization method is mainly used to develop multithreaded programs. For multithreading please refer to the contents of chapter 13th. Methods that are decorated with native are called local methods, which are used by local methods to invoke functions written in other languages, such as the C language.

7) Declaring a method throws an exception if the method itself throws an exception that does not handle it, you can declare the method to throw an exception. The declaration of the exception uses the throws keyword, followed by a list of the exception names. See chapter 8th for exception handling.

Tip: In a class body, it is often necessary to define a constructor method for a class that is used to create a new object. Some experts think that construction methods are not methods, and they are not members of a class.

Can finally write the first Java program about classes, La La La &

 Packagedemo; Public classDemo { Public Static voidMain (string[] args) {Circle cc; CC=NewCircle (); Cc.setradius (10); System.out.println ("RADIUS =" +Cc.radius); System.out.println ("Perimeter =" +Cc.perimeter ()); System.out.println ("area =" +Cc.area ()); }}classCircle {Doubleradius;  Public voidSetradius (DoubleR) {Radius=R; }     Public DoubleGetradius () {returnradius; }     Public Doubleperimeter () {returnMath.PI * 2 *radius; }         Public DoubleArea () {returnMath.PI * RADIUS *radius; }}

Basics of Java Classes and objects (note)

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.