Java EE Basics (eight)

Source: Internet
Author: User

1. Object-oriented (overview and classification of code blocks) (Learn) (interview will ask, development is not used or very rarely)
    • A: code block overview
      • In Java, code enclosed in {} is called a code block.
    • B: code block classification
      • Depending on its location and declaration, it can be divided into local code blocks, building blocks of code, static blocks of code, and synchronizing code blocks (multithreading).
    • C: Application of common code blocks
      • A: Local code block
        • To appear in a method; limit a variable's life cycle, release early, and improve memory utilization
      • B: Building blocks of code (initialization blocks)
        • Occurs outside of a method in a class, the same code is stored together in multiple constructor methods, each call construct executes, and is executed before the method is constructed
      • C: Static code block
        • Occurs outside of a method in a class, with a static modifier, to initialize the class, execute at load time, and execute only once.
        • Typically used to load the drive
2, Object-oriented (code block interview questions)
    • A: See Program Write results
    • class Student {    static {        System.out.println("Student 静态代码块");    }    {        System.out.println("Student 构造代码块");    }    public Student() {        System.out.println("Student 构造方法");    }}class Demo2_Student {    static {        System.out.println("Demo2_Student静态代码块");    }    public static void main(String[] args) {        System.out.println("我是main方法");        Student s1 = new Student();        Student s2 = new Student();    }}
3. Object-oriented (demonstration of succession case)
    • A: Inheritance (extends)
      • To have a relationship between a class and a class, the child parent class relationship
    • B: Inheritance Case Demo:
      • Animal, Cat, dog
      • Define two properties (color, number of legs) two functions (Eat, sleep)
    • C: Case Demo
      • Before using inheritance
    • D: Case Demo
      • After using inheritance
4. Object-oriented (benefits and drawbacks of inheritance)
    • A: Benefits of inheritance
      • A: Improved reusability of the code
      • B: Improved maintainability of the code
      • C: The relationship between classes and classes is a prerequisite for polymorphism
    • B: The drawbacks of inheritance

      • The coupling of the class is enhanced.

      • Principles of development: cohesion, low coupling.

      • Coupling: Class-to-class relationships
      • Cohesion: the ability to do something yourself
5. Object-oriented (inheritance characteristics of classes in Java)
    • Inheritance characteristics of classes in A:java
      • A:java only supports single inheritance and does not support multiple inheritance. (a son can only have one father)
        • Some languages are supported for multiple inheritance, format: Extends Class 1, Class 2,...
      • B:java supports multilayer inheritance (inheritance system)
    • B: Case Demo
      • Inheritance characteristics of classes in Java
        • If you want to use all the functions of this system to create objects with the lowest class
        • If you want to see the common features of this system, look at the top class
6. Object-oriented (considerations of inheritance and when to use inheritance)
    • A: Considerations for inheritance
      • A: Subclasses can only inherit all non-private members of the parent class (member methods and member variables)
      • B: Subclasses cannot inherit the constructor of the parent class, but can access the parent class construction method through the super (immediately speaking) keyword.
      • C: Do not inherit for part of the function
      • Project Manager name work number salary bonus
      • Programmer's name work number salary
    • B: When to use inheritance

      • Inheritance actually embodies a relationship: "is a". Person Student Teacher Fruit Apple Banana Orange

      Adopt the Hypothesis method. If there are two classes, a, B. Only if they meet the one of A is B, or B is a, you can consider using inheritance.

7. Object-oriented (relationship of member variables in inheritance)
    • A: Case Demo
      • A: variable with no Name
      • B: Variable with the same name
8. Object-oriented (the difference and application of this and super)
    • What do a:this and super mean?
      • This: Represents a reference to the current object, who calls me, I represent who
      • Super: A reference to the parent class of the current object
    • The difference between the use of B:this and super
      • A: Calling member variables
        • This member variable calls the member variable of this class, or it can call the member variable of the parent class
        • Super. member variables call member variables of the parent class
      • B: Call construction method
        • This (...) calls the construction method of this class
        • Super (...) constructor method for calling parent class
      • C: Call member method
        • This member method calls the member method of this class, or it can call the method of the parent class
        • Super. Member methods call the parent class's Members method
9. Object-oriented (relationship of construction method in inheritance)
    • A: Case Demo
      • All constructor methods in a subclass access the constructor of the parent's hollow parameter by default
    • B: Why?

      • Because subclasses inherit data from the parent class, data from the parent class may also be used.
      • Therefore, before subclasses are initialized, it is important to complete the initialization of the parent class data first.

      • In fact:

        • The first statement of each constructor method defaults to the top-most parent class of the super () object class.
