s2/java/01-Abstraction and encapsulation

Source: Internet
Author: User

Constants: In C # , constants are used to store unchanging data, constants are not changed during program operation, can only be referenced, cannot be re-assigned, that is, the value can only be modified at the time of definition. There is also desolation in Java, which is decorated with the final keyword, and the constant name is usually capitalized.

The naming conventions for class names, attribute names, method names, and constant names are as follows:

1. The class name has one or several words, the first letter of each word is capitalized, such as Dog,stringbuffer.

2. The attribute name and method name have one or several words, the first letter is lowercase, the other words are capitalized, such as Health,stuname,println (), GetMessage ().

3, the constant name consists of one or several words, all uppercase letters, such as PI,sex_male.

The construction method is a special method, and when there is no Penguin () method, the system provides an empty Penguin () method .

The constructor method has the same name as the class name and no return value type. The primary function of the construction method is to perform some initialization operations when the object is created, such as assigning an initial value to a member property.

Overloading of construction methods: The construction method of the two parameters---------the method of constructing the parameter without parameter--------the construction method of four parameters. Method names are the same, and parameter lists are different, which is called overloading of the constructor method.

as in Java and C # , when no constructor is given to a class, the system provides a default constructor that is empty for a method body without parameters. Once a custom construction method is provided, the system will no longer provide this default constructor method. If you want to use it, the programmer must add it manually.

In Java , the destruction of objects does not need to be done by the programmer, but is automatically implemented in the background by the Java Virtual machine's garbage collector, so only learning to create objects without learning how to destroy objects.

If the same class contains two or more than two methods, their method name is the same, the method parameter number or parameter type is different, it is said that the method is overloaded. Both member methods and construction methods can be overloaded.

Method overloads are judged based on :

1, must be in the same class.

2, the method name is the same.

3, parameter list (method parameter number or parameter type) is different.

4, there is no relationship to the method return value and method modifiers.

When you use this to invoke other constructor methods of itself, you can only act as the first statement.

Static can use classes to decorate properties, methods, and code blocks. static modified variables are owned by this class, that is, all objects created by this class share the same Static variable. static modified properties and methods are commonly referred to as class properties (class variables), class methods. Properties and methods that do not use static adornments belong to a single object, often referred to as instance properties (instance variables), instance methods.

In the process of loading the class, the memory allocation of the static variable is completed, and then the static block is executed before the object is created.

Ray properties, class methods can be accessed through the class name and object name, instance properties, instance methods can only be accessed through the object name.

Class methods can only access class properties and other class methods.

The This and Super keywords cannot be used in static methods .

Static constants: Staticfinal String sex_mmale= "Q Tsai ";

A Static variable cannot be defined in a method , that is, a class variable cannot be a local variable.

The construction method does not return a value type. If so, it is not a construction method, but a member method with the same name as the constructor method.

Private: Member variables and methods can only be accessed in the class in which they are defined, with class visibility.

Default: member variables and methods can only be accessed by classes in the same package, with packet visibility.

Protected: Can be accessed by classes in the same package, accessed by subclasses of different packages in the same project

Public: Can be accessed by all classes in the same project, with project visibility, which is the maximum access rights.

Using this to call the overloaded constructor method, which can only be used in a constructor method, must be the first statement of the constructor method.

This can only invoke instance variables, instance methods, and construction methods because this refers to its own reference within the object.

This cannot call a class variable and a class method, nor can it call a local variable.

s2/java/01-Abstraction and encapsulation

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.