Object oriented advanced level

Posted by canobi on Sat, 26 Feb 2022 05:18:32 +0100

1. Inherit
//getter method,
//The function is to assign the attribute passed in by the constructor to the encapsulated attribute in this class through the getter method,
//Then the assigned attribute is sent out through the constructor
//setter method,
//The function is to call the setter method in this class through the calling method in other classes to set the encapsulated properties,
//Finally, the set attribute is passed out through the constructor
//
//Therefore, in the class that encapsulates the attribute:
//1. First encapsulate the attributes,
//2. Write the constructor with and without parameters to pass in and out variables or constants
//3. Then write getter and setter methods

//The setter method is used to set the passed in variable or constant,
//Assigning an assignment to the encapsulated variable attribute of the passed in variable or constant,
//That is, first pass in variables or constants, and then set them
//Therefore, the parentheses in setter () should write the type and name of the variable

Inheritance instance
public class Inheritance {

public static void main(String[] args) {
    student2 s = new student2();
    s.setName("Zhang San");
    s.setAge(18);
    s.say();


}

}

class person2{
private String name;
private int age;

public person2(String name, int age) {
    this.name = name;
    this.age = age;
}
public person2() {
}

// getter and setter methods set values (assigned to encapsulated variables) for external calling classes to set and obtain,
// The externally called class then passes in or out the corresponding variables through the constructor with or without parameters

// getter method,
// The function is to assign the attribute passed in by the constructor to the encapsulated attribute in this class through the getter method,
// Then the assigned attribute is sent out through the constructor
// setter method,
// The function is to call the setter method in this class through the calling method in other classes to set the encapsulated properties,
// Finally, the set attribute is passed out through the constructor
//
// Therefore, in the class that encapsulates the attribute:
// 1. Encapsulate attributes first,
// 2. Then write the constructor with and without parameters to pass in and out variables or constants
// 3. Then write getter and setter methods

public String getName() {
    return name;
}

public void setName(String name) {  // The setter method is used to set the passed in variable or constant,
                                    // Assigning an assignment to the encapsulated variable attribute of the passed in variable or constant,
                                    // That is, first pass in variables or constants, and then set them
                                    // Therefore, the parentheses in setter () should write the type and name of the variable
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public void say(){
    System.out.println("I am"+name+",this year"+age+"Years old");
}

}

class student2 extends person2{

}

How much inheritance is in C + + (for example, a child has multiple fathers. Disadvantage: when the inherited father has dual things, it is easy to get confused)
Java has only single inheritance (a subclass can only have one parent class), and multiple inheritance, without multiple inheritance

When the inheritance code is executed on the stack, first check whether the subclass of heap memory has encapsulated properties, constructors, getter s and setter s,
In some cases, the memory of the parent class is not created, and the encapsulated properties, constructors, getter s, setter s and other methods of the parent class are not used,
If not, the heap memory creates the parent memory, and then uses the encapsulated properties, constructors, getter s, setter s, and other methods of the parent,
It's going up layer by layer,
If all parent classes do not have encapsulated properties, constructors, getter s, setter s, and other methods, the system will report an error

When the inheritance code is executed on the stack, the heap memory is the memory of the parent class first, and the memory of the child class
Then, the object of the child class points to the memory of the parent class, and then the operation

2,super
Constructor that accesses the parent class through super (i.e. incoming or outgoing data)
When accessing (i.e. incoming or outgoing data) the parent class, it is accessed by the parameterless constructor by default,
When the parent class does not have a parameterless constructor, the subclass should be explicitly called with super when inheriting. If it is not called with super, an error will be reported

    // If you don't write super,
    // Is that the compiler automatically omits the parameterless constructor that calls the parent class
    // Here is to call the constructor with parameters of the parent class through super
    // The code that calls the super constructor must be written on the first line of the subclass constructor

    /*this keyword
    When another construction method is invoked in a construction method
    The calling code must be written on the first line of the constructor
    Reason: when creating an object in heap memory (i.e. the object is initialized in heap memory) for the object called by this keyword, it has not been completed,
    Wait until the object is initialized in heap memory, and then execute other logic, otherwise an error will be reported*/

