Object Oriented 02

1. Code block

* A: Code Block Overview
* In Java, code enclosed in {} is called a code block.
* B: Code block classification
* According to its location and declaration, it can be divided into local code block, construction code block, static code block, synchronous code block
* C: Application of common code block
* a: Local code block (as long as it is What is related to the local is related to the method) 
* Appears in the method; limits the life cycle of the variable, releases it early, and improves the memory utilization
* b: Construction code block (initialization block)
* Appears outside the method in the class; The same code in multiple constructor methods is stored together, executed every time the constructor is called, and executed before the constructor
* c: static code block 
* Appears outside the method in the class, and adds static modification; used for the class Initialize, execute when loading, and execute only once
* Generally used to load driver
cases:

1  public  class Mxdx {
 2      // Construction block, every time the constructor is executed, it will be executed, that is, every time a new object is created, this will be executed first 
3      {
 4          System.out.println("gou zhao kuai" );
 5      }
 6      // Executed when the class is loaded, only executed once, generally used to load drivers: such as database connection 
7      static {
 8          System.out.println("jin tai kuai" );
 9      }
 10      
11      private String name;
 12      private  int age;
 13      public Mxdx() {
 14          super ();
 15          // TODO Auto-generated constructor stub
16         System.out.println("kon can");
17     }
18     public Mxdx(String name, int age) {
19         super();
20         this.name = name;
21         this.age = age;
22         System.out.println("you can");
23     }
...set,get方法 36 }
1  public  class MxdxTest {
 2      public  static  void main(String[] args) {
 3          Mxdx m1 = new Mxdx();
 4          m1.setName("Zhang San" );
 5          
6          Mxdx m2 = new Mxdx();
 7          m2. setName("Li Si" );
 8          
9          Mxdx m3 = new Mxdx("Wang Wu", 11 );
 10      }
 11 }

Results:
jin tai kuai
gou zhao kuai
kon can
gou zhao kuai
kon can
gou zhao kuai
you can

2. Inheritance

Let the relationship between the class and the class, the relationship between the child and the parent class Purpose: the advantages and disadvantages of
the child class inheriting the variables and methods of the parent class  : * A: The benefits of inheritance * a: Improve the reusability of the code, the parent class is Reuse * b: Improve the maintainability of the code * c: Make a relationship between classes and classes, which is the premise of polymorphism * B: Disadvantages of inheritance * The coupling of classes is enhanced. The relationship between classes is too close, adding attributes on one side, and one more on the other side, sometimes it is expected, but sometimes it is not expected * The principle of development: high cohesion, low coupling. It refers to the ability to complete things by yourself. If you can do it yourself, don’t bother others * Coupling : the relationship between classes Multiple inheritance. (A son can only have one father, and multiple inheritance has security risks. For example, A inherits B, C, because the attributes in BC may be contradictory, so inheritance will have problems, so multiple inheritance cannot be used) * b: Java supports multiple Layer inheritance (inheritance system), such as A inherits B inherits C, pastoral dog inherits dog inheritance Precautions and when to use animal inheritance: * A: Precautions for inheritance * a: Subclasses can only inherit all non-private properties of the parent class Members (member methods and member variables) * b: The subclass cannot inherit the constructor of the parent class, but can access the constructor of the parent class through the super keyword. * c : Don't inherit some functions





















* Inheritance actually reflects a relationship: "is a".
Person
Student
Teacher
Fruit
Apple
Banana
Orange
Using the hypothesis method.
If there are two classes A,B. As long as they meet that A is a kind of B, or B is a kind of A, you can consider using inheritance.
Case:

 1 class Demo3_Extends {
 2     public static void main(String[] args) {
 3         Son s = new Son();
 4         s.show();
 5     }
 6 }
 7 class Father {
 8     private String name;
 9     private void show() {
10         System.out.println("Hello World!");
11     }
12 }
13 14 class Son extends Father {
15 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325630740&siteId=291194637