“This is the 13th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”


Related articles

Java with Notes: Java with Notes


  • Brother meng, I really can’t support, recently busy of die, really have no time to write !!!! All these old manuscripts (hydrology) have been forced to issue! Ah!

1. Object-oriented concepts: Example: An elephant in a refrigerator. Process-oriented: Emphasizes process (action). Open the fridge store the elephant closes the fridge

Object orientation: The emphasis is on objects (entities). The refrigerator has the functions of opening, storing and closing. Refrigerator open refrigerator storage refrigerator closed features: Object oriented is a common idea, in line with people's thinking habits. The appearance of object - oriented simplifies complex problems. The appearance of object orientation makes the executor in the process become the leader in the object.Copy the code

2. The relationship between classes and objects: A class is an abstraction of objects with the same behavior characteristics. A description of something. An object is an actual entity in the abstract concept of a class. A description of such a thing. In Java, objects are created by new.

Public class Car {private int num; Private String color; Public int getNum() {return num; } public String getColor() { return color; } public void setNum(int num) { this.num = num; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Car{" + "num=" + num + ", color='" + color + '\'' + '}'; {Car Car = new Car(); // CAR is a reference variable of a class type that refers to an object of that class.} Anonymous object: When an object method is called only once, it can be simplified to anonymous. Example: new Car().getcolor ();Copy the code

3. Encapsulation (one of the three features) : It hides the attributes and implementation details of an object and only provides public access.

Benefit: Isolates change. Easy to use. Improve reuse. Improve security. Principle of encapsulation: Hide content that doesn't need to be made public. Hide attributes and provide public access to them. Private: private, which is a permission modifier used to modify members. Private content is only valid in this class.Copy the code

Constructor: Features: 1) The function name is the same as the class name. 2) Do not define return value types. 3) No concrete return value.

Function: Initializes an object. ———— Build the function that is called when the object is created. Note: 1) Characteristics of the default constructor. All objects created must be initialized via constructors. A class that has no constructor defined has a default empty constructor. If the specified constructor is defined, the default constructor in the class is gone. 2) Multiple constructors exist in overloaded form. Constructor: When an object is created, the corresponding constructor is called to initialize the object. Generic functions: after the object is created, they are called only when the function function is needed. Constructor: called only once when the object is created. General functions: After an object is created, it can be called multiple times. When do you define a constructor? When describing something, something that it already has is defined in the constructor.Copy the code

5, this keyword: features: this keyword represents the function of the object reference. That is, a reference to an object of this class. That is, whichever object calls this represents that object.

When to use the this keyword? When the function class needs to call the object of the function. When a local variable and a member variable have the same name, you can use the this keyword to distinguish them. Note that this calls the constructor only on the first line of the constructor because the initialization action is performed first. Public Boolean compare(Person p){return this.age == p.age; }Copy the code

The static keyword is used to modify members (member variables and member functions).

The modified members have the following characteristics: 1) they are loaded as the class is loaded. 2) Prior to the existence of objects. 3) Shared by all objects. 4) Can be called directly by the class name. 1) Static methods can only access static members. 2) Static methods can not write this, super keyword. 3) The main function is static. What's the difference between a member variable and a static variable? 1) The life cycles of the two variables are different: member variables exist with the creation of the object and are released with the collection of the object. Static variables exist as the class is loaded and disappear as the class disappears. 2) Different calls: Member variables can only be called by objects. Static variables can be called by objects as well as by class names. 3) Different aliases: Member variables are also called instance variables. Static variables are called class variables. 4) Different data storage locations: member variable data is stored in objects in heap memory, so it is also called object specific data. Static variable data is stored in the method area. Static code block: Loaded as the class is loaded and executed only once. Purpose: Initializes a class. Because some classes don't need an object, they can call a static block of code directly. What's the difference between static methods and constructors? Static methods: Are loaded as the class is loaded and executed only once. Constructor: initializes the corresponding object. Construct code block: All objects can be initialized.Copy the code

Inheritance (two of the three features) : Java supports single inheritance, does not directly support multiple integration, but c++ multiple inheritance mechanism is improved. Single inheritance: A subclass can have only one immediate parent. Multiple inheritance: A subclass can have more than one direct parent class. ———— is not supported directly because there is invocation uncertainty when two parent classes have methods with the same name. It is represented in Java by multiple implementations!

Java supports multiple layers (multiple inheritance). C inherits B, and B inherits A. Inheritance systems arise when using an inheritance system: Look at the top-level classes in the system to understand the basic functionality of the system. Create the most subclass object in the system, complete the use of the function. Benefits: Improved code reuse. This leads to relationships between classes, providing the premise for a third feature polymorphism. When do you define inheritance? Inheritance is defined when there is an ownership relationship between classes. XXX is one of YYY, and XXX can inherit one of YYY. Owner: is a relationship. Specific embodiment: in the child and parent class: 1) member variables. Use this when members and local variables in this class have the same name. When a member variable in a child parent class has the same name, it is distinguished as a parent class by super. The difference between this and super: This represents a reference to an object of this class. Super represents the space of the parent class. Calss fu{int num = 4; } class zi extends fu{ int num = 5; Void show(){sout(this.num + "+" + super.num); {zi z = new zi(); z.show(); } print: 4 + 5 2) member functions. When there are identical member functions in the child and parent classes: the function of the child class is run. This phenomenon is called overlay operation. Two features of the function: 1) overload: in the same class. The phrase ". 2) Override (overwrite) : In subclasses, override is also called override (overwrite). Override. When overriding a subclass method, the permissions of the subclass must be greater than or equal to the permissions of the parent class. 2) Static can only override static, or be overridden by static. When to use overwrite operations? When extending a subclass of a class, the subclass needs to keep the functionality declaration, but needs to define the functionality specific to the subclass, the override operation is used. Calss fu{void show(){sout("old num"); } } class zi extends fu{ void show(){ sout("new pic"); sout("new img"); this.show(); {zi z = new zi(); z.show(); } 3) Constructor. When the subclass constructs the object, it is found that the parent class also runs when accessing the subclass constructor because there is an implicit statement in the first line of the subclass constructor. super(); ———— calls the constructor of the parent class. Instantiation of a subclass: by default, all constructors in a subclass access the constructor for empty arguments in the parent class.Copy the code

