DAY10-all objects-2018-2-2, day10 -- 8-8-2-2
I haven't written it for a long time. Although I have been studying it every day, I have learned less, and the difficulty is gradually increasing. The learning speed is also slowing down. These are the notes that have been accumulated over the past few days. From the first contact with the object, to understanding the object slowly, we are still in a state to be further understood. Everything is an object. Isn't it that a partner without an object doesn't have to worry about it?
All objects
Finally, we arrived at the object. Object-Oriented Programming (OOP), Java is fully object-oriented.
1. Use object-oriented ideas to describe the real world.
Basic steps:
1. Discovery class
For example, you can divide people into several categories.
Human/scientist/actor/funny Ratio
2. Identify attributes (nouns)
Name, gender, occupation, age, hobbies ......
3. Find the action (verb)
Eating, talking, performing ......
Data abstraction: A combination of data and processing methods
Ii. Use a class chart description class
Role: intuitive and easy to understand
Reference Tools: StarUML and Astah UML
Iii. Relationship between classes and objects
Class: abstract concepts, just templates, such as "actors" and "Presidents ". A class is a template and blueprint for constructing objects and is used to describe a data type.
Object: You can see the specific object.
Take cookies as an example. Think of a class as a machine for making cookies, and the object is a cookie. The construct object is called as an instance for creating a class ).
Class on the left and object on the right
Supplement: how to access the private variables of other classes in another class
Iv. Relationships between classes
Common relationships include dependency, aggregation, and inheritance.
Dependency (uses-a): If a class method is used to manipulate objects of another class, it means that a class depends on another class. We should try to reduce the number of dependent classes as much as possible, in terms of software engineering, the coupling between classes is minimized.
Has-a: the object of Class A contains the object of Class B.
Inheritance (is-a): special and general relationships.
5. pre-defined categories
In java, nothing can be done without classes. However, not all classes have object-oriented features, such as Math classes.
1. Object and object variable: to use an object, you must first construct the object and specify its initial state.
Difference: Date deadline; // deadline does not refer to any object,
In fact, it is equivalent to c ++: Date * deadline;
Defines an object variable deadline, which can reference objects of the Date type. However, you must realize that the variable deadline is neither an object nor an object to be referenced. First, initialize this variable: deadline = new Date (); If deadline = null; it indicates that this object has not referenced any objects.
Two variables reference one object: (an object variable does not actually contain one object, but only references one object. In Java, the value of any object variable is a reference to an object stored in another place. The return value of the New operator is also a reference .)
In c ++, you may create an incorrect pointer or cause memory overflow. In Java, none of these problems exist. If an uninitialized pointer is used, an error is reported instead of the random running result. At the same time, you do not have to worry about memory management, the garbage collector will handle related issues.
2. LocalDate class in Java class library
The time is represented by the number of milliseconds (positive and negative) from a fixed time point. This point is called epoch, Which is UTC time 00:00:00 on January 1, January 1, 1970. UTC is the abbreviation of Coordinated Universal Time. Like the familiar GMT (Greenwich Mean Time, Greenwich Mean Time), UTC is a scientific standard Time of practical significance.
Do not use constructors to construct LocalDate class objects.
Local Date. now () constructs a new object, indicating the Date when this object is constructed.
You can provide the year, month, and day to construct the object corresponding to a specific date: LocalDate. of (1999, 12, 31 );
Of course, you usually want to save the constructed object in an object variable: LocalDate newYearsEve = Local Date. of (1999, 12, 31 );
Once a LocalDate object is available, you can use the methods getYear, getMonthValue, and getDayOfMonth to get the year, month, and day:
Int year = newYearsEve. getYearO; // 1999
Int month = newYearsEve. getMonthValueO; // 12
Int day = newYearsEve. getDayOfMonth (); // 31
It seems that this does not make much sense, because it is exactly the values used when constructing objects. However, sometimes a certain date may be calculated and you want to call these methods for more information. For example, the plusDays method will get a new LocalDate. If the object applying this method is called the current object, this new date object is a new date from the specified days of the previous object:
LocalDate aThousandDaysLater = newYearsEve. piusDays (1000 );
Year = aThousandDaysLater. getYearO; // 2002
Month = aThousandDaysLater. getMonthValueO; // 09
Day = aThousandDaysLater. getDayOfMonth (); // 26
The LocalDate class encapsulates the instance domain to maintain the set date. If you do not view the source code, you cannot know the date representation of the class's inner part. Of course, the significance of encapsulation is that this is not important, but it is important that the class provides external methods.
3. Method and Method
1. accessor method: the method that only accesses objects without modifying objects. For example, LocalDate. getYear and GregorianCalendar. get.
LocalDate aThousandDaysLater = newYearsEve. plusDays (1000); Call newYearEve. after plusDays, it will not change to the date after 1000. In fact, the plusDays method does not change the object that calls this method. Instead, the plusDays method will generate a new LocalDate object, then, assign the new object to the aThousandDaysLater variable. The original object is not modified.
2. mutator method: the object method changes.
There is a GregorianCalendar method which is the modifier method and can be used to add 1000 days:
CregorianCalendar someDay = new CregorianCalendar (1999, 11, 31); // note that the month is from 0 to 11.
SomeDay. add (Calendar. DAY_0F _ M0NTH, 1000 );
Note: In c ++, the method with the const suffix is the accessor method. The default value is the modifier method. However, there is no obvious syntax difference between the accessor and the modifier in Java.