Getting Started with Java-object-oriented Foundation (i)

Source: Internet
Author: User

Getting Started with Java-object-oriented Foundation (i) purpose of this blog
    1. Exercises (important)
    2. Object-oriented Concepts (understanding)
    3. Three main features of Object-oriented (important)

2016-09-10--13:13:39

LL exercises sorting arrays in the form of method calls
1  packageSep10;2 //call method to complete the sorting of two shaped arrays and print3  public classArrayRefDemo03 {4 5     /**6      * @paramargs7      */8      public Static voidmain (string[] Args) {9         //TODO auto-generated Method StubTen         intage[]={12,32,43,54,43}; one         intscore[]={123,312,432,54,65}; a Sort (age); - Print (age); -System.out.println ("\n************************************"); the Sort (score); - Print (score); -     } -      public Static voidSortintX[]) { +          for(inti=0;i<x.length;i++){ -              for(intj=i+1;j<x.length;j++){ +                 if(x[i]>X[j]) { a                     intmiddle=x[i]; atx[i]=x[j]; -x[j]=middle; -                 } -             } -         } -          in     } -      public Static voidPrintintTemp[]) { to          for(inti=0;i<temp.length;i++){ +System.out.print (temp[i]+ "\ t"); -         } the     } *}
    ************************************54    123    312    432    
using the Java class Library for array sorting
1  packageSep10;2 Importjava.util.Arrays;3  public classArrayRefDemo04 {4 5     /**6      * @paramargs7      */8      public Static voidmain (string[] Args) {9         //TODO auto-generated Method StubTen         intage[]={12,32,43,54,43}; one         intscore[]={123,312,432,54,65}; aArrays.sort (age);//in the Java class library, the array class itself has a sort function - Print (age); -System.out.println ("\n************************************"); the Arrays.sort (score); - Print (score); -     } -      public Static voidPrintintTemp[]) { +          for(inti=0;i<temp.length;i++){ -System.out.print (temp[i]+ "\ t"); +         } a     } at}
    ************************************54    123    312    432    
Write for 1! +2! +3! +......+30! And show the results of the program
1  packageSep10;2 //Write for 1! +2! +3! +......+30! And show the results of the program!3  public classDemo01 {4 5     /**6      * @paramargs7      */8      public Static voidmain (string[] Args) {9         //TODO auto-generated Method StubTen         LongY=0; one          for(inti=1;i<=30;i++){ ay=y+factorseries (i); -         } - System.out.println (y); the     } -     //function to find factorial -      public Static intFactorseries (intx) { -         intj=x; +          while(x>1){ -j=j* (x-1); +x--; a         } at         returnj; -     } -}
3430808089
Object-oriented Concepts
1  packageSep10;2 //object is created by using the3  public classClassDemo02 {4 5     /**6      * @paramargs7      */8      public Static voidmain (string[] Args) {9         //TODO auto-generated Method StubTenPerson per=NewPerson ("Bob", 23); one Per.info (); a     } -  - } the classperson{ - String name; -     intage ; -      publicPerson (String name,intage ) { +          this. name=name; -          this. age=age ; +     } a      public voidInfo () { atSystem.out.println ("name" +name+ ", age" +age ); -     } -}
Name bob, age 23
Object reference passing
1  packageSep10;2 //object reference passing is actually the stack memory space of the object behind the equals sign to use before the equals sign .3  public classCalssDemo05 {4 5     /**6      * @paramargs7      */8      public Static voidmain (string[] Args) {9         //TODO auto-generated Method StubTenPerson2 per1=NewPerson2 (); onePerson2 per2=NewPerson2 (); aPer1.age=21; -Per1.name= "Bob"; -Per2=per1; thePer2.age=23; -Per2.name= "glove"; - Per1.info (); - Per2.info (); +     } - } + classperson2{ a String name; at     intage ; -      public voidInfo () { -System.out.println ("name:" +name+ ", age" +age ); -     } -}
Name: glove, age 23 name: glove, age 23
Encapsulation of

In development, the properties of the class are encapsulated, the properties are private, they cannot be accessed through the object, they must be obtained through setter and Getter methods, and "inspection code" can be added to the setter to ensure the practical significance of the private variables. It is obviously a cumbersome method to assign values through setter, so the object is typically initialized by constructing the Method.

Class design

String class

You can directly use the "instantiation", or you can use the new string (""), cannot directly use ' = = ' to compare the contents of the string, you need to use the Equals Method--str1.euqals (str2); returns a Boolean Amount.

The change in the contents of the string object is actually implemented by the "disconnect-join" of the memory address, and the contents of the string itself are not Changed.

Getting Started with Java-object-oriented Foundation (i)

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.