Overview Object-oriented three features: encapsulation, inheritance, polymorphism. Find objects, build objects, use objects, and maintain relationships between objects. Class and object relationships: classes: A description of things in real life. class that is defined by class. Object: It is this kind of thing, the individual of the existence of fact. The entity that corresponds to New in Java. The object points to the entity. defines a class: it is in describing things, that is, defining attributes and behaviors, and the behavior corresponds to a method. When defining a class, if the property is not assigned a value, the initialization value is automatically initialized, and if the value is assigned, the initialization value is displayed. The properties of the class are member variables: They work in the entire class and exist in heap memory. Local variable: Acting in a function, or in a statement, exists in the stack memory. The main () function is a program entry, and the class containing main can run independently. Example: class Zx { public static void Main (String args[]) { car Zx = new Car (); zx.ran (); zx.color= "Blue"; zx.ran (); }} class Car { String color= "red"; int num = 4; void Ran () { system.out.println (color+ "...") +num); } } Anonymous object: How to use one: when the method of the object is treated = only once, it can be done with an anonymous object. Relatively simple, if you have multiple member calls to a slap object, you must name it. Use mode two: You can pass an anonymous object as an actual parameter. Example one: New Car (). num = 5; Become garbage new Car (). Colur = "blue";//become Garbage New Car (). Ran ();//become garbage, but can run the object method example two: public static void Show (Car c)// A method in the main class. {c.num = 3;c.color = "BLACK"; C.ran ();} show (New Car ()); The C in Show points to new Car (), not a garbageRubbish, when the show () life cycle ends, it becomes garbage.
feature One: encapsulation: encapsulation: Hides the properties and implementation details of an object, providing only public access. Encapsulation principle: Hide content that does not need to be provided externally. Hides properties, providing public methods for accessing them. private: Private, permission modifier, which is used to decorate a class in which a member (property, method) Private is a representation of encapsulation. Example: class Zx { public static void Main (String args[]) { persion p = new Persion ();p. age=-20;&n Bsp P.speak (); }} class persion { int age; void speak () { system.out.println (age); } } out:-20; private: class Zx { public static void Main (String args[]) { persion p = new Persion (); //p.age=-20; error P.speak (); }} class persion { Private int age;//can take advantage of a public void getage (int a) { age=a; &N Bsp } external methods to provide delivery method. void Speak () { system.out.println (age); } } Constructor feature: function name same as class name undefined return value type not writable return statement when a constructor is not defined in a class, the system automatically adds an empty argument constructor to the class. Example: class zx { Public static void Main (String args[]) { persion p = new Persion (); }} class Persion { pe Rsion () { System.out.println ("Persion ran ()") } }//will directly output "Persion ran ()" As Object one is established, The corresponding constructor is called. Function: The object can be initialized. The constructor can be overloaded, and the function name is still the class name, based on the new persion (int n), which is the parameter that determines which constructor to execute.
Java 5 (object)