Java study notes (3) - encapsulation, inheritance, polymorphism

1. Packaging

concept:

Some information of the class is hidden inside the class, and the external program is not allowed to access it directly, but the operation and access to the hidden information are realized through the methods provided by the class.

Implementation steps:

Modify the visibility of the property - set to private.

Create getter/setter methods - for reading and writing properties.

Add property control statements to getter/setter methods—judging the validity of property values.

Bag:

effect:

Manage Java files; resolve file conflicts with the same name.

Definition Name:

package package name

It must be placed in the first line of the Java source program, and the package names can be separated by ".".

Packages in the system:

java.(function).(class)

java.lang.(classes) - classes that contain the basics of the Java language

java.util. (class) - contains various tool classes in the Java language

java.io. (class) - a class that contains functions related to input and output

Package usage:

(1) import package name

(2) The naming convention of packages in Java is all lowercase spelling

(3) ~~.* - load all files under it

Access modifiers:

The access scope of properties and methods can be modified.
 
access modifier this class Same package Subclass other
private      
default    
protected  
public
 
 
 
 
 
 

this keyword:

1. The this keyword represents the current object. this.properties - Manipulate the properties of the current object. this.method - the method to invoke on the current object.
2. When encapsulating object properties, the this keyword is often used.

inner class:

definition:

An inner class is a class defined inside another class. Correspondingly, a class containing an inner class is called an outer class.

effect:

1. The inner class provides better encapsulation, which can hide the inner class within the outer class, and does not allow other classes in the same package to access the class.
2. The methods of the inner class can directly access all the data of the outer class, including private data.

Classification:

Member inner class, static inner class, method inner class, anonymous inner class.
****To use the methods of the inner class, first create an outer class object, create the inner class object through the outer class object .new inner class() form, and finally call the inner class method. ****

Member inner class (normal inner class):

1. After defining the member inner class, you must use the outer class object to create the inner class object, instead of directly going to new an inner class object, that is: inner class object name = outer class object.new inner class ();
2. .class file of member inner class: outer class name $ inner class name .class
3. If the outer class and the inner class have the same member variables or methods, the inner class accesses its own member variables or methods by default. If you want to access the member variables or methods of the outer class, you need to use the this keyword (external class.this.member variable /method).

Static inner class (static decorated inner class):

1. A static inner class cannot directly access the non-static members of the outer class, but it can be accessed by means of new outer class (). member.
2. If the static members of the outer class and the members of the inner class have the same name, you can access the static members of the outer class through class name.static member; if they are not the same, you can directly call the static members of the outer class through the member name. 3. When creating a static inner class object, you do not need the object of the outer class, directly "inner class object name = new inner class (); "

Method inner class (the inner class is defined in the method of the outer class, and the method inner class can only be used within the method):

1. The inner class of the method cannot use the access control and static modifiers.

2. Inheritance (a relationship between classes and classes, inheritance in Java is single inheritance)

Advantage:

The subclass has all the properties and methods of the parent class (the private modification is invalid); implements code reuse.

Grammar rules:

class subclass extends superclass

Override of method:

If the subclass is not satisfied with the method inherited from the parent class, it can override the method inherited by the parent class. When calling the method, the method of the subclass will be called first. The return value type, method name, parameter type and number must be the same as the method of the parent class, which is called method overriding.

Inherited initialization order:

Initialize the parent class and then initialize the subclass; first execute the properties in the initialization object, and then execute the initialization in the constructor.

final keyword:

final can modify classes, methods, properties and variables.
Final modified class, the class is not allowed to be inherited.
final modified method, the method cannot be overridden.
The final modification attribute, the attribute of the class will not be implicitly initialized (the initialization attribute of the class must have a value) or assigned in the constructor (but only one of them can be selected).
If the variable is modified by final, the value of the variable can only be assigned a value once, that is, it becomes a constant.

super keyword:

