Class:
is a template or blueprint for constructing objects;
The process of constructing objects by class is called creating an instance of an object;
All code written in Java is in the interior of a class;
Packaging:
Package the instance fields (attributes) and methods together;
The user of the object hides the way the data is implemented;
The key is that the methods in the class can never directly access the instance fields of other classes, the program interacts with the object data only through the method of the object, that is, using methods to hide the data of the class, to control the degree to which the other classes modify and access the data; that is, the properties of the object are generally obtained and modified by methods
For each particular instance (object) there is a specific set of instance field values, which are the current state of the object;
Pros: For domain
Avoid the domain being accessed directly by methods of other classes, (so it is generally defined as private, and is invoked and modified only by methods in this class)
Can change the internal implementation, in addition to the method of the class, does not affect the other code;
The change method can perform error checking;
Attention:
Do not write accessor methods that return references to mutable objects; If you need to return a reference to a Mutable object, you should first clone it.
Inherited:
Subclasses inherit all the content of the parent class (including all properties and methods, but not the construction method), but not necessarily directly;
Java only supports single inheritance, extends can only be followed by one class;
Example:
Class B inherits Class A;
Class B Creates an instance that can invoke the same public method and public instance domain as in Class A, but cannot call or set the same private instance domain and private method in a class and A;
All objects of a class, each with its own copy of the instance field, but all objects share those methods;
Inherited attributes:
1. When a class inherits from another class, the subclass does not explicitly define the same method as the parent class (the method name, parameter, and return value are the same). When you invoke a method with an object of a subclass, it is first found in the subclass, and if the method is not in the subclass, then it is found in the parent class.
Example (1):
1 Public classDemo12 {3 //the print () method defined by the parent class4 Public voidprint ()5 {6SYSTEM.OUT.PRINTLN (123);7 }8 }9 Ten One Public classDemo2extendsDemo1 A { - //subclasses do not explicitly define the same method as the parent class (method name, parameter, return value type) - } the - - Public classTest - { + Public Static voidMain (string[] args) - { +Demo2 demo =NewDemo2 (); A //The final print results are 123 at demo.print (); - } -}
Example (2):
Public classdemo1{//the print () method defined by the parent class Public voidprint () {System.out.println (123); }} Public classDemo2extendsdemo1{//subclasses explicitly define the same method as the parent class (override) Public voidprint () {System.out.println (456); }} Public classtest{ Public Static voidMain (string[] args) {Demo2 demo=NewDemo2 (); //The final print results are 456Demo.print (); }}
2. Usage rules for constructors between subclasses and parent classes:
(1). Each class in Java has a constructor, and if a constructor is not explicitly defined in the class, the compiler provides an parameterless constructor by default;
(2). When a subclass inherits from a parent class, the constructor of the parent class must be called in the constructor;
(3). The parent class does not explicitly define the constructor, there is only one default parameterless constructor, and when the subclass inherits the parent class, it is also the constructor of the parent class by calling the default parameterless constructor;
Public classdemo1{/*** This is the parameterless constructor provided by the compiler by default * This constructor is hidden, not visible, now let it be explicit just to see the look of the parameterless constructor * here super () is called the constructor of the Object class * The parent class is not explicitly determined Semantic Constructors*/ PublicDemo1 () {Super(); }} Public classDemo2extendsdemo1{/*** Subclass inherits the parent class, invoking the constructor method of the parent class through the default constructor provided by the compiler * Here the constructor method is also hidden * here's super () called the Demo1 class constructor method*/ PublicDemo2 () {Super(); }}
(4). The parent class display defines a parameter (must be a parameter) constructor, and when the subclass inherits the parent class, it must also display a constructor that defines a parameter constructor that calls the parent class in the constructor;
Public classdemo1{/*** Parent class display defines a parameter constructor*/ PublicDemo1 (String s) {Super(); System.out.println (s); }} Public classDemo2extendsdemo1{/*** Subclasses must display the constructor that defines a parameter constructor to invoke the parent class * Here super (s) is called the parent class with the parameter constructor, S is the argument*/ PublicDemo2 (String s) {Super(s); }}
3. when a method is defined only in the parent class, the property used to invoke the method is the property of the parent class , and if other methods are called in the method, then it is in the preceding order that it is looked up in the subclass and then in the parent class.
Public classdemo1{intx = 6; Private inty = 2; PublicDemo1 (inta) {x=A; } intGetz () {intZ; Z= x/y; returnZ; } voidShow () {System.out.println ("X=" +x); System.out.println ("Y=" +y); //Other methods are called in the methodSystem.out.println ("z=" +Getz ()); }} Public classDemo2extendsdemo1{//x, y variable redefined intx = 3, y = 5, Z; PublicDemo2 (inta) {Super(a); } //overridden The Getz () method intGetz () {Z= x +y; returnZ; } //define a unique method Public voidprint () {System.out.println ("x =" +x); System.out.println ("y =" +y); System.out.println ("Z =" +z); } } Public classtest{ Public Static voidMain (string[] args) {Demo1 demo1=NewDemo1 (10); Demo2 Demo2=NewDemo2 (9); Demo1.show (); /*** Printed Results: * x=9: Subclass Demo2 by constructing a method to set the value of the X attribute in the parent class Demo1 to 9, the * Show () method called by the subclass object is not redefined (overridden) in the subclass, and the call is inherited The show () method from the parent class, * The X property that is called in the Show () method in the parent class is the attribute in the parent class with a value of 9; * y=2: Similarly, the show () method called by the subclass is not redefined in the subclass, so the show () party The Y property called in the method * is the Y property in the parent class, the value is 3, the Getz () method in the Z=8:show () method has both the subclass and the parent class, and the Getz () * method in the subclass is called first, then the get Z () The x, y, and y (x, y) attributes are defined in the subclass, and the * values are 3 and 5, respectively, so the value computed by the number is 8;*/demo2.show (); /*** Call a subclass-specific method, and the property used in the method is a redefined property of the child class*/Demo2.print (); }}//The result printed at the end://x=10//y=2//z=5//x=9//y=2//z=8//x = 3//y = 5//z = 8
Java Object-oriented