Java (13) Abstraction and encapsulation

Source: Internet
Author: User

I. Brief description of the steps to abstract classes from the real world

First: Find the classification (analyze the Class)
Second: Find out the characteristics of the class (analyze the related properties of the class)
Third: Find out the behavior of classes (methods for analyzing classes)

Ii. Constants (variable values that often occur)

2.1 Syntax:

public static final data type constant name = constant value;
Note: 1.final final, modified variables cannot be modified
2. All letters of the variable name are capitalized, and multiple words are split with an underscore (_).
For example:public static final String Sex_male = "Q Tsai";
Access to constants: class name. Constant Name

Example:

1  Public classExample1 {2String name = "Anonymous";//Nickname3     intHealth = 100;//Health Value4     intLove = 0;//Degree of intimacy5      Public Static FinalString sex_male= "Female";6      Public Static FinalString Sex_female = "Male";7String sex = Sex_male;//Sex8      Public voidji () {9System.out.println ("Confession of the pet: \ n My Name" + This. name+ ", health value is" + This. HealthTen+ ", and the intimacy of the host is" + This. love+ ", I am a" + This. sex+ ". "); One     } A}
Penguin Class
1  Public classText1 {2      Public Static voidMain (string[] args) {3Example4 Qi =NewExample4 ();4System.out.println ("The Sex of the first penguin is:" +qi.sex);5Qi =NewExample4 ();6Qi.sex =Qi. Sex_female;7System.out.println ("The Sex of the second penguin is:" +qi.sex);8Qi =NewExample4 ();9Qi.sex =Qi. Sex_male;TenSystem.out.println ("The Sex of the third penguin is:" +qi.sex); One     } A}
Test Class

The result of the operation is:

Three. Construction method (special method, used to create object, initialize value)

3.1 Syntax:

public class name (argument list) {
Method body
}
A. Use
Class Name Object name = new constructor method name (parameter);
Dog d = new Dog ();
B. Differences from the general approach
1, the construction method does not return a value type;
2, the method name of the construction method is the class name, the general method can take arbitrary
3, constructor method invocation: New constructor Method name (parameter);
Generic method Invocation: Object name. Method name (parameter);
C. The role of construction methods
1, creating a Method object
2, Initialize value (attribute value);
Note: When you do not write a constructor, Java provides the default parameterless constructor
For example:public Dog () {
}

Example 2:

1  Public classexample2{2String name = "Anonymous";//Nickname3         intHealth = 100;//Health Value4         intLove = 0;//Degree of intimacy5String sex = "Q Tsai";//Sex6          PublicExample6 () {7Name = "Nan nan";8Love =20;9sex = "Q sister";TenSystem.out.println ("Execution construction method"); One         } A          Public voidji () { -System.out.println ("Confession of the pet: \ n My Name" + This. name+ ", health value is" + This. Health -+ ", and the intimacy of the host is" + This. love+ ", I am a" + This. sex+ ". "); the         } -          Public Static voidMain (string[] args) { -Example6 Qi =NULL; -Qi =NewExample6 (); + Qi.ji (); -         } +}
Non-parametric construction method

The result of the operation is:

Iv. Method Overloading

The parameter list is not the same as the method name
Note: overloading is independent of the return value of type and access modifiers

1  Public classExample8 {2String name = "Anonymous";//Nickname3     intHealth = 100;//Health Value4     intLove = 0;//Degree of intimacy5String sex = "Q Tsai";//Sex6      PublicExample8 () {7Name = "Nan nan";8Love =20;9sex = "Q sister";TenSystem.out.println ("Execution construction method"); One     } A     //two methods for constructing parameters -      PublicExample8 (String name,string sex) { -          This. Name =name; the          This. Sex =sex; -     } -      PublicExample8 (String name,intHealthintlove,string Sex) { -          This. Name =name; +          This. health=Health ; -          This. Love =Love ; +          This. Sex =sex; A     } at      Public voidji () { -System.out.println ("Confession of the pet: \ n My Name" + This. name+ ", health value is" + This. Health -+ ", and the intimacy of the host is" + This. love+ ", I am a" + This. sex+ ". "); -     } -      Public Static voidMain (string[] args) { -Example8 Qi =NULL; inQi =NewExample8 (); - Qi.ji (); toQi =NewExample8 ("Ya Ya", "Penguin"); + Qi.ji (); -Qi =NewExample8 ("Sister", 80,20, "Q Tsai")); the Qi.ji (); *     } $}
Method Overloading

The result of the operation is:

Six, Package

6.1 Effects: Hidden classes do not implement details;
6.2 Steps:

1. A variable with the private modifier cannot be accessed outside the class, while the public-decorated set and get methods access
2. Write the appropriate access control statements in the set method to avoid any assignment that does not meet the actual requirements
The properties are set to private permissions, so that the properties are visible only within the class.
Use the Ste method and the Get method of the public permission to access the property.
In the Set method, the scope of the input property value is judged, simply speaking, specifying the value of a thing and making judgments with set
6.3 This keyword
This: Indicates the current object
Call properties: this. Property name
Calling method: this. method name ();
This (): represents the call constructor.
Note:this () must be written on the first line of the constructor.

 

Java (13) Abstraction and encapsulation

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.