In-Java constructors, keywords, permission modifiers, access control, and object-oriented features!

Source: Internet
Author: User

1, constructor (construction method)
When using new to create an object, it is called a constructor (constructor) of the class, the constructor name is the same as the class, and there is no return value (no return value and no write void)

constructor and parameter constructors for null parameters

Notes, naming rules!!!!!!!!!!

2, the constructor in the inheritance:
  --the constructor method of the subclass must call the constructor of the parent class
--super (), call the constructor of the parent class, this () call another constructor method in this class

3, keyword this, static, package, and import statements
This
--Use the This keyword in the method definition of a class to represent a reference to an object that uses the method

--Use this when you must indicate who is currently using the method's object

--Sometimes this can handle the case of member variables and parameter names in a method

--this can be seen as a variable whose value is a reference to the current object

Static (not inside the heap nor inside the stack, in the data seg):

--Class name directly. Variables that come out are static variables, with only one copy of the static variables in each class, which is common (each instantiated class is available after assignment)

--Non-static members are not accessible in static methods

--Static variables and static methods do not need to be instantiated

Package

Easy to manage the large number of classes in big software systems, to solve the phenomenon of class name duplicate

4, access control, permission modifier (Public private default protected)


Puclic class Name {

Private type attribute 1;

Private type attribute 2;

Set, get
}
5, object-oriented three major features: encapsulation, inheritance, polymorphism
Encapsulation (...)

Inherited:

Subclasses have properties and methods of the parent class

Object.Finalize (); Call this method to release resources

Polymorphism ((during execution) dynamic binding, pool binding):

Overload (Overload):

Method name is the same, only the parameter type is different or the number of parameters is different, the return value is not overloaded

Rewrite (overwrite/override):

Redefine the method of the parent class

--in subclasses can be overridden as needed from a method integrated in the parent class

--The overriding method must have the same method name as the overridden method, a parameter list, and a return type

--The overriding method cannot use more restrictive access than the overridden method

Override of ToString Method

Override of the Equals method

6,super, final keyword

Super: Reference to Parent class

Final value of variable cannot be changed

The final method cannot be overridden

The final class cannot be inherited

7, Object transformation

--a reference to the parent class can point to the object of its child class

--The parent class's reference does not have access to the new members (including properties and methods) of its child class object

--You can use the INSTANCEOF keyword to determine

 Public classTestwrapper { Public Static voidMain (string[] args) {//classes commonly used in wrapper classes and Java programming mathString S= "12345"; System.out.println ("Convert a string to a number:" +Integer.parseint (s)); //Math class//PiSystem.out.println (Math.PI); //Natural logarithmSystem.out.println (MATH.E); //Trigonometric Functions//Math.sin (12.34); //Math.tan (12.34); //...        //exponential function//Math.exp (12.34); //Math.log (12.34); //...                //Rounding method, rounding up , rounding up and rounding downSystem.out.println (Math.rint (12.74));//Who are you close to?System.out.println (Math.floor (12.74));//take the smallest integerSystem.out.println (Math.ceil (12.34));//take maximum integerSystem.out.println (Math.Round (12.34));//serious rounding, returns a long integerSystem.out.println (Math.min (12.34, 23.45));//take a two-digit smaller one.System.out.println (Math.max (12.34, 23.45));//take two of the bigger one.System.out.println (Math.Abs (-23));//take absolute value                    }}

  Example : Define a Circle class, define a point, and calculate whether the point is within the circle range

 Public classpoint{Private Doublex;  Public Doubley;  PublicPoint (DoubleXDoubley) { This. x =x;  This. y =y; }     Public DoubleGetX () {return  This. x; }}
 Packagecom.maya.test; Public classYuan { Public Doublex;  Public Doubley;  Public DoubleR;  PublicYuan (DoubleXDoubleYDoubleR) {         This. x =x;  This. y =y;  This. R =R; }         Public voidSuan (point p) {DoubleL = Math.Abs (Math.sqrt ((x-p.x) * (x-p.x) + (y-p.y) * (y( p.y))); if(l>R) {System.out.println ("Not in the circle"); }Else{System.out.println ("Inside the Circle"); }    }}
 Package com.maya.test;  Public class Main {    publicstaticvoid  main (string[] args) {    new Point (3,4);     New Yuan (2.2,3.3,5);    S.suan (n);    }}

In-Java constructors, keywords, permission modifiers, access control, and object-oriented features!

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.