Object-oriented: Encapsulation (ii): Use of packages, static members, Singleton mode cases

Source: Internet
Author: User

Convention: Class name, first letter to uppercase, if more than one word, each first letter is uppercase, the rest of the letter lowercase.

Constant name, with each letter capitalized.

One: the concept of package: Create, use.

1. New Package:

In the top row, no more code can be found before.

Package name;

Note: The package name generally has a named specification. Example: Com.itnba.maya.zza

2. Use the package:

Import package name. *;

Import package name. class name;

For example: Import com.itnba.maya.ccz.*;

Note: Import should not be placed on the top of the package

Two: Constants

Constant. The final type variable is not: const,readonly

There are no real constants in Java. Only the final type of variable can be used as represented.

The final variable, only two places can be assigned, the rest of the place is not placed on the left:

1. You can assign a value when you define it again.

2. The re-purchase function can be assigned values, and if the definition is assigned, it cannot be assigned in the constructor. Includes values that are assigned null when defined, and cannot be assigned in constructors.

Three: static member: (keyword: static, and "static" corresponds to called "instance")

Static members include: Static variables and static methods. Corresponding to this is the instance variable and the instance method.

1. Instance variables and instance methods belong to each individual object. Static variables and static methods are shared by all objects, that is, they belong to

of the class.

2. Static methods can only manipulate static variables and static methods of the current class, and cannot manipulate instance variables and instance methods. The static and static methods of the current class can be called in an instance method.

 PackageCom.inba.maya.zzb; Public classBao2 { Public FinalString aaa= "666"; PrivateString name; Private FinalString sex;  Public StaticString Zhuren;  Public StaticString Getzhuren () {SetName ("6546"); returnZhuren; }     Public Static voidSetzhuren (String zhuren) {Bao2.zhuren=Zhuren; }     PublicBao2 () {sex= "NV"; }         PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }    }/*in this code, when calling Getzhuren with the object name, because the Getzhuren method (static) has a SetName method (instance), because the object shares a static Getzhuren method in the HiPe space, it can be called,
And SetName is also an instance, so it can be called. *//*But when I call Getzhuren with the class name, because the Getzhuren method (static) has a SetName method (instance), when there is more than one name in the HiPe space, it is not possible to determine which object is calling SetName so it cannot be called*/ PackageCom.inba.maya.zza;Importcom.inba.maya.zzb.*; Public classBao1 { Public Static voidMain (string[] args) {Bao2 B21=NewBao2 (); Bao2 B22=NewBao2 (); B21.setzhuren ("Zhang San"); System.out.println (B21.getzhuren ()); B22.setzhuren ("John Doe"); System.out.println (B22.getzhuren ()); System.out.println (B21.getzhuren ()); Bao2.getzhuren (); B21.getzhuren (); System.out.println (B21.getzhuren ()); }}

Singleton model Cases

 PackageCom.inba.maya.zzb; Public classBao3 {String age;
Make a static member variable of the current typePrivate StaticBao3 NL;
Set the constructor to privatePrivateBao3 () { age= "18"; }
Do a static member method of public. Used to instantiate the member variable in the second step and return back Public StaticBao3 Bao () {if(nl==NULL) {NL=NewBao3 (); } returnNL; } PublicString getage () {returnAge ; } Public voidsetage (String age) { This. Age =Age ; }}

Bao3 B1=bao3.bao ();
System.out.println (B1.getage ());

Bao3 B2=bao3.bao ();
System.out.println ();

When I tune in, B2 will not be new to a Bao (), b1,b2 points to the same Bao ()

Single-class mode. Singleton mode. Only one object can be generated. ---case. Private constructor. static methods.

The first step: Set the constructor to private.

Step two: Make a static member variable of the current type.

Step three: Do a static member method of public. Used to instantiate the member variable in the second step and return back.

Object-oriented: Encapsulation (ii): Use of packages, static members, Singleton mode cases

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.