    // Therefore, the super and this keywords cannot appear in the same code block
    
    // super does not need to be written in the first line of the constructor when calling the member property

super instance
public class Inheritance {

public static void main(String[] args) {
    student2 s = new student2();
  /*  s.setName("Zhang San ");
    s.setAge(18);*/
    s.say();


}

}

class person2{/ / parent class
private String name;
private int age;
public String sex;

public person2(String name, int age) {
    this.name = name;
    this.age = age;
}
public person2() {
}

// getter and setter methods set values (assigned to encapsulated variables) for external calling classes to set and obtain,
// The externally called class then passes in or out the corresponding variables through the constructor with or without parameters

// getter method,
// The function is to assign the attribute passed in by the constructor to the encapsulated attribute in this class through the getter method,
// Then the assigned attribute is sent out through the constructor
// setter method,
// The function is to call the setter method in this class through the calling method in other classes to set the encapsulated properties,
// Finally, the set attribute is passed out through the constructor
//
// Therefore, in the class that encapsulates the attribute:
// 1. Encapsulate attributes first,
// 2. Then write the constructor with and without parameters to pass in and out variables or constants
// 3. Then write getter and setter methods

public String getName() {
    return name;
}

public void setName(String name) {  // The setter method is used to set the passed in variable or constant,
                                    // Assigning the value of the passed in variable or constant to the encapsulated variable attribute,
                                    // That is, first pass in variables or constants, and then set them
                                    // Therefore, the parentheses in setter () should write the type and name of the variable
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public void say(){
    System.out.println("I am"+name+",Gender:"+sex+",this year"+age+"Years old");
}

}

class student2 extends person2{/ / the subclass inherits from the parent class

public student2() {
    super("No name",18);  // This line represents the automatically created parent object. Calling the construction method must be in the first line of the code block,
                                   // How many subclasses are created will automatically create how many parent objects
    super.sex = "male";              // super calls the member attribute, no more than in the first line of the code block

    super.setName("Zhang Sanfeng");        // The method of accessing the parent class through super does not need to be in the first line of the code block

    // If you don't write super,
    // Is that the compiler automatically omits the parameterless constructor that calls the parent class
    // Here is to call the constructor with parameters of the parent class through super
    // The code that calls the super constructor must be written on the first line of the subclass constructor

    /*this keyword
    When another construction method is invoked in a construction method
    The calling code must be written on the first line of the constructor
    Reason: when creating an object in heap memory (i.e. the object is initialized in heap memory) for the object called by this keyword, it has not been completed,
    Wait until the object is initialized in heap memory, and then execute other logic, otherwise an error will be reported*/

    // Therefore, the super and this keywords cannot appear in the same code block

    // super does not need to be written in the first line of the constructor when calling the member property
}

}
3. Rewrite
Override definition: the override of a method occurs only in the inheritance of a class
//Method. The contents in parentheses are parameter lists,

When rewriting,
1 ① the format of this subclass should be exactly the same as that of the parent class,
② The parameter list should also be exactly the same,
③ The and return classes must be the same
2. Access permission: the access permission of the parent class cannot be lower than that of the child class
3. Member methods of a parent class can only be overridden by member methods of other child classes
4. Methods declared as static and private cannot be overridden, but can be declared again,
Because static static itself has no inheritance and has nothing to do with objects;
private things themselves are not inherited or rewritten

Override: occurs in the child parent class
Overload: occurs in a class. There are multiple methods in a class with the same name,
However, if the length, type and type order of the parameter list are different, the overload will be formed
The difference between overload and overwrite
① Location of occurrence:
Override: in child parent class
Overload: in a class

② Parameter list restrictions:
Override: must be the same
Overload: must be different

③ Return value type:
Override: the return value type must be consistent
Overload: independent of return value type

④ Access rights:
Override: the access permission of the subclass must be greater than or equal to (≥) the access permission of the parent class
Overload: independent of access rights

⑤ Exception handling:
Rewrite: the exception range can be smaller, but new exceptions cannot be thrown
Overload: independent of exception

public class overWrite {
//Rewrite
public static void main(String[] args) {
student s = new student();
s.say();

}

}

class person {/ / parent class
public void say(){
System.out.println("the bright moon in front of the bed is suspected to be frost on the ground!");
}
}

Class student extensions person {/ / subclasses
public void say() {/ / overrides the method. The contents in parentheses are parameter lists,
//1. When rewriting, 1. The format of this subclass and the parent class must be exactly the same, 2. The parameter list must be exactly the same, and 3. The return class must be the same
//2. Access permission: the access permission of the parent class cannot be lower than that of the child class
//3. Member methods of a parent class can only be overridden by member methods of other subclasses
//4. Methods declared as static and private cannot be overridden, but can be declared again,
//Because static static itself has no inheritance and has nothing to do with objects;
//private things themselves are not inherited or rewritten
System.out.println("at noon on the hoeing day, sweat drops into the soil!!");
}
}

4,final
The attribute has a default value, and the default value of int attribute is zero (0);
When final modifies a global constant (public static final), it means that the global constant cannot be changed and is already a constant

① final is used to modify attributes and variables,
② After modification, it becomes a constant and cannot be assigned again
③ The local variable modified by final can only be assigned once at most (it can be declared first and then assigned)
④ final modifies the member attribute, which must be assigned at the time of declaration
⑤ public static final
⑥ The class decorated by final cannot be inherited
⑦ Methods decorated with final cannot be overridden by subclasses
⑧ Specification for constant naming:
It consists of one or more words, which must be separated by underscores, and all letters in the word are capitalized
For example: SQL_INSERT

final modifier instance
public class finalKeyword {

final int a = 0;  // The main method is preceded by the member variable. When modifying the member attribute here, it must be assigned at the time of declaration

public static void main(String[] args) {
    // After final modification, variables can only become constants
    final int b;  // The main method is followed by local variables,
                  // When the modified variable is not assigned, the variable can only be assigned once, followed by a constant
    b = 10;

}

}

5. Abstract class
Abstract class concept: abstract classes must be declared using abstract class
An abstract class can have no abstract method, but the abstract method must be written in the abstract class or interface

