Object-oriented features
1. Encapsulation 2. Inheritance 3. Polymorphism
First, the package
1. Class: Combine the static properties of a class of things with the dynamically executed operations to get the class. of abstract
2. Object: the instance of a class has a specific life cycle
3. Constructors for classes
Feature: function name is the same as type; there is no return type; can have or can have no arguments, can have more than one constructor
A constructor is always called automatically when an object is generated
The system automatically generates an argument-free constructor when no constructor is defined
Assignment problems for constructors:
When an object is created, its various types of member variables are automatically initialized for assignment. such as byte (0) int (0) Boolean (flase) all reference type (NULL)
Be aware that local variables are not initialized automatically and must be initialized before they are used.
4.static
Static members belong to the class itself, not to the object, and are shared by all objects of the class
Only static members that are not private can be accessed through the class name, for example, Student.name ()
Static methods: Static variables can be accessed by non-static variables, and static variables cannot be accessed by non-static variables
Static methods cannot refer to this and the Super keyword in any way
5.this
This is a system-implied pointer that is automatically attached to a non-static member function parameter list;
The keyword this represents the object that is calling the method, including the method caller in the normal method and the new object created by the method in the constructor method for this run.
6.final
1) Modify the entire class: Indicates that the class cannot be inherited public final class A
2) modifies several properties in a class: Indicates that the property must be assigned and can only be assigned once (note that the default value is not a true assignment)
The first method, which is initialized while defining the member variable, and the second, in the constructor in the class.
3) Several methods in the decorated class: Indicates that the method can be inherited by the quilt class, but cannot be overridden by a quilt class
7. Function overloading
For a function in the same class (typically, a function with similar functions is recommended for function overloading)
Same name, different parameter
Independent of return value type
Ii. inheritance
1. Definition: A new class obtains its existing properties and methods from existing classes
class Subclass entends Superclass
2. principles: General to special, inclusive, use of the relationship
Such as: Parent object = Subclass Object//dog can be seen as an animal
3. Permissions issues
Subclass objects can access all members of the parent class except private members
4. Attention Issues
1) Java only supports single inheritance, does not allow multiple inheritance (a class has several parent classes); multiple layers of inheritance
2) Subclasses cannot inherit the constructor of the parent class, but can call the constructor of the parent class by Super
3) Non-private members can be inherited
5.super
The constructor method that calls the parent class must be a super (argument list) and cannot write directly to the class name of the parent class
Each subclass constructs the first statement of the method, implicitly calling Super ()
Explicitly write super (parameter list)
6. Method overrides
Refers to a subclass that overrides an existing method in the parent class (such as a fruit class that is dissatisfied with a method in the parent class, and can override the parent class's method)
The overriding method must have the same method name, parameter list, return value type as the overridden method
Note: When overriding a method, you cannot use more restrictive access than the overridden method in the parent class.
In parent class: public void FN ()
Cannot write private void Fn () in subclass
Three, polymorphic
1. Definition: A reference variable of a parent class that can point to the parent class object or to a subclass object, which can point at the current moment
Different, automatic methods of invoking different objects, which is polymorphic.
2. Meaning: Using polymorphism can be achieved--the same piece of code do different things
3. Precautions:
1) Subclasses can be assigned directly to a parent class reference, but the parent cannot be assigned directly to the subclass reference in any case
2) The parent class reference can only access members that inherit from the parent class, and cannot access the members that are unique to the child class
3) only if the parent reference itself points to a subclass object, the parent class reference can be cast to a subclass reference
Iv. Other Concepts
A) abstract class
1. When modifying a class with the abstract keyword, the class is called an abstract class
2. Abstract classes do not necessarily have abstract methods, but classes with abstract methods must be abstract classes
Abstract method: Any method that does not have a method body must use the keyword abstract to modify the abstract method public abstract void FN ()
3. cannot new abstract object, but can define a reference of an abstract class
such as a AA =new a (); This is wrong, because a is an abstract class
Can be used to implement all the abstract methods of a subclass B to: a aa=new B ();
4. Abstract classes can also implement polymorphic
II) interface
1. Definition: The collection of abstract methods and constant values is essentially a special abstract class
2. The properties of the interface definition must be public static final, and the definition of the interface must be public abstract, these modifiers can be omitted partially or completely
Interface it{publicstaticfinalint i=20; Public Abstract void f ();}
3. Interfaces can inherit multiple interfaces, that is, allow multiple inheritance
4. To implement only some of the methods in the interface, the abstract class
5. You can not new object, but you can use the subclass new
6. Multi-state can be achieved
7. Use: Thread creation, event handling, container organization, SERIALIZABLE interface
Java Object-oriented programming