Java object-oriented programming knowledge points summary

Source: Internet
Author: User

One: Completed today

The morning details about Java object-oriented programming are documented below.

1) class

is a reference type that contains a signature and a principal, which is a member that is placed inside curly braces, with members including fields and methods, as well as construction methods, initializers, and nested types.

There are four common members: Class field, class method, instance field, strength method.

Other reference types also include arrays, interfaces, enumerations, and annotations.

2) fields and methods

Example: A simple class and member

12345678910111213141516171819202122 publicclassCircle{//类字段public staticfinaldoublePI=3.14159;//类方法publicstaticdoubleradiansToDegrees(doubleradians){    return radians * 180/ PI;}   //实例字段publicdoule r;//两个实例方法publicdoublearea(){  returnPI*r*r;}publicdoublecircumference(){  return2*PI*r;}}

There is the use of this, it unexpectedly is an implicit parameter! The first time you know, or maybe I didn't notice. The meaning is "to raise an instance method on the current object ".

3) Creating and initializing objects

A constructor method is considered a class member, and multiple construction methods can be defined, as long as the argument list is not the same. Other construction methods can also be called in a construction method, which avoids a large amount of initialization code.

4) Subclasses and inheritance

For the Super keyword, you need to know that calling a superclass construction method in a subclass must be called in the first statement. The code is as follows

12345 publc PlaneCircle(doublex;double y;doubler){super(r);//调用超类的circle()构造方法this.cx=x;this.cy=y;}

This involves constructing a chain of methods, that is, a series of construction methods when creating objects, from subclasses to the object class at the top.

How do I refer to a masked field? Straighten this (or instance of the class) into the appropriate superclass, and then access the fields.

For example, there are three classes A, B, and C. They all define a field of x, and C is a subclass B and B is a subclass, so you can do that in C.

123456789 x;//c类this.x;//c类 super.x;//B类 ((B)this).x;/B类((A)this).x;//A类<br>类似的,如果c是C的实例,那可以这样引用<br>c.x;<br>((B)c).x<br>((A)c).x

You also need to distinguish between method overrides and field cloaking.

Note the Super usage when calling the override method is not the same as super when calling the superclass.

5) data hiding and encapsulation

Java packages cannot be "nested", such as Java. A.A and JAVA.A are different packages.

For access control and inheritance, the following

Summarizes the member access rules, as follows

For some data accessor methods (Getter,setter), it is necessary to remember that the public method access field can be checked.

6) Abstract classes and methods

Some abstract classes can have no abstract methods, such as Classloder.

Example code: An abstract class and two concrete classes

1234567891011121314151617181920 publicabstractclassShape{//两个抽象方法publicabstractdouble area();publicabstractdoublecircumference(); }//具体子类classCircle extendsShape{publicstaticfinal doublePI= 3.14159265358;//类字段protecteddoubler;//实例字段publicCircle(doubler){this.r=r}//构造方法 publicdoublegetRadius(){ returnr;}//访问器publicdoublearea(){ returnPI*r*r ;}//实现的两个抽象方法publicdouble circumference(){ return2*PI*r ;}}

Conversions can also occur between objects, which are divided into zoom in and zoom out conversions

12 Object  o="string";//放大String s= (String)o;//缩小

Array conversion rules apply only to arrays that consist of objects or arrays, and arrays of primitive types cannot be converted.

Second: PM Plan

Read and record the Java type System chapter, and have time to start doing the first plate of the task
Three: Encounter problems

Why does it feel like writing code doesn't feel like there's so much knowledge? Is it useless? Or lack of self-reflection?
Four: Harvest sentiment

In contrast to other object-oriented languages, Java can really be said to be the first choice, even if it sometimes seems verbose.

Java object-oriented programming knowledge points summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.