Java-encapsulation and java Encapsulation

Source: Internet
Author: User

Java-encapsulation and java Encapsulation

  • Encapsulation
  • Java package

Note: by default, java automatically imports classes in the same package.

Two classes with the same name as Telephone are defined as follows:

Package com. test; public class Telephone {private double screen; public double getScreen () {return screen;} public void setScreen (double newScreen) {screen = newScreen;} public Telephone () {System. out. println ("com. test. telephone construction method without Parameters ");}}

 

package com.test.second;public class Telephone {    public Telephone() {        System.out.println("com.test.secont.Telephone");    }}

Demo. java

package com.test;import com.test.Telephone;public class Demo {    public static void main(String[] args) {        Telephone phone = new Telephone();    }}

Running result:

Com. test. Telephone construction method without Parameters

package com.test;import com.test.second.Telephone;public class Demo {    public static void main(String[] args) {        Telephone phone = new Telephone();    }}

Running result:

Com. test. secont. Telephone

  • Access Modifier
Access Modifier This class Same package Subclass Others
Private √      
Default √ √    
Protected √ √ √  
Public √ √ √ √
  • This keyword

This indicates the current object. this. Attribute operates on the attributes of the current object; this. method calls the method of the current object

  • Internal class

An internal class is a class defined in another class. The class that contains the internal class is called an external class.

Role of internal classes:

  • Member internal class

  

Note:

  

  • Static internal class

  

  • Internal method class

The internal class of the method is the method in which the internal class defines the external class. The internal class of the method is only visible inside the method.

Note: because the internal class of the method cannot be used outside the external class, the internal class of the method cannot use the access controller and static modifier.

Package com. test; public class Demo12 {public void show () {final int a = 12; int B = 13; class MInner {int c = 2; public void print () {System. out. println ("variable a =" + a in the external class method); System. out. println ("variable B =" + B in the external class method); System. out. println ("variable c =" + c) ;}} MInner mi = new MInner (); mi. print ();} public static void main (String [] args) {Demo12 demo = new Demo12 (); demo. show ();}}

Running result:

Variable a = 12 in the external Class Method
Variable B = 13 in the external Class Method
Variable c = 2 in the internal class

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.