Visibility of domains, methods, classes in Java

Source: Internet
Author: User

  1. Polymorphism is a special problem in the domain. I can not understand the Chinese version of the book directly called the domain, read the original English, the original writing is fields, direct translation Although correct, but the problem of the variable is not a domain. Specifically checked what's the meaning of field in Java? Many people think of it as a range enclosed in curly braces. In fact, there is a kind of thing inside the frame--domain model, which is also called domain, domain models.

    and find this post.

    What's a field in Java?

    A field is an attribute. A field may is a class ' s variable, an object's variable, an object ' s method ' s variable, or a parameter of a function.

    [Java]View PlainCopy
    1. Class bike{
    2. static int bikes;
    3. int gear;
    4. int cadence;
    5. void Create ( int newgear, int newcadence) {
    6. Bikes = bikes + 1;
    7. Gear = Newgear;
    8. Cadence = newcadence;}
    9. int GetSpeed () {
    10. int speed = gear*cadence*5*3.141;
    11. return speed;
    12. }
    13. }

    ' Bikes ' is a class ' s variable (class variable) (static field).
    The ' gear ' and ' cadence ' could is an object's variables (instance variables) (non-static fields).
    ' Speed ' was an object ' s method ' s variable (local variable).
    ' Newgear ' and ' newcadence ' are parameters of a function (parameters).

    field, a field is a property that can be a class variable, an object variable, an object method variable, or a parameter to a function. (Supplemental, class's variables, instance variables and static variables of classes are called class ' s variables, generic variables, also known as class variables or data fields, actually translated into properties can also, class properties, sounds not weird, from Baidu Encyclopedia).

    [Java]View PlainCopy
    1. Class bike{
    2. static int bikes;
    3. int gear;
    4. int cadence;
    5. void Create ( int newgear, int newcadence) {
    6. Bikes = bikes + 1;
    7. Gear = Newgear;
    8. Cadence = newcadence;}
    9. int GetSpeed () {
    10. int speed = gear*cadence*5*3.141;
    11. return speed;
    12. }
    13. }


    Bikes is a class variable (static field).

    Gear and Cadence are object variables (instance variables) (non-static domains).

    (Here is a little bit of a contradiction, in fact, according to the encyclopedia, so bikes, gear and cadence are class variables, bikes is a static variable in the class variable, and gear and cadence are the instance variables in the class variable.) )

    Speed is the variable (local variable) of the object method.

    (see no, local Variable,java does not appear gobal variable, global variables, to say that the scope of the class variable is the same as the global variable, but not called).

    Newgear and Newcadence are parameters (parameters) of a function (method).

  2. **
  3. * public class, visible in all packages
  4. * @author OOS
  5. *
  6. */
  7. Public class Publicclass {
  8. /** 
  9. * Public domain, visible in all sub-classes
  10. */
  11. public int publicnum;
  12. /** 
  13. * Private domain, current class visible
  14. */
  15. private int privatenum;
  16. /** 
  17. * Protected domain, visible in all subclasses
  18. */
  19. protected int protectednum;
  20. /** 
  21. * Default domain, visible in sub-class of current package
  22. */
  23. int defaultnum;
  24. /** 
  25. * Public method
  26. * Available: All classes
  27. * Inheritable: All sub-classes
  28. */
  29. public void Publicmethod () {};
  30. /** 
  31. * Private Method
  32. * Available: Current class
  33. * Inheritable: None
  34. */
  35. private void Privatemethod () {};
  36. /** 
  37. * Protection method
  38. * Available: Current package
  39. * Inheritable: All sub-classes
  40. */
  41. protected void Protectedmethod () {};
  42. /** 
  43. * Default method
  44. * Available: Current package
  45. * Inheritable: Sub-class of the current package
  46. */
  47. void Defaultmethod () {};
  48. /** 
  49. * Private class, only visible in the class in which it is defined
  50. * @author OOS
  51. *
  52. */
  53. private class privateclass{
  54. }
  55. /** 
  56. * Protection class, visible only in current package, current class, and so subclasses
  57. * @author OOS
  58. *
  59. */
  60. protected class protectedclass{
  61. protected Protectedclass ()
  62. {
  63. }
  64. }
  65. /** 
  66. * Default class, only visible in current package
  67. * @author OOS
  68. *
  69. */
  70. class Defaultclass {
  71. }
  72. }

Test class Currentpackagetest in the same package as Publicclass, as follows:

[Java] View plaincopy
  1. Public class Currentpackagetest extends publicclass{
  2. public static void Main (string[] args)
  3. {
  4. Publicclass pc = new Publicclass ();
  5. //Current package can use protection method and default method
  6. Pc.protectedmethod ();
  7. Pc.defaultmethod ();
  8. //Protection class visible in current package
  9. Publicclass.protectedclass Protectedclass = new Publicclass ().   New Protectedclass ();
  10. //default class is visible in current package
  11. Defaultclass Defaultclass = new Publicclass ().   New Defaultclass ();
  12. }
  13. Public currentpackagetest ()
  14. {
  15. System.out.println (super.publicnum);
  16. System.out.println (super.protectednum);
  17. //publicclass and current similarity belong to a package, so defaultnum can inherit
  18. System.out.println (super.defaultnum);
  19. }
  20. }


Unlike Publicclass, a test class in a package is Otherpackagetest, as follows:

[Java] View plaincopy
    1. Public class Otherpackagetest extends publicclass{
    2. public static void Main (string[] args)
    3. {
    4. Publicclass pc = new Publicclass ();
    5. //publicclass does not belong to the same package as the current class, only public methods can be used
    6. Pc.publicmethod ();
    7. }
    8. Public otherpackagetest ()
    9. {
    10. System.out.println (super.publicnum);
    11. System.out.println (super.protectednum);
    12. }
    13. }

Visibility of domains, methods, classes in Java

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.