10. Object-oriented (considerations for constructing methods in inheritance)
    • A: Case Demo
      • The parent class does not have a parameterless constructor method, what about subclasses?
      • Super Solution
      • This resolves
    • B: Precautions
      • Super (...) Or this (...) must appear on the first statement of the constructor method
11. Object-Oriented (interview questions in succession)
    • A: Case Demo
    • 看程序写结果1class Fu{    public int num = 10;    public Fu(){        System.out.println("fu");    }}class Zi extends Fu{    public int num = 20;    public Zi(){        System.out.println("zi");    }    public void show(){        int num = 30;        System.out.println(num);        System.out.println(this.num);        System.out.println(super.num);    }}class Test1_Extends {    public static void main(String[] args) {        Zi z = new Zi();        z.show();    }}看程序写结果2class Fu {    static {        System.out.println("静态代码块Fu");    }    {        System.out.println("构造代码块Fu");    }    public Fu() {        System.out.println("构造方法Fu");    }}class Zi extends Fu {    static {        System.out.println("静态代码块Zi");    }    {        System.out.println("构造代码块Zi");    }    public Zi() {        System.out.println("构造方法Zi");    }}Zi z = new Zi(); 请执行结果。
12. Object-Oriented (member method relationship in succession)
    • A: Case Demo
      • A: Methods that do not have the same name
      • B: Method with the same name
13. Object-oriented (overview of method overrides and their applications)
    • A: What is method rewriting
      • Rewrite: The child parent class appears exactly the same way (note: The return value type can be a child parent class, this we learned the object-oriented talk)
    • B: Application of Method rewriting:
      • You can override a method in a parent class when the subclass needs the functionality of the parent class, and the feature body subclass has its own unique content. In this way, the function of the parent class is inherited, and the content specific to the subclass is defined.
    • C: Case Demo
      • A: Define a mobile phone class.
14. Object-oriented (considerations for method rewriting)
    • A: Method Rewriting considerations

      • A: Private methods in the parent class cannot be overridden
        • Because the parent class private method subclass simply cannot inherit
      • B: When subclasses override parent class methods, access permissions cannot be lower
        • It's best to be consistent
      • C: Parent class static methods, subclasses must also be overridden by static methods

        • In fact, this is not the method of rewriting, but the phenomenon is true, as to why not the method rewrite, polymorphic I will explain (static only cover static)
      • When a subclass overrides a parent class method, it is best to declare exactly the same.

    • B: Case Demo
      • Method Override Considerations
15, Object-oriented (method rewrite interview questions)
    • A: Method rewrite the face question

      • What is the difference between override and overload? Can overload change the return value type?
      • Overload can change the return value type, just look at the parameter list
      • Method overrides: A method that is identical to the method declaration in the parent class appears in the subclass. With respect to the return value type, the return value is a consistent (or child-parent)

      • Method Overloading: The method names that appear in this class are the same as the parameter lists. Is independent of the return value type.

      • When a subclass object invokes a method:

        • Look for the subclass itself, and then look for the parent class.
16. Object-oriented (using student and teacher cases before inheritance)
    • A: Case Demo
      • Use pre-inheritance student and teacher cases
      • Properties: Name, age
      • Behavior: Eating
      • Teachers have a unique approach: lectures
      • Students have a unique approach: learning
17. Object-oriented (use of inherited student and teacher case)
    • A: Case Demo
      • Use of inherited student and teacher cases
18_ Object-oriented (CAT and dog case analysis, implementation and testing)
    • A: Cat and dog Case study
    • B: Case Demo
      • Cat and dog Case inheritance version
      • Properties: Color of hairs, number of legs
      • Behavior: Eating
      • Cat-specific behavior: Catching mouse Catchmouse
      • Dog-specific behavior: housekeeping lookhome
19, Object-oriented (final keyword decoration class, methods and variables of the characteristics)
    • A:final Overview
    • B:final Modification Features
      • Modifier class, class cannot be inherited
      • Modifier variable, the variable becomes a constant and can only be assigned once
      • Modification method, method cannot be overridden
    • C: Case Demo
      • Final retouching features
20. Object-oriented (final keyword decorated local variable)
    • A: Case Demo

      • Method inside or on a method declaration (learn)

      • Basic type, value cannot be changed

      • Reference type, which is the address value cannot be changed, the property in the object can be changed
21. Object-Oriented (the time of initialization of the final modified variable)
    • A:final the initialization time of a modified variable
      • Display initialization
      • Before object construction is complete

Java EE Basics (eight)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.