8. Final keyword inheritance disadvantages: break encapsulation. Prohibit inheritance with final. 1) You can modify classes. Methods, variables. 2) Modified classes may not be inherited. 3) Modified methods cannot be overridden. 4) The modified variable is a constant. Can only be assigned once during initialization. Why use final to modify variables? If a variable is fixed in the program, then 1 can use the data directly, and the variable name and value cannot change, plus final is fixed. Written specification: constants all letters in uppercase, multiple words, with _ between them. 5) Inner classes can only access modified local variables.

9, abstract class (based on inheritance) ————

Abstract methods must be defined in an abstract class. Abstract methods must be defined in an abstract class. The class must also be modified by abstract. 2) Abstract classes cannot be instantiated. Why is that? Because it doesn't make sense to call an abstract method. 3) An abstract class must be instantiated only after its subclass overrides all of its abstract methods. Otherwise, the subclass is still abstract. {abstract void howler (); } class dog extends dog {sout(" dog "); } class Wolf extends dog {sout(" ow ~~ ow ~~"); } raise five questions about abstract classes: 1) Are there constructors in abstract classes? Yes, used to initialize its subclass. 2) Can abstract classes not define abstract methods? Yes, but rarely, the purpose is not to let the class create objects. AWT's adapter object is such a class. Usually this class has a method body, but no content. 3) Which keywords cannot coexist with abstract keywords? Private cannot ———— because abstract methods must be overridden. Illegal modifier combination. ———— If the member is static, no object is needed, and the abstract method is meaningless. Final cannot be overwritten ————. 4) What are the similarities and differences between abstract classes and general classes? Different: General classes have enough information to describe things. Abstract classes may not have enough information to describe things. A general class cannot have a first abstract method, only a non-abstract method can be defined. Abstract methods can be defined in an abstract class as well as non-abstract methods. Generic classes can be instantiated. Abstract classes cannot be instantiated. Both describe things, and both define members internally. 5) Is an abstract class necessarily a parent class? It must be the parent. Because a class inherits from an abstract class, you must override all of its abstract methods to instantiate it. Otherwise, the class remains abstract!Copy the code

When the methods in an abstract class are all abstract methods, you can define the abstract class as an interface.

Member modifiers in interfaces are fixed: 1) member constants (global variables) : public static final 2) member functions (abstract methods) : The interface members are all public privileges. Instead of using inheritance, implements the interface. Interface demo{public static final int num = 4; void show(); } calss demoImpl implements demo{@override public void show(){sout(" demoImpl implements demo "); }} Multiple inheritance is not supported in Java because of the invocation uncertainty. So Java took the multi-inheritance mechanism and changed it to multi-implementation in Java. A class can implement multiple interfaces. Class Test implements interface 1, interface 2 {method body; } A class can implement multiple interfaces while inheriting from another class. Interfaces avoid the limitations of single inheritance. Details: Classes are inherited from each other. An implementation relationship is between a class and an interface. Interfaces are inherited from one another. And interfaces can be multiple inheritance. Implementing a subinterface overrides all the abstract methods of the subinterface and all the parent interfaces. Features: 1) Interfaces are exposed rules. 2) Interface is the function expansion of the program. 3) The appearance of interfaces reduces the coupling. 4) Interfaces can be used for multiple implementations. 5) There is an implementation relationship between a class and an interface, and a class can inherit a class and simultaneously implement multiple interfaces. 6) Interfaces can have inheritance relationships with each other. Anything exposed to the outside world can be called an interface. What are the similarities and differences between interface classes and abstract classes? The same: are constantly drawn up. Different: 1) Abstract classes need to be inherited, and only inherited. Interfaces need to be implemented, and can be implemented more than once. 2) Abstract methods and non-abstract methods can be defined in abstract classes. After subclasses inherit, non-abstract methods can be used directly. Only abstract methods can be defined in an interface and must be implemented by subclass sections. 3) The inheritance of the abstract class is a relation. In defining the basic common content of the system. The implementation of the interface is a like A relationship. In defining the additional functions of the system.Copy the code

11. Polymorphic definition: multiple forms of something.

Example: Parent class name = new subclass (); Among the animals is the cat. The dog. The cat object corresponds to the cat type. Cat x = new cat (); Cats are also one of the animals. We can also call cats animals. Animal y = new cat (); Animals are parent types extracted from concrete things like cats and dogs, and parent type references refer to subclass objects. Object polymorphism. The cat has both the form of a cat and the form of an animal. This is the polymorphism of the object. Simply put, an object corresponds to different types. Polymorphism is represented in code: a parent class or interface refers to an object of its subclass.Copy the code

The road ahead is long, I see no end, I will search high and low

If you think I bloggers write well! Writing is not easy, please like, follow, comment and give encouragement to the blogger ~ Hahah