06. Encapsulation inheritance polymorphism

Directory Structure

  • 1. Package
  • 2. Object-oriented paivate keyword
  • 3. Object-oriented this keyword
  • 4. Object-oriented construction method
  • 5. Steps to create an object
  • 6. Characteristics of object-oriented static keyword
  • 7. The difference between static variables and member variables
  • 8. Code Blocks
  • 9. Inheritance
  • 10. The difference between this and super
  • 11. Overview of method rewriting and its application
  • 12. Final keyword overview
  • 13. Polymorphism
  • 14. Disadvantages and benefits of polymorphism
  • 15. Abstract class features
  • 16. Interface Features
  • 17. The relationship between classes and classes, classes and interfaces, and interfaces and interfaces
  • 18. Difference between abstract class and interface
  • 19. Object-oriented class names as formal parameters
  • 20. Object-oriented abstract class name as a formal parameter
  • 21. Object-oriented interface name as a formal parameter
  • 22. Object-oriented class names as return value types
  • 23. Object-oriented abstract class name as return value type
  • 24. Object-oriented interface name as return value type
  • 25. Package definition and package
  • 26. Overview and use of import keyword
  • 27. Testing of the Four Permission Modifiers
  • 28. Common modifiers used by classes and their components
  • 29. Internal class overview and access features
  • 30. Anonymous inner classes

1. Package

  • 1.1 Overview of Packaging
  • Package Overview
    • It refers to hiding the properties and implementation details of an object, and only provides public access to the outside world.
  • Packaging benefits
    • Hide implementation details and provide public access
    • Improve code reusability
    • Improve security
  • encapsulation principle
    • Hide content that is not required to be provided externally.
    • Hide the property and provide public methods to access it.

2. Object-oriented paivate keyword

  • private keyword features
    • is a permission modifier
    • Member variables and member methods can be modified
    • Members modified by it can only be accessed within this class
  • The most common application of private
    • Modify member variables with private and provide the corresponding getXxx() and setXxx() methods

3. Object-oriented this keyword

  • why have this
    • When our local variables and member variables are the same, if we do not use the this keyword, it will cause a problem: the problem of local variables hiding member variables
  • this keyword feature
    • is an object reference of the current class
    • Simply remember, it represents an object of the current class. Who calls this method, then the internal this of the method represents who
  • Application scenarios of this
    • Solve local variables to hide member variables

4. Object-oriented construction method

  • Construction method overview and role
    • Initialize members of an object
  • Constructor Format Features
    • The method name is the same as the class name; no return type, not even void
  • Construction method considerations
    • If we do not provide a constructor, the system will automatically provide a no-argument constructor.
    • If we give a constructor, the system will no longer provide a default no-argument constructor.
      • Note: At this time, if we want to use the no-argument constructor, we must give it ourselves. It is recommended to always give the no-argument constructor by yourself

5. Steps to create an object

  • object creation steps
    • (1): Load A.class file into memory
    • (2): Create space for s in the stack memory
    • (3): Open up space for student objects in heap memory
    • (4): Default initialization of the member variables of the student object
    • (5): Display initialization of the member variables of the student object
    • (6): Assign value to the member variable of the student object through the construction method
    • (7): After the initialization of the student object is completed, assign the object address to the s variable

6. Characteristics of object-oriented static keyword

  • 6.1 Features of the static keyword
    • Loaded as classes are loaded
    • takes precedence over object existence
    • Shared by all objects of the class
    • It can be called by the class name [the content of static modification is generally called: related to the class, class members]
  • 6.2 Notes on static
    • There is no this keyword in static methods
      • Static is loaded as the class is loaded, and this exists as the object is created.
      • Statics exist before objects.
    • Static methods can only access static member variables and static member methods [static can only access static, non-static can access static and non-static]

7. The difference between static variables and member variables

  • A: The affiliation is different
    • Static variables belong to classes, so they are also called class variables
    • Member variables belong to objects, so they are also called instance variables (object variables)
  • B: Different locations in memory
    • Static variables are stored in the static area of ​​the method area
    • Member variables are stored in heap memory
  • C: The memory appears at different times
    • Static variables are loaded as the class loads and disappear as the class disappears
    • Member variables exist when the object is created and disappear when the object disappears
  • D: call different
    • Static variables can be called by class name or by object
    • Member variables can only be called by object name

