Java-based object-oriented encapsulation

Source: Internet
Author: User

Java-based object-oriented encapsulation

2017-01-15

Packaging

1) encapsulation is to ensure that the inner class is not visible outside the class

2) All attributes must be declared with the Private keyword

3) The property must implement the set and get methods if it is to be accessed externally

For example:

  Properties that use the private declaration are visible only in their own class
1 classbook{2     PrivateString title;//properties that use private declarations3     Private intPrice ;4 }5  Public classPrivateDemo1 {6      Public Static voidMain (string[] args) {7Book BK =NewBook ();8Bk.title = "Java Foundation";//properties that use the private declaration are visible only in their own class9Bk.price = 89.9 ;Ten     } One}

If we want to make our program work properly, we have to find a way to let the external program manipulate the properties of the class.

All properties in a class require the private keyword to be declared, and if the attribute needs to be used externally, the corresponding set and get methods need to be defined.

Setter Method: Setting Property Contents

1      Public void Settitle (String title) {2         this. title = title; 3     }

Getter method: Gets the property contents of the setting

     Public String GetTitle () {        return  title;    }

Construction method

Object Generation Format: 1 class Name 2 object name = 3new 4 class name ();

Class Name: Specifies the type of the object that specifies the instance of the class that the object belongs to (the properties or methods that can be used)

Object name: If you want to use the object, you need to have a name, the object name is unique

NEW: Opens up a fresh pair of memory space, if no new keyword object is not instantiated

      Class name (): A method called the same as the class name, which is called the constructor method

Java in order to ensure the normal execution of the program, then after the compilation of the program for the class to add a parameterless construction method.

Construction method Definition Principle: The method name is the same as the class name, there is no return value declaration.

1 classperson{2      PublicPerson () {//Non-parametric construction method3System.out.println ("**********");4     }5 }6  Public classPrivateDemo2 {7      Public Static voidMain (string[] args) {8Person p =NULL;//declaring Objects9p =NewPerson ();//instantiating an objectTen         /** One * Constructor method is called when instantiating an object A          */ -     } -}

All construction methods are invoked by default when using the keyword new instantiation.

The biggest difference between the construction method and the ordinary method

The constructor method is called only once when instantiating an object.

The normal method is that after instantiating the object, you can call it multiple times

The central role of the constructor method, which sets the property initialization content when the class object is instantiated. The constructor method is prepared for the property initialization.

If a construction method has been explicitly defined in a class, the automatically generated parameterless constructor will not automatically generate the default constructor method. At least one construction method is reserved in a class.

1 classperson{2     PrivateString name;3     Private intAge ;4      PublicPerson (String N,intA) {//Non-parametric construction method5Name =N;6Age =A;7System.out.println ("**********");8     }9      PublicString GetName () {Ten         returnname; One     } A      Public voidsetName (String name) { -          This. Name =name; -     } the      Public intGetage () { -         returnAge ; -     } -      Public voidSetage (intAge ) { +          This. Age =Age ; -     } +      A } at  Public classPrivateDemo2 { -      Public Static voidMain (string[] args) { -Person p =NULL;//declaring Objects -p =NewPerson ("Zhangsan", 30);//instantiating an object -         /** - * Constructor method is called when instantiating an object in          */ -     } to}
     The central role of the constructor method: set initialization for a property when the class object is instantiated


overloading of construction methods

Since the construction method also belongs to the method, then the construction method can be overloaded.
1 classperson1{2     PrivateString name;3     Private intAge ;4      PublicPerson1 () {5System.out.println ("Method of constructing without parameters");6     }7      PublicPerson1 (String N) {8System.out.println ("A method of constructing a parameter");9     }Ten      PublicPerson1 (String name,intAge ) { OneSystem.out.println ("Construction method with two parameters"); A     } -}

  

  Anonymous objects

The real information is stored in the heap memory, if the object that the stack points to is called an anonymous object.
1 classperson2{2     PrivateString name;3     Private intAge ;4      Public voidinfo () {5System.out.println ("anonymous method call");6     }7 }8  Public classPrivateDemo4 {9      Public Static voidMain (string[] args) {Ten         NewPerson2 (). info ();//Anonymous Object Call info () method One         /** A * Objects that do not have a stack memory reference are anonymous objects -          */ -     } the}

  Anonymous objects because there are no references to other objects, anonymous objects can only be used once.

Summarize:
1, the definition of the construction method requires: The method name is exactly the same as the class name, and there is no return value.
2. The constructor method is called when the class object is instantiated.
3, a class must retain a construction method, if not explicitly defined a construction method will automatically generate a non-parameter of what does not do the construction method.
4, the core of the constructor method is that the class object is initialized for the property in the class when it is instantiated.
5, the construction method overload only requires to consider the number and type of parameters.
6. Anonymous objects can only be used once.

Java-based object-oriented 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.