  Abstract class format:  abstract  class  Class name    {      }      //  abstract class

Abstract method: a method that is declared but not implemented is called an abstract method (not implemented means that there is no {} method body),
Abstract methods must be declared using the abstract keyword

              Abstract method format: 
              abstract  class  Class name  {   //  abstract class
                            public  abstract  class  Method name ();   //  Abstract method, declared but not implemented                                
                        }

Abstract class cannot be instantiated:
① The abstract class itself cannot be instantiated directly, that is, it cannot be completed directly with the keyword new;
② An abstract class must be inherited by subclasses,
The inherited subclass (if it is not an abstract class) must write all the abstract methods in the abstract class

common problem
① Abstract classes cannot use final declaration because the class modified by final cannot have subclasses,
If an abstract class wants to be instantiated, it must have subclass inheritance, and all abstract methods must be completed in the subclass,
Therefore, abstract classes must have subclasses to be meaningful
② Abstract classes can also have their own constructors, and the process of subclass object instantiation is the same as that of ordinary class inheritance,
Are to call the constructor of the parent class (the default is parameterless), and then call the constructor of the child class itself

Specification: a point java file (. Java) file only writes one class

When inheriting an abstract class, the subclass itself can be an abstract class, or the subclass can not be an abstract class,

However, when the subclass inheriting the abstract class is not an abstract class, the subclass shall instantiate the abstract method,
It means that the format of the abstract method in the inherited parent abstract class is the same, but the abstract should be removed

Abstract classes can have abstract parts or non Abstract parts

Interfaces are more abstract than abstract classes

When a subclass is created in heap memory, the object of the parent class is created first

Abstract classes can have constructors, and objects cannot be created through the new method,
However, the JVM (Java virtual machine) can create objects through other methods

Differences between abstract classes and ordinary classes:
① The abstract class must be decorated with public or protected (if it is private, the subclass cannot inherit or implement its abstract methods),
If there is no write permission modifier, it defaults to public,. Instead of default as previously said
② Abstract classes cannot use the new keyword to create objects,
However, when subclasses create objects, the abstract parent class will also be instantiated by the JVM (Java Virtual Machine)
③ If a subclass inherits an abstract class, it must implement all its abstract methods,
If there are abstract methods that are not implemented, subclasses must also be defined as abstract classes

Abstract method instance
public class abstractClass {
public static void main(String[] args) {
student3 s = new student3();
s.say();

}

}

Abstract class personality {/ / abstract class

public personel(){            // Abstract classes can also have construction methods. When subclasses are created in heap memory, the parent abstract class object has been created and executed
    System.out.println("The construction method is executed");
}
public abstract void say();    // Abstract method

}

class student3 extends personel{

@Override
public void say() {
    System.out.println("I am a student. I want to study hard and make progress every day");

}

}

6. Interface
Interface definition:
If all methods in a class are abstract methods and all attributes are global constants, then this class can be defined as an interface

