Java program Ape Object-oriented (top)

Source: Internet
Author: User
Tags modifiers naming convention

  Defining classes

  There are two important concepts in object-oriented program design: Class and Object (object, also known as instance, instance).

A class is an abstraction of a batch of objects, or a class can be understood as a certain concept, and an object is a concrete entity.

Classes and objects are the object-oriented core.

The simple syntax for the Java language definition class is as follows:

class class Name {  0 to multiple constructor definitions  ... 0 to multiple member variables  0 to multiple methods}   

above modifiers can be public, final, abstract, or completely omit these three modifiers.

The class name is as long as it is a valid identifier, but for the readability of the program, the Java type must be made up of one or more meaningful concatenating, the first letter of each word capitalized, the other letters all lowercase, and the words do not use any separators between them.

A class definition that can contain three common members: constructors, member variables, and methods.

Static decorated members cannot access members that do not have a static adornment.

A member variable is used to define the state data contained by the class or instance of that class, and the method is used to define the class or instance behavior characteristics or feature implementations of the class. The constructor is used to construct an instance of the class, and the Java language invokes the constructor with the new keyword. A constructor is the fundamental way for a class to create an object, and you cannot create an instance without a constructor. The Java language provides a feature: If the programmer does not write a constructor for a class, the system provides a default constructor for that class.

// Defining member Variables [modifier] Type member variable name [= default value]

Modifiers: modifiers can be omitted, can also be pubic, protected, private, static, final where public, protected, private can only appear one, can and static, final combination to decorate member variables.

Type: Any data type allowed by Java

Member variable name: A valid identifier is available, but readability will suggest the first letter lowercase, followed by the first letter of each word capitalized.

Default value: Define member face You can specify an optional default value.

// Defining Methods [modifier] method returns a value type method name [parameter list]{  one or more executable statements to form the body of the method}

Modifiers: modifiers can be omitted, can also be public, protected, private, static, final, abstract, where public, protected, Privete can only appear one, Abstract and final can only appear at most one, combined with the static modifier method.

method returns a value type: The return value type can be any data type allowed by the Java language, and if the method return value type is declared, the method body class must have a valid return statement. If the method does not return a value, you must use void to declare no return value.

Method Name: The naming convention for method names is basically the same as the naming rules for member variables, but because methods are used to describe the behavior or functionality of the class or instance of that class, it is recommended to start with a verb.

Formal parameter lists: parameter lists are used to define the parameters that the method can accept, and the formal parameter list is composed of 0 groups of parameter type parameters, separated by commas. A parameter list is specified, and the corresponding argument value must be passed in when the method is called.

In the method body, multiple executable statements have a strict order of execution, and the statements in front of the method are always executed first.

Static is a special keyword that can be used to modify members such as methods, member variables, and so on. The member of the static modifier indicates that it belongs to the class itself, not an instance of the class, and usually the static modified member variables and methods are also referred to as class variables, class methods, or static variables, static methods. A normal method that does not use the static modifier, which belongs to a single instance of the class, and not to that class. member variables and methods that are not static modified are often referred to as instance variables, instance methods, or nonstatic variables, non-static methods. Static members cannot access non-static members directly.

The real function of static is to distinguish between member variables, methods, inner classes, and initialization blocks, whether the four members belong to the class itself or to the instance. A member defined in a class, Static equals a flag, a member with a static adornment belongs to the class itself, and no static decorated member belongs to an instance of the class.

Constructors are a special method

[modifier] Constructor name (parameter list) {  // 0 more than one executable statement of constructor execution body }

Modifiers: modifiers can be omitted, or they can be public, protected, priveate one of them

Constructor Name: The constructor name must be the same as the class name.

Formal parameter list: The format of the parameter list is exactly the same as that of the definition method.

The constructor can neither define a return value type nor use void to declare that the constructor has no return value. If you define a return value type for the constructor, or declare with void, the compilation will not go wrong, but Java will treat the constructor as a method-it is no longer a constructor.

  Why can't I define a constructor with void? The constructor is actually a return value, and when the constructor is called with the New keyword, the constructor returns an instance of the class, which can be used as the constructor's return value, so that the constructor's return value type is always the current class without the need for an order return value type. It is important to note, however, that you do not explicitly use return in the constructor to return the object of the current class, because the return value of the constructor is implicit.

  System default Constructor

 Public class persontest{    String name;     int Age     // The system provides the default constructor, which is not a parameter     // persontest () {}     Public Static void Main (string[] args) {                }    }

Java classes generally have the following functions: Defining variables, creating objects, invoking class methods of classes, or accessing class variables of classes.

 The generation and use of objects

The fundamental path to creating an object is the constructor, which invokes the constructor of a class with the new keyword to create an instance of the class.

New Person (); // or  New person ();

The purpose of a Java object is to access the object's instance variables and invoke the object's methods.

If access permission is allowed, the methods and member variables defined in the class can be called through the class or instance. The syntax for a class or instance access method or member variable is: Class. A class variable/method, or instance. An instance variable/method in which a class or instance is the keynote person that accesses a member variable or method of that class or instance.

Static modified methods or member variables, which can be invoked either through a class or by an instance, without a normal method and member variable using the static modifier, can only be invoked using an instance.

The class is human, human is an instance, and the class is not a concrete existence but a characteristic of multiple instances.

  objects, references, and pointers

  A class is a reference type data, so a variable in the program that defines the person type is actually a reference, it is stored in the stack memory, is pointed to an actual person object, and the real person object is in the method heap memory. The reference variable in the stack memory does not actually store the object's member variables, the object's Chenyuan variable data is actually stored in the heap memory, and the reference variable is simply pointing to the object in that heap's memory. Like the C pointer, it stores an address that refers to an object by its address.

When an object is created successfully, the object is saved in heap memory, and the Java program does not allow direct access to the objects in the heap memory, and can only manipulate the object through a reference to that object. They can only be accessed by reference, regardless of the array or object.

Whether an array or an object, when a program accesses a member variable or method that references a variable, it is actually accessing the array referenced by the variable, the member of the object, or the method.

If you want to notify the garbage collection mechanism to reclaim an object, it is good to cut off the reference variable and its connection, and assign the value to null.

  

  

  

Java program Ape Object-oriented (top)

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.