Day_12_ Polymorphism, Object, abstraction, interface

1. Six principles of software design:           1) Single responsibility principle: single fu...
  2.1 several forms of polymorphism:
  2.2 polymorphism disadvantage: loss of subclass specific attributes
2.3 advantages
2.4 covert polymorphism
2.5 Instanceof
3.1 advantages
1. Six principles of software design:



          1) Single responsibility principle: single function embraces a change
                   A method is only responsible for one thing, so that when the method is changed, it will not affect other programs. Almost all programmers follow this principle
                   Advantages: reduce the coupling between classes, improve readability, increase maintainability and scalability, and reduce the risk of variability

           2) Richter substitution principle: all places where a parent class can be used, a child class must be used

           3) Dependency Inversion Principle: the high level depends on the low level through abstraction, and the details should depend on abstraction

           4) Interface isolation principle:  

           5) Dimitri principle: also known as the least knowledge principle
                   A class should minimize its dependence on other classes. The principle is low coupling and high cohesion

           6) Opening and closing principle: it is closed for modification and open for extension


  2. Polymorphism:

          A parent class reference points to a child class object
           Parent class reference: a reference type variable declared with a parent class
           Point to: who can be found through this variable
           Using variables declared by the parent class, you can find subclass objects
Animal a = new Animal();
           Animal a = new Cat();   polymorphic


  2.1 several forms of polymorphism:


          1. Direct polymorphic Animal a = new Animal();
           2. Formal and actual parameters: when a method is declared, the parameter list needs to receive the parent type, and when a method is called, the subclass object is passed in
public static void m1(Animal a){}
m1(new Cat());
           3. Return value: the return value type is the parent type, but the returned object is the child object
public static Animal m2(){
return new Cat();
}


  2.2 polymorphism disadvantage: loss of subclass specific attributes


           Call with polymorphism:  
           1. If there is no parent class, an error will be reported regardless of whether there is a child class, because the unique attributes of the child class are lost
           2 if the parent class has and the child class does not, use the parent class because of inheritance
           3. If there is a parent class, there are also subclasses. Except that the member method executes the subclass, all others execute the parent class (because the member method can be overridden)

public class Poly_01 { public static void main(String[] args) { Animal a = new Cat(); a.eat(); } } class Animal{ public void eat() { System.out.println("having dinner..."); } } class Cat extends Animal{ public void eat() { System.out.println("Cats eat fish"); } }

2.3 advantages

         High cohesion, low coupling

public class Poly_02 { public static void main(String[] args) { Cat_01 c = new Cat_01(); eat(c); Dog_01 d = new Dog_01(); eat(d); Pig p = new Pig(); eat(p); } public static void eat(Animal_01 a) { a.eat(); } } class Animal_01 { public void eat() { } } class Cat_01 extends Animal_01 { int age = 2; public void eat() { System.out.println("Cats eat fish"); } } class Dog_01 extends Animal_01 { int age = 3; public void eat() { System.out.println("Dogs eat meat"); } } class Pig extends Animal_01 { @Override public void eat() { System.out.println("Eat cabbage"); } }

2.4 covert polymorphism

         When a method of a parent class is called through a subclass, the context of the method of the parent class is a polymorphic environment (belonging to the parent class space and subclass objects)

public class Poly_04 { public static void main(String[] args) { Sup sup = new Sub(); // 2 System.out.println(sup.i); // Subclass sup.m2(); Sub sub = new Sub(); // 3 // System.out.println(sub.i); // Subclass // sub.m2(); sub.m1(); } } class Sup { int i = 2; public void m1() { System.out.println(this.i); this.m2(); } public void m2() { System.out.println("Parent class m2"); } } class Sub extends Sup { int i = 3; public void m2() { System.out.println("Subclass m2"); } }

2.5 Instanceof

         Polymorphism is also called upward transformation. Subclass to parent is upward transformation, and parent to child is downward transformation