8. Code Blocks

  • A: Overview of code blocks
    • In Java, code enclosed in {} is called a code block.
  • B: Code block classification
    • According to their different positions and declarations, they can be divided into local code blocks, construction code blocks, static code blocks, and synchronized code blocks.
  • C: Application of common code blocks
    • a: local code block
      • Appears in the method; limits the life cycle of variables, releases early, and improves memory utilization
    • b: Construct code block
      • Appears outside the method in the class; the same code in multiple constructor methods is stored together, executed each time the constructor is called, and executed before the constructor
    • c: static code block
      • Appears outside the method in the class, with static modification
      • Appears outside the method in the class and adds static modification; it is used to initialize the class, execute it when it is loaded, and execute it only once.

9. Inheritance

  • 9.1 Overview of Inheritance
    • When the same attributes and behaviors exist in multiple classes, these contents are extracted into a single class, then multiple classes do not need to define these attributes and behaviors, just inherit that class.
  • 9.2 Inheritance Format
    • Class-to-class inheritance can be achieved through the extends keyword
    • class subclass name extends parent class name{}
    • This single class is called the parent class, base class or superclass; these multiple classes can be called subclasses or derived classes
  • 9.3 Benefits of Inheritance
    • a: Improve the reusability of the code
    • b: Improve the maintainability of the code
    • c: The relationship between classes and classes is a prerequisite for polymorphism
  • 9.4 Disadvantages of inheritance
    • Class coupling is enhanced.
    • The principle of development: high cohesion, low coupling.
    • Coupling: Class-to-Class Relationship
    • Cohesion: the ability to accomplish something by yourself
  • 9.5 Notes on inheritance
    • a: Subclasses can only inherit all non-private members of the parent class (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 (to be discussed later) keyword.
    • c: Don't inherit for part of the function
  • 9.6 Relationship of Member Variables in Inheritance
    • A: The name of the member variable in the subclass is different from that of the member variable in the parent class
    • B: The member variable in the subclass has the same name as the member variable in the parent class
    • Search order for accessing a variable in subclasses ("nearest rule")
      • a: Find it in the local scope of the method of the subclass, and use it if there is one
      • b: Find it in the member range of the subclass, use it if there is one
      • c: Find it in the member range of the parent class, use it if you have it
        • d: If it is still not found, report an error
  • 9.7 Notes on Constructors in Inheritance
    • The parent class does not have a parameterless constructor, what should the child class do?
      • a: Add a parameterless constructor to the parent class
      • b: The subclass uses super to display and call other constructors with parameters of the parent class
      • c: The subclass calls other constructors of this class through this
      • Other constructs of this class must also first access the parent class construct
    • B: Precautions
      • super(...) or this(....) must appear on the first statement

10. The difference between this and super

  • A: Bring out super through questions
    • Subclass local scope to access parent class member variables
  • B: Tell me the difference between this and super
    • this represents a reference to an object of this class
    • super represents the identity of the storage space of the parent class (which can be understood as a reference to the parent class, and can operate the members of the parent class)
  • C: The use of this and super
    • a: call member variable
      • this. member variable calls the member variable of this class
      • super. member variable calls the member variable of the parent class
    • b: call the constructor
      • this(...) calls the constructor of this class
      • super(...) calls the constructor of the parent class
    • c: call member method
      • this. member method calls the member method of this class
      • super. member method calls the member method of the parent class

11. Overview of method rewriting and its application

  • A: What is method overriding
    • The same method declaration (method name, parameter list, return value type) appears in the subclass as in the parent class, also known as method overriding and method overriding.
  • B: What is the difference between Override and Overload? Can Overload change the return value type?
  • C: Application of method overriding
    • When the subclass needs the function of the parent class, and the function main subclass has its own unique content, it can override the method in the parent class.
    • In this way, the function of the parent class is inherited, and the specific content of the subclass is defined.
  • Method Override Considerations
    • a: Private methods in the parent class cannot be overridden, because the parent class's private methods cannot be inherited by subclasses at all
    • b: When the subclass overrides the parent class method, the access rights cannot be lower, it is best to be consistent
    • c: The static method of the parent class, the subclass must also be rewritten through the static method. In fact, this is not a method rewriting, but the phenomenon is true. As for why it is not a method rewriting, I will explain it in polymorphism.

12. Final keyword overview

  • A: Why is there a final
    • Due to the phenomenon of method overriding in inheritance, sometimes we do not want subclasses to override methods of superclasses. In this case, java provides us with a keyword: final
  • B:final overview
    • The final keyword is the final meaning, which can modify classes, variables, and member methods.
  • C: final modification features
    • Modified class: Modified class cannot be inherited
    • Modified method: Modified method cannot be overridden
    • Modified variables: Modified variables cannot be reassigned, because this quantity is actually a constant
  • **D:final keyword modifies local variables**
    • Primitive type, the value cannot be changed
    • Reference type, the address value cannot be changed

19. Object-oriented class names as formal parameters

  • case
/*要的都是对象具体类的作为参数传递的问题*/
class Student {
    public void show() {
        System.out.println("student.....show.............") ;
    }
}

class StudentDemo {
    public void method(Student s) {
        s.show() ;
    }
}


// 测试类
class ArgsDemo {
    public static void main(String[] args) {
        // 创建StudentDemo的对象
        StudentDemo sd = new StudentDemo() ;
        // 创建Student的对象
        Student s = new Student() ;
        // 调用方法
        // sd.method(s) ;
        sd.method(new Student()) ;
    }
}

20. Object-oriented abstract class name as a formal parameter

  • case
/*抽象类作为参数的时候如何进行调用*/
abstract class Animal {
    // 定义一个抽象方法
    public abstract void eat() ;
}

// 定义一个类
class Cat extends Animal {
    public void eat(){
        System.out.println("吃.................") ;
    }
}


// 定义一个类
class AnimalDemo {
    public void method(Animal a) {
        a.eat() ;
    }
}

// 测试类
class ArgsDemo2  {
    public static void main(String[] args) {
        // 创建AnimalDemo的对象
        AnimalDemo ad = new AnimalDemo() ;
        // 对Animal进行间接实例化
        // Animal a = new Cat() ;
        Cat a = new Cat() ;
        // 调用method方法
        ad.method(a) ;
    }
}

21. Object-oriented interface name as a formal parameter

  • case
/*接口作为参数的时候我们如何进行调用*/
interface Jump {
    // 跳高接口
    public abstract void jump() ;
}

// 定义一个子类
class JumpImpl implements Jump {
    public void jump(){
        System.out.println("jump.............................") ;
    }
}

// 定义一个类
class JumpDemo {
    public void method(Jump jump) {
        jump.jump();
    }
}

// 测试类
class ArgsDemo3  {
    public static void main(String[] args) {
        // 1. 创建JumpDemo对象
        JumpDemo jd = new JumpDemo() ;
        // 2. 调用method方法
        // 对Jump进行间接实例化
        Jump jump = new JumpImpl() ;
        jd.method(jump) ;
    }
}

22. Object-oriented class names as return value types

  • case
/*具体类作为返回值类型*/
class Student {
    public void show() {
        System.out.println("student....show.....................") ;
    }
}

// 定义一个类
class StudentDemo {
    public Student getStudent() {
        return new Student() ;
    }
}

// 测试类
class ReturnDemo {
    public static void main(String[] args) {
        // 创建StudentDemo的对象
        StudentDemo sd = new StudentDemo() ;
        // 调用 public Student getStudent()
        Student s = sd.getStudent() ;
        // 调用方法
        s.show() ;
    }
}

23. Object-oriented abstract class name as return value type

  • case
/*抽象类作为返回值类型*/
abstract class Animal {
    public abstract void eat() ;
}

// 定义一个子类
class Cat extends Animal {
    public void eat(){
        System.out.println("吃..........................") ;
    }
}

// 定义一个类
class AnimalDemo {
    public static Animal getAnimal() {   
        // Animal a = new Cat() ;
        // return a;
        return new Cat() ;
    }   
}

// 测试类
class ReturnDemo2  {
    public static void main(String[] args) {
        // 调用AnimalDemo的getAnimal这个方法
        Animal a =  AnimalDemo.getAnimal() ;
        // 调用方法
        a.eat() ;
    }
}

24. Object-oriented interface name as return value type

  • case
/*接口作为返回值类型*/
interface Jump {
    public abstract void jump() ;
}

// 定义一个子类
class JumpImpl implements Jump {
    public void jump(){
        System.out.println("jum....................") ;
    }
}

// 定义一个类
class JumpDemo {
    public static Jump getJump() {
        return new JumpImpl() ;
    }
}

// 测试类
class ReturnDemo3  {
    public static void main(String[] args) {
        // 调用getJump方法
        Jump jump = JumpDemo.getJump() ;
        // 调用
        jump.jump() ;
    }
}

25. Package definition and package

  • 25.1 Overview and role of the package keyword
    • A: Overview of the package: just the folder
    • B: The role of the package: It is used to solve the problem that files with the same name cannot exist in the same path (classification management)
    • C: Division of packages: by function, by module
  • 25.2 Package Definition and Precautions
    • A: Define the format of the package
      • package package name;
      • Multi-level package use. Separate
    • B: Notes on defining packages
      • A: The package statement must be the first executable code of the program
      • B: There can only be one package statement in a java file
      • C: If there is no package, the default means no package name

26. Overview and use of import keyword

  • A: Overview of the guide package
    • For access between classes under different packages, we found that every time we use classes under different packages, we need to add the full path of the package. kind of hard. At this time, java provides the function of importing packages
  • B: Guide package format
    • import package name;
    • Notice:
    • This way imports are to the name of the class.
    • Although it is possible to write * at the end, it is not recommended.

27. Testing of the Four Permission Modifiers

  • modifier
    • Four permission modifiers: private (private), default, protected (protected), public (public)
  • in conclusion
*            本类   同一个包下  不同包下(子类) 不同包下(无关类)
* private      Y       
* 默认         Y       Y
* protected    Y       Y          Y
* public       Y       Y          Y           Y

28. Common modifiers used by classes and their components

  • A: Modifier:
    • Permission modifiers: private, default, protected, public
    • State modifiers: static, final
    • Abstract modifier: abstract
  • B: Class:
    • Permission modifiers: default modifier, public
    • State Modifier: final
    • Abstract modifier: abstract
    • The most used is: public
  • C: member variable:
    • Permission modifiers: private, default, protected, public
    • State modifiers: static, final
    • The most used is: private
  • D: Construction method:
    • Permission modifiers: private, default, protected, public
    • The most used is: public
  • E: member method:
    • Permission modifiers: private, default, protected, public
    • State modifiers: static, final
    • Abstract modifier: abstract
    • The most used is: public
  • F: Other combination rules:
    • Member variable: public static final
    • Member methods: public static / public abstract / public final

29. Internal class overview and access features

  • A: Overview of inner classes:
    • If a class is defined inside another class, the class is called an inner class.
    • Example: A class B is defined in class A, and class B is an inner class.
  • B: Inner class access features
    • a: Inner classes can directly access members of outer classes, including private ones.
    • b: To access the members of the inner class, the outer class must create an object.
  • 29.1 Inner class classification and direct use of member inner classes
    • A: Classify according to inner class position
      • Member position: A class defined in a member position is called a member inner class.
      • Local location: A class defined in a local location is called a local inner class.
    • B: member inner class
      • How to directly access members of inner class in test class.
      • Format: outer class name. inner class name object name = outer class object. inner class object;
  • 29.2 Common Modifiers and Applications of Member Inner Classes
    • A: Modifiers for member inner classes:
      • private for data security
      • static for easy access to data
      • Precautions:
        • a: The outer class data accessed by the static inner class must be statically decorated.
        • b: member methods can be static or non-static
    • B: The access method of member inner class after static modification is: * Format: outer class name. inner class name object name = new outer class name. inner class name ();
  • 29.3 Problems with local inner classes accessing local variables
    • A: You can directly access the members of the outer class
    • B: You can create an inner class object and call the inner class method through the object to use the local inner class function
    • C: Local inner class access to local variables must be modified with final
      • why?
        • Because the local variable will disappear when the method is called, at this time, the local object does not disappear from the heap memory immediately, and the variable is still used. In order to allow the data to continue to be used, it is modified with fianl, so that what is stored in the heap memory is actually a constant value.
        • When we add final, it actually prolongs the life cycle. In fact, it is a constant. The constant is in the constant pool and in the method area.

30. Anonymous inner classes

  • 30.1 Format and understanding of anonymous inner classes
    • A: Anonymous inner class: It is a simplified way of writing local inner class.
    • B: Premise: There is a class or interface; the class here can be a concrete class or an abstract class.
    • C:Format:
new 类名或者接口名(){
     重写方法;
} ;
  • 30.2 What is the essence?
    • Is an anonymous object of subclasses that inherits the class or implements the interface.
    • Anonymous inner class interview questions
      • A: Interview questions
按照要求,补齐代码
interface Inter { void show(); }
class Outer { //补齐代码 }
class OuterDemo {
public static void main(String[] args) {
        Outer.method().show();
    }
}
要求在控制台输出”HelloWorld”

- 答案
//补齐代码
public static Inter method() {
    //匿名内部类
    return new Inter() {
        public void show(){
            System.out.println("HelloWorld") ;
        } ;
    }
}

Guess you like

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