Used inside an object to represent a parent class object.
Access the properties of the parent class: super.age
The method of accessing the parent class: super.eat()
If the subclass constructor does not explicitly call the superclass constructor, and there is no parameterless constructor in the superclass, a compilation error occurs.

object class:

The object class is the parent class of all classes. If a class is not explicitly marked to inherit another class using the extends keyword, then this class inherits the object class by default.
tostring() method: Returns the hash code of the object (object address string), which can be used to represent the properties of the object by overriding the tostring() method.
equals() method: compares whether the reference of the object points to the same memory address. In general, when comparing two objects, the comparison is whether the value is consistent, so it needs to be rewritten.

3. Polymorphism (inheritance is the basis of polymorphism)

Various forms of objects:

1. Reference polymorphism:

The reference of the parent class can point to the object of this class/subclass.

2. Method polymorphism:

When an object of this class is created, the method called is a method of this class.
When creating a subclass object, the method called is the method overridden or inherited by the subclass.

Reference type conversion:

1. Up-type conversion (implicit/automatic type conversion) is a conversion from a small type to a large type.
2. Down type conversion (forced type conversion) is the conversion of a large type to a small type.
3. The instanceof operator is used to solve the type of the reference object and avoid the security problem of type conversion.

Abstract class:

1. Grammar definition:

If an abstract class is modified with the abstract keyword, the class is an abstract class.

2. Application scenarios:

a. In some cases, a parent class only knows what methods its subclasses should contain, but cannot know exactly how these subclasses implement these methods.
b. An abstract class is abstracted from multiple classes with the same characteristics, and this abstract class is used as the template of the subclass, thereby avoiding the randomness of subclass design.

3. Function:

Restriction rule subclasses must implement certain methods, but are not concerned with implementation details.

4. Rules of use:

a, abstract defines an abstract class.
b. Abstract defines abstract methods, which are just declarations and do not need to be implemented.
c. A class that contains abstract methods is an abstract class.
d. An abstract class can contain ordinary methods or no abstract methods.
e. Abstract classes cannot be created directly, but reference variables can be defined.

interface:

1. Interface concept:

An interface can be understood as a special class consisting of global constants and public abstract methods. A class is a specific implementation body, and an interface defines the specifications that a certain batch of classes need to comply with. The interface does not care about the internal data of these classes, nor the implementation details of the methods in these classes. It only stipulates that these classes must provide certain some methods.

2. Interface definition:

Unlike class definitions, interfaces are defined using the interface keyword instead of the class keyword.
[modifier] abstract interface interface name [extends parent interface 1, parent interface 2...]
{
Zero or more constant definitions...
Define a definition of multiple abstract methods...
}
The interface is used to be inherited and implemented. The modifier is generally public, and the interface cannot be modified with private and protected.
The methods in an interface are abstract methods.
A method in an interface cannot have a method body. //void geta();

3. Use the interface:

A class can implement one or more interfaces using the implements keyword. In Java, a class can only inherit one parent class, which is not flexible enough. It can be supplemented by implementing multiple interfaces.
The syntax for inheriting a parent class to implement an interface is:
[modifier] class class name extends parent class implements interface 1, interface 2...
{
Class body part//If you inherit an abstract class, you need to implement the inherited abstract method; you need to implement the abstract method in the interface
}
If you want to inherit the parent class, you must inherit the parent class before implementing the interface.
Interfaces are often used in conjunction with anonymous inner classes.
An anonymous inner class is an inner class without a name, which is used to focus on the implementation and not the name of the implementing class.
Interface i=new Interface()
{
public void method(){
~~
}
}

4, UML

Unified Modeling Language (Unified Modeling Language/Standard Modeling Language) is a graphical language that supports modeling and software system development, providing modeling and visualization support for all phases of software development.
Commonly used UML diagrams - use case diagrams, sequence diagrams, class diagrams
Common modeling tools - Visio, Rational Rose, PowerDesign

 

Guess you like

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