         Because polymorphism will lose subclass specific attributes, when you want to call subclass specific attributes, you need to convert to the corresponding subclass type

public class Poly_05 { public static void main(String[] args) { Animal_01 a = new Cat_01(); // Cannot be called, missing unique properties // System.out.println(a.age); m1(a); // If a Dog type is passed, an error will be reported a = new Dog_01(); m1(a); } public static void m1(Animal_01 a) { // instanceof: judge whether an object is instantiated from a class if (a instanceof Cat_01) { Cat_01 c = (Cat_01) a; System.out.println(c.age); } else { Dog_01 c = (Dog_01) a; System.out.println(c.age); } } }
3. Abstract

abstract is a modifier

The modified class is an abstract class. It cannot create an object, but is only used to be inherited
The modified method is an abstract method, and the method has no method body (that is, {})
Abstract methods must be in abstract classes, but there must not be abstract methods in abstract classes

   abstract and final are two choices, because the class modified by final cannot be inherited and member methods cannot be overridden
  The abstract modified abstract class is used to be inherited, and the abstract method is used to override the implementation

   A non abstract class inherits an abstract class and needs to implement all abstract methods
   An abstract class inherits an abstract class and needs to implement 0~N abstract methods

   Although an abstract class cannot create an object, it has a constructor that is called when a subclass creates an object

public class Abstract_01 { public static void main(String[] args) { // Animal animal = new Animal(); Animal a = new Cat(); a.eat(); } } abstract class Animal{ public Animal(){ System.out.println("Parent class construction"); } public abstract void eat(); } class Cat extends Animal{ @Override public void eat() { System.out.println("Cats eat fish"); } } class Dog extends Animal{ @Override public void eat() { System.out.println("Dogs eat meat"); } }

Note: abstract and final cannot appear at the same time

3. Interface

Interface: define interface keywords   Syntax: [permission modifier] interface interface name

  Interface is mainly used to solve the problem of weakening single inheritance function in java. A class can only inherit one class, but multiple interfaces can be implemented

   Before 1.8, the interface was completely abstract, and can only have abstract methods and constants

   1.8 default methods and static methods are allowed in the start interface

   1. Grammar   [permission modifier] interface interface name
   2. All interfaces are public by default
               All variables in the interface are constants, and public static final can be omitted. The default is
               The abstract method in the interface, public abstract, can be omitted. The default is
   3. The interface has no construction method and cannot create an object
   4. A class can only inherit one class, but can implement multiple interfaces
               [permission control modifier] class name implements interface name 1, interface name 2,... {}
               A class that implements an interface must implement all abstract methods in the interface
               An abstract class that implements an interface needs to implement 0~N abstract methods
   5. There are multiple inheritance between interfaces, which are separated by commas
           [permission control] interface interface name extends parent interface name 1, parent interface name 2,... {}

   6.1.8 start with static methods and default methods (default methods can be understood as member methods, and sub implementation classes can also be overridden)
               Static method call: interface name. Method name (parameter);
               Default method call: subclass object. Method name (parameter) needs to be implemented;  
              1.9 started to support the private method

public interface Interface_01 { // There are no variables in the interface, only constants public static final int xxx = 2; // public static final can be omitted. The default is int aaaa = 3; // Abstract method public abstract void m1(); // public abstract can be omitted. The default is void m2(); // Static method public static void m3() { System.out.println("Static method"); } // Default method default void m4() { System.out.println("Default method"); } } interface A{ void m1(); } interface B{ void m2(); } interface C{ void m3(); } interface D extends A,B,C{ void m4(); } class E implements A,C{ @Override public void m3() { } @Override public void m1() { } }

3.1 advantages

When a function has many different implementations, it needs to rely on interface programming, which can reduce the degree of coupling

If a function embraces only one change, there is no need to use an interface

Polymorphism can also be used

Note: if the interface and abstract class can accomplish one thing, the interface is preferred, because the class and interface are multiple implementations, and the class and class are single inheritance, which can retain the inheritance of the class

14 October 2021, 15:36 | Views: 4629

Add new comment

For adding a comment, please log in
or create account

0 comments