  Interface format: 
  interface   Interface name  {
   public  static  final   Attributes;                //   Global constant  
  public abstract void   Method name (  )  ;        //   Abstract method
  }

Implementation of interface
Interfaces can be implemented in multiple ways

Format of interface implementation:
class subclass implements parent interface 1, parent interface 2... {/ / interface implementation

                                     }

A class that implements both the interface and the inherited format:
Class subclass extends, parent class implements, parent interface 1, parent interface 2{

                                    }

Interface oriented programming idea:
Interface is the separation of definition (specification and constraint) and Implementation (the principle of separation of name and reality)
advantage:
① Reduce program coupling (so that program code block requirements are not too glued)
② Easy program expansion
③ It is conducive to program maintenance

Abbreviations for global constants and abstract methods:
Because the interface itself is composed of global constants and abstract methods, the member definitions in the interface can be abbreviated as:
① When writing global constants, you can omit the public static final keyword,
For example:
public static final String INFO = "content";
After abbreviation:
String INFO = "content";

② When writing abstract methods, you can omit the public abstract keyword,
For example:
public abstract void print ( ) ;
After abbreviation:
void print ( ) ;

Interface inheritance:
Because interfaces are abstract parts and there is no concrete implementation, multiple inheritance is allowed,
For example:
Interface C extends a, B {/ / multiple inheritance of interfaces

                  }

Key points for interface class:
① There can be no implementation in the interface, and there can be no method code block {}
② When subclasses implement interface classes, they must add implementation methods to the code
③ When subclasses implement the abstract methods of the interface, they must write out all the abstract methods in the interface
④ The global constants in the interface do not have to be written out,
⑤ If the interface wants to be used, it must rely on subclasses (if subclasses are not abstract classes) to implement all abstract methods in the interface




Difference between interface and abstract class:
① Abstract classes should be inherited by subclasses, and interfaces should be implemented by classes
② Interfaces can only declare abstract methods, and abstract classes can declare abstract methods or write non abstract methods
③ The defined variables in the interface can only be public static constants (i.e. global constants: public static final attribute;),
Variables in abstract classes are ordinary variables
④ Abstract classes are used through inheritance and cannot be inherited more. Interfaces are used through implementation and can be implemented more
⑤ Static methods can be included in abstract classes, but not in interfaces (static methods cannot be overridden by subclasses, so static methods cannot be declared in interfaces)
⑥ Interfaces cannot have constructors, but abstract classes can

public class interfaceKnowledgePoint {
public static void main(String[] args) {
village v = new village();
v.say();
}
}

interface personner{
int a = 20 ;
void say() ;
void jump();

}

class village implements personner{

@Override
public void say() {       // When subclasses implement the abstract methods of the interface, they must write out all the abstract methods in the interface
    System.out.println("I am a villager this year"+a+"Years old"); // However, the global constants in the interface do not have to be written out,
                                                  // As "a" here, the code can be omitted logically
}

@Override
public void jump() {      // When subclasses implement the abstract methods of the interface, they must write out all the abstract methods in the interface
    System.out.println(1);
}

}

7. Polymorphism
A parent class reference points to a child class object
instances

8. The object class is the parent of all classes

Topics: iOS Vue.js objective-c