Introduction to Java Tutorial (vi) Object-oriented (encapsulation)

Source: Internet
Author: User
# # #面向对象 (Package) # # #

1. Concept: Encapsulation is to hide some of the class information inside the class, not allow direct access to external programs, but through the method provided by the class to implement the operation and access to the hidden information

Advantages: A. Access only by the prescribed method B. Hide instance details of classes for easy modification and implementation

2. Implementation steps:

1) Modify attribute visibility (set to private)

2) Create Getter/setter method (for property read and write)

3) Add the attribute control statement to the method (the property is worth legal judgment)

Example: Setting Student class: two attributes 1) name 2) age

In the student class, add an age-based judgment, if the age is less than 0 or greater than 150, hint: unreasonable data
Add judgment, can not solve the existence of unreasonable data;

Using the keyword private---> Cannot access (the private modified member variable/member method, only accessible in this class, not accessible directly from outside )

3. Package

1) The role of the package: Managing Java files, resolving file conflicts with the same name

Definition Packages: Package name

Note: Must be placed in the Java source program first line package name can be used. Separated, such as: Com.westos.Class

2) packages in the system:

java. (function). (Class)

Java.lang. (Class) Contains the Java language base class

Java.util. (Class) contains various tool classes in the Java language

java.io. (class) contains input and output related function classes

3) Use of the package: using the Import keyword to use classes from other files in a file

Such as: Between us in the keyboard entry in the import package

Note: In Java, the naming conventions for packages are all lowercase letters

Can load all files under a package (import java.util.*) or Shant specific sub-packages (import Java.util.Scanner) when used

4. Access modifiers: The scope of access used to modify properties and methods

PS: Usually we use the public property to modify the method with the private

5.this keywords

1) The This keyword represents the current reference object

This. Property: Manipulate the properties of the current object

This. Method: Method that invokes the current object

2) The This keyword is often used when encapsulating the properties of an object

PS: Generate GET, set method

Source--->generate Getters and Setters

At this point we found that our property names and parameter names were duplicated, in order to differentiate, we added the This keyword, meaning to assign the value of the parameter to the value of the current property

The This keyword can be used not only with attributes, but also if we want to invoke a method in a set method.

Example: Synthesis of previous examples, output results

1. Mobile Phone class

Package Example;public class Phone {private string brand;private int price;private String color;public void SendMessage () {System.out.println ("can message to Andrew");} Public String Getbrand () {return brand;} public void Setbrand (String brand) {This.brand = Brand;this.sendmessage ();} public int GetPrice () {return price;} public void Setprice (int. price) {this.price = Price;} Public String GetColor () {return color;} public void SetColor (String color) {this.color = color;} public void Show (String name) {System.out.println (name+ ' use of the iphone to call Andrew ');}}

2. Student Class

Package Example;public class Student {//define two variables string name; String gender;private int age;//provide method: Output variable public void show () {System.out.println (name+ "   " +age+ ""   +gender);} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} Public String Getgender () {return gender;} public void Setgender (String gender) {This.gender = gender;} The public void setage (int a) {if (a<0| |) is judged for age. a>150) {System.out.println ("The data does not match the actual");} else {age = A;}}}

3. Call

Package Example;public class Ex2 {public static void main (string[] args) {Student x = new Student (); Phone i = new phone (), X.setage (X.setname ("Silvia"); X.setgender ("Lady"); X.show (); I.setbrand ("Apple"); I.setcolor ("Rose gold"); I.setprice (6999); I.show (X.name);}}

Results:

# # # #END # #

Related articles:

Introduction to Java Tutorial (iv) Basic syntax (2)

Introduction to Java Tutorial (v) Basic Syntax (3)

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.