1, Object oriented
1. Main features
1.1 packaging
Abstract data types (classes) are used to bind data and data-based operations. The data is stored in the abstract data type. The system can access the data only through authorized operation methods
- characteristic:
- Data and data-based operation methods form a unity
- The implementation details of the operation method of class are hidden, and debugging is only carried out through the operation interface name. Changes within the operation will not affect the use of the interface
- realization
- Set access rights and modify visibility
- Create getter/setter methods
- Add attribute control statement in getter/setter method
- Create an instance of the class and call the constructor
- purpose
- Users do not need to know the internal details of the object, but they can access the object through the interface provided by the object, which is conducive to safe operation
- High cohesion, loose coupling
1.2 succession
It is implemented through the extends keyword to make the two classes inherit
is-a relation
- Inheritance can only be single inheritance. A class can only have one parent class
- Purpose:
- An important means of reusing functions
- It brings new features to citation
1.3 polymorphism
In a program, different methods with the same name coexist, providing two mechanisms - overloading and overriding
- heavy load
- Methods have the same name and different parameter lists (including parameter order, number, type, etc.)
- Must be within the same class
- The function and purpose of the method are the same, but the specific details are different
class StuManage { public int getScore(){ return 3; } public int getScore(int i){ return i; } /*public long getScore(int i){ return i; }*/ /*private int getScore(int i){ return i; }*/ /*public static int getScore(int i){ return i; }*/ /*public final int getScore(int i){ return i; }*/ // None of the in the comment are overloaded public int getScore(Integer i){ return i; } //Variable parameter, i is essentially an array int [] public int getScore(int ... i){ return 3; } }
- cover
- Subclasses redefine the methods with the same name (the same method name, the same parameters and the same return type) of the parent class, that is, define methods with the same name but different contents in the subclass.
- After the subclass inherits from the parent class, when the subclass has business requirements different from the parent class, the subclass can use method override
- Conditions:
- Two classes must have an inheritance relationship
- The overridden method and the previous method have the same return value type, the same method name and the same formal parameter list (the method with the same name may not be overwritten, but may be overloaded)
- The access permission of the overriding method cannot be lower than that of the previous method, but can be higher
- The number of exceptions thrown by the overriding method cannot be more or less than that of the previous method
- Purpose:
- Meet diverse needs and realize more powerful functions
class Parent { Number getScore(int a) { return new Integer(7); } } class Son extends Parent { @Override //All overwriting methods shall be indicated Number getScore(int b){ return new Integer(8); } } public class RunSon{ public static void main(String args[]){ Son s = new Son(); System.out.println(s.getScore()); } }
2. Interaction between objects
- Action mode: objects can be combined, inherited, and one object can directly create another object within it.
- Action condition: two objects should be visible to each other.
3. Common methods of object base class
Method name | explain |
---|---|
Class getClass() | Gets the class object of the current object |
String toString() | Get the string representing this object |
Object clone() | Clone the current object. The default is shallow copy |
void finalize() | Object was used when it was released, deprecated |
int hashCode() | Get an integer representing the object, which remains unchanged when the application runs |
Boolen equals(Object obj) | Judge whether two references point to the same object, and its parameters cannot be ordinary data types |
void notify | Used to wake up waiting threads in thread synchronization |
void wait() | Applied to thread waiting in thread synchronization |
2, Access control modifier
Modifier
Access modifier | Can the same type be accessed | Can the same package be accessed | Can subclasses be accessed | Can different packages be accessed |
---|---|---|---|---|
public | yes | yes | yes | yes |
protected | yes | yes | yes | no |
private | yes | yes | no | no |
Default (default) | yes | no | no | no |
- protected
- Subclasses are allowed to access the data and methods defined in the parent class, but non subclasses are not allowed to access these data and methods. In this case, protected can be used, which allows subclasses in any package to access the parent class
- The protected members of the base class are visible within the package and are visible to subclasses
// Design the fitplane class in the resource package package com.resource; public class FightPlane { protected String name; protected int missileNum; public void fire(){ if(missileNum > 0){ System.out.println("now fire a missile!"); missileNum -= 1; }else{ System.out.println("No missile left!"); } } }
// Design the RunPlane class in the run package and inherit the fitplane class package com.run; import com.resource.*; public class RunPlane extends FightPlane { private void init(){ name = "su35"; missileNum = 6; // You can access the protected attribute in another package } public static void main (String[] args){ RunPlane fp = new RunPlane(); fp.init(); fp.fire(); } }
- Default (default)
- Package access
- Not in a package, even inheritance relationships cannot be accessed
// Design the fitplane class in the resource package package com.resource; class FightPlane { // By default, it is set to package access protected String name; protected int missileNum; public void fire(){ if(missileNum > 0){ System.out.println("now fire a missile!"); missileNum -= 1; }else{ System.out.println("No missile left!"); } } }
// Design the RunPlane class in the run package and inherit the fitplane class package com.run; import com.resource.*; public class RunPlane extends FightPlane { private void init(){ name = "su35"; missileNum = 6; // You can access the protected attribute in another package } public static void main (String[] args){ RunPlane fp = new RunPlane(); fp.init(); fp.fire(); } } // Error reporting: Java: com.resource.firmplane is not public in com.resource; It cannot be accessed from an external package
Access rights in inheritance
- The subclass inherits all the methods and properties of the parent class, but only public and protected are visible
- private cannot be accessed directly by subclasses, but subclasses can be accessed indirectly by calling the public methods of the parent class
class FightPlane { private String name; protected int missileNum; public void fire(){ if(missileNum > 0){ System.out.println("now fire a missile!"); missileNum -= 1; }else{ System.out.println("No missile left!"); } } } public class RunPlane extends FightPlane { private void init(){ name = "su35"; // Error: java: name is a private access control in com.run.firmplane missileNum = 6; } public static void main (String[] args){ RunPlane fp = new RunPlane(); fp.init(); fp.fire(); } }
3, Combination of classes
Use the reference of the defined class object in a class, and call the properties and methods of the object by sending a message
has-a, including relation, different from inheritance relation
- effect:
- Without destroying encapsulation, classes are independent of each other, and the function is more powerful by using the methods of other classes
- Loose coupling between objects
class FighterPlane { String name; int missileNum; public FighterPlane(String _name,int _missileNum){ name = _name; missileNum = _missileNum; } public void fire(){ if (missileNum>0){ System.out.println("now fire a missile !"); missileNum -= 1; } else{ System.out.println("No missile left !"); } } } class A { FighterPlane fp; public A(FighterPlane fpp){ this.fp = fpp; //A object has a reference to the fighter plane object } public void invoke(){ //The object in A sends A message to the object in fighter plane System.out.println(fp.name); } } public class RunPlane{ public static void main(String[] args) { FighterPlane ftp = new FighterPlane("su35",10); //Generate an A object and pass the ftp object reference as A parameter A a = new A(ftp); //Send a message to generate a call relationship a.invoke(); } }
summary
Starting from the three main characteristics of object-oriented, focusing on encapsulation, inheritance and polymorphism, we have understood the difference between composition and inheritance and the importance of reasonably designing access rights, so as to become more familiar with Java object syntax and gradually realize that the subsequent learning can not be limited to the stage of writing code, More should be considered from the perspective of software development. It is worth thinking about how to use the learned knowledge to make the code structure clear, concise and beautiful.
In the process of sorting out the blog, I have referred to some materials and many excellent articles of others. I won't list them one by one. Thank you here.