The analysis of object-oriented and class of Dark horse Programmer _

Source: Internet
Author: User

First, object-oriented overview:

Java is an object-oriented programming language, which means that the object is the basis of the language, no object, there is no java. Any function is implemented by object, which is to encapsulate the function into the object and let the object invoke these functions. The idea is to use the data as the first bit, and the method (function or algorithm) as the second. I personally think that this is an optimization of the data, security is higher, the operation of data to a more convenient.

Then the idea of ascension to a state is that all things are objects.

1, the object-oriented understanding:

1) Object-oriented is relative to process-oriented, and is based on process-oriented.

2) Object-oriented is an idea.

3) object-oriented to encapsulate the function into the object, emphasizing the function of the object, the subject is the object, the process is simplified

4) in the actual development, to the object as the core, the first clear good objects, in the implementation of specific functions, or use existing objects.

5) Object-oriented three features: encapsulation, inheritance, polymorphism.

Ii. Overview of Classes and objects

1, class: Can be understood as a blueprint or template for constructing objects, is an abstract concept, on the other hand, the object is a class as a model to create concrete instances, is a kind of embodiment, visualization.

Class: The description of things in life

Object: A concrete implementation of a class, a real entity.

A class is a description of an object.
An object is an entity that exists that can be manipulated.


Requirement: describe a car.
Describes two aspects: properties, behavior.
Properties: Number of tires. Color.
Behavior: Run.

In fact, the classes are defined as members:
The member variable-----property.
The member function-----behavior.

[Java]View Plaincopy
  1. Class Car
  2. {
  3. //1, describes the property.
  4. int num;
  5. String color;
  6. void Run ()
  7. {
  8. System.out.println (num+"::" +color);
  9. }
  10. }
  11. The purpose of describing the object is to create the entity.
  12. Class Cardemo
  13. {
  14. public static void Main (string[] args)
  15. {
  16. //Create an Entity object for car.
  17. new Car ();
  18. }
  19. }

2. Anonymous objects

[Java]View Plaincopy
  1. Class Cardemo
  2. {
  3. public static void Main (string[] args)
  4. {
  5. //Create objects.
  6. Car c = new car ();
  7. C.num= 4;
  8. C.color = "Red";
  9. C.run ();
  10. Car C1 = new car ();
  11. C1.num = 4;
  12. C1.color = "Red";
  13. C1.run ();
  14. Show (c);
  15. Show (C1);
  16. /* 
  17. An anonymous object.
  18. Car c = new car ();
  19. C.run ();
  20. Simplified into:
  21. New Car (). run ();
  22. Creating an object is called only once, and can be done using an anonymous object.
  23. An anonymous object can be passed as an argument.
  24. Car c = new car ();
  25. C.num = 4;
  26. C.color = "Red";
  27. C.run ();
  28. New Car (). num = 5;
  29. New Car (). Color = "Blue";
  30. New Car (). run ();
  31. */
  32. Show (new Car ());
  33. }
  34. public static void Show (Car c)
  35. {
  36. C.num = 3;
  37. C.color = "BLACK";
  38. C.run ();
  39. }



3. Object-oriented-the difference between a member variable and a local variable

[Java]View Plaincopy
    1. /*
    2. 1. Location of source code:
    3. The member variable is defined in the class, and the variable is valid throughout the class access.
    4. A local variable is defined in a method, and the variable is valid only within the method.
    5. 2, in-memory location:
    6. Member variables are stored in the object of the heap memory. The member variable belongs to the object.
    7. Local variables are stored in the method of stack memory. The local variable belongs to the method.
    8. 3, initialize.
    9. Member variables have default initialization. Different initialization values are different depending on the type.
    10. Local variables are not initialized by default. When used, it must be initialized before it can be used.
    11. */


4. Object-oriented-encapsulation and privatization and encapsulation benefits

[Java]View Plaincopy
    1. /*
    2. Encapsulation: Hide implementation Details and provide public access to the outside.
    3. 1, security.
    4. 2, reusability.
    5. 3, the change of isolation. Later applied to layered design.
    6. */
    7. /*
    8. Description Person:
    9. Attribute: Name, age.
    10. Behavior: Speak.
    11. */
    12. Class Person
    13. {
    14. private String name;
    15. Private */personal */ int age;
    16. /* 
    17. The member variable is privatized and the set get method is provided externally, and the reason for its access is that the property can be controlled.
    18. The Private keyword private is a permission modifier that can only be decorated with members (member variables, member functions).
    19. Note: Private is only one form of encapsulation!
    20. */
    21. public void SetName (String s)
    22. {
    23. name = S;
    24. }
    25. Public String getName ()
    26. {
    27. return name;
    28. }
    29. public void Setage (int a)
    30. {
    31. / * You can add robustness to your judgment.
    32. if (a<=0 | | a>120)
    33. Throw
    34. */
    35. age = A;
    36. }
    37. public int getage ()
    38. {
    39. return age;
    40. }
    41. void Speak ()
    42. {
    43. System.out.println (name+":" +age);
    44. }
    45. }
    46. Class Persondemo
    47. {
    48. public static void Main (string[] args)
    49. {
    50. Person p = new person ();
    51. P.name = "Eastern evil";
    52. P.age =-20;
    53. P.setname ("eastern Evil");
    54. P.setage (20);
    55. P.speak ();
    56. }
    57. }

The analysis of object-oriented and class of Dark horse Programmer _

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.