Object-Oriented depth
1. Object-oriented three major features
A) Inherit inheritance
Subclasses can inherit properties and methods from the parent class
Subclasses can provide their own property methods
b) Package Encapsulation
Hide some properties and methods from the outside
Exposing certain properties and methods externally
c) polymorphic polymorphism
Multiple variations of adaptive variables
1.1 Package
A) Why encapsulation is required
Hides the complexity inside the object, exposing simple interfaces only externally. Easy to call outside, improve system scalability, maintainability;
b) Implement encapsulation
Using Access-rights characters
Member Four access rights
- public.
Project visible
- Protected protected
Class itself access
Other class access within the package
Sub-class access
- Default defaults (package visible)
Class itself access
Other class access within the package
- Private privately owned
Accessed only by this class itself
Two kinds of access rights for a class
- Public
All class access in the project
- Default
Class access within the same package
Packaging essentials
- Handling of properties of a class
A) generally use private unless subclass inheritance is required
b) Provide the Get/set access property, usually by public
c) Auxiliary methods only for this class private
D) methods that you want other classes to call public
1.2 Inheritance
Definition: A class is an abstraction of an object, and inheritance is an abstraction of a batch of classes
Meaning: Improving code reusability
public class Testextend {
public static void Main (string[] args) {
Mammal A = new mammal ();
A.eat (); Inherits the Eat () from the parent class;
A.pure ();
}
}
public class animal{
String name = "Cerebellum Axe";
int age = 2;
public void Eat () {
System.out.println ("eat");
}
}
public class mammal Extend animal{
public void Pure () {
System.out.println ("suckling");
}
}
Other
- Java has only single inheritance
- The public parent class for all classes is Object
1.3 override of Method
a) = = Method name, same parameter
b) <= return value type and exception type subclass less than parent class
c) >= access permission subclass greater than parent class
Object class
The object class is the base class for all classes and contains many methods
1. Example: overriding ToString (Default return package + class [email protected] hash code)
- 2. Comparison of objects = = and Equal
a) = =
A) Compare the values of the two basic variables
b) Compare two reference types memory is the same, point to the same object
b) Equal
A) whether the contents of the two objects are the same
1.4 Super Key Words
b) Super is a reference to the immediate parent class object
c) The properties and methods covered by the quilt class in the parent class can be accessed through super
1.5 polymorphic
Polymorphism is an important feature of OOP, which is used to implement dynamic linking, that is, the final state of a program is determined only by the re-execution process and not by the compilation period.
- How to use polymorphic
A) Two types of reference types
I. Compile-time Type: Type determination at the time of declaration
Ii. run-time type: determined by the actual type of the object
b) Three necessary conditions for polymorphic existence
I. To have inheritance
II. To have a method to rewrite
Iii. Parent class reference to child class object
Example
Note: Type conversions for reference types
1.6 Final keyword
- The final keyword can be used to modify variables, classes, methods
A) modifier variable: Equivalent to defining a constant that cannot be changed
b) Modification method: cannot overwrite in subclass
c) Modifier class: Cannot be inherited
Java Learning · The first knowledge object-oriented deep one