In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to understand Java object-oriented". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas to study and learn "how to understand Java object-oriented".
I. object-oriented understanding
First of all, it is clear that, unlike process-oriented, object-oriented has its own unique advantages. To take a popular example, imagine that we want to get a chair, and for the process, we may need to buy wood, design drawings, and finally cut it. But for object-oriented, we just need to find a furniture store and buy a chair.
Object-oriented is also one of the characteristics of Java language. In fact, it is not difficult to see from idea that this object-oriented language has:
1. Easy to maintain
two。 Easy to reuse
3. Easy to expand
II. Three major features 2.1inheritance (extends):
As the name implies, it is a process of obtaining the original foundation and operating on this basis. A process in which new properties can be added using the definition of an existing class as a basis. (in java, inheritance is used between subclasses and parents.)
About inheritance:
1. The subclass cannot selectively inherit the parent class, as long as it chooses to inherit, it will inherit all (except the private-decorated properties and methods in the parent class).
two。 A subclass is an extension of the parent class and can have its own properties and methods
3.java is single inheritance, but can inherit multiple (that is, a subclass can inherit only one parent class, but a parent class can have multiple subclasses. In popular terms, your father can have multiple children, but these children have only one father. )
2.2 Encapsulation (private):
Definition of encapsulation:
Privatize some of the details and provide them with methods that can be accessed by the outside world (if not provided, then this encapsulation is meaningless)
Features:
The class modified by private has the lowest permission and the most security, and its security and maintainability are significant.
For example:
Public class Fengzhuang {private int chang;// member attribute private int kuan; private int di; private int gao; public Fengzhuang () {/ / No-parameter construction} public Fengzhuang (int chang, int kuan, int di, int gao) {/ / Construction with parameters this.chang = chang; this.kuan = kuan; this.di = di; this.gao = gao } public int getChang () {return chang;} public void setChang (int chang) {this.chang = chang;} public int getKuan () {return kuan;} public void setKuan (int kuan) {this.kuan = kuan;} public int getDi () {return di;} public void setDi (int di) {this.di = di } public int getGao () {return gao;} public void setGao (int gao) {this.gao = gao;} public void mj (int chang,int kuan,int di,int gao) {int sum=chang*kuan; int sum2=di*gao/2; System.out.println ("area of rectangle" + sum+ "triangle is" + sum2 ");}}
In this example, the areas of rectangles and triangles are encapsulated and used as a method.
Conduct a test
Public class Fengzhuangtest {public static void main (String [] args) {Fengzhuang FZ = new Fengzhuang (); FZ.mj (5, 4, 6, 7);;}
About encapsulation:
Methods and private modified classes are in the form of encapsulation. When you encounter this situation where you only need to change the number (length and width, bottom and height), it can be encapsulated as a method, which is convenient for everyone to use.
2.3 Polymorphism:
Polymorphic definition:
Allow different objects to respond differently to the same message
Conditions for polymorphisms:
In order to achieve polymorphism, we must meet the three necessary conditions of inheritance, rewriting and parent reference pointing to subclass objects (upward transition and downward transition).
Upward transformation:
Parent class type variable name = new subclass type
This situation generally applies when the parent class fully meets the requirements, and we don't need to add new attributes.
Transform downwards:
Subclass type variable name = subclass type variable of parent class type
This situation is generally applicable when the parent class cannot meet the requirements and needs to use methods specific to the subclass.
Automatic packing and automatic unpacking 3.1 automatic packing
Automatically convert basic data types to wrapper class types
3.2 automatic unpacking
Automatically convert wrapper class types to basic data types
Thank you for your reading, the above is the content of "how to understand Java object-oriented". After the study of this article, I believe you have a deeper understanding of how to understand Java object-oriented, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.