ACCP8.0 Java Course second semester-about classes and objects

Source: Internet
Author: User


This chapter focuses on: constructors, encapsulation, overloading
This chapter is difficult: The static keyword this keyword

1. Process-oriented and object-oriented differences
Process-oriented is the completion of functions in the main function, from the beginning to the end, step-by-step implementation of the function
An object-oriented approach is to work together, interact with each class, consider which classes, what properties, and how


2. How to use object-oriented programming
2.1 Abstract class (find noun)
2.2 Defining attributes (finding features)
2.3 Definition method (find behavior or verb)
1/2*3*Now there is such an example that Xiao Zhang went to buy an ice cream, and he was quite happy4* 1First, we find the noun to find the small Zhang (people), ice cream (things) These two kinds of5* 2The second step is to find out what characteristics we have found for the name (Xiao Zhang), the characteristics of satisfaction (happy)6* 3The third step is to find out what the people are doing. We found the "buy" action by example .7*/8  Public classperson{9     PrivateString name;//nameTen     Private  intLove//Satisfaction Degree One  A      Public voidsetName (String name) { -          This. Name =name; -     } the      -      PublicString GetName () { -          -         return  This. Name +     } -      +     /** A * method of purchase at     */ -      Public voidBuy (SomeThing St) { -  -     } -       - } in classsomething{ -     to   PrivateString name; +    -    PublicString GetName () { the       returnname;  *   }   $}




3. Constructors
3.1 Related operations for class initialization
3.2 No return value
3.3 A default constructor is provided if the class does not define a constructor
3.4 Constructors Support overloading
1 /**2 * Created by Niechen on 17/4/18.3  */4  Public classTest3 {5 6      PublicTest3 () {7System.out.println ("This is the default constructor");8     }9 Ten      PublicTEST3 (intNumber ) { OneSystem.out.println ("This is a number constructor"); A     } -  -      PublicTest3 (String text) { the          This(10); -System.out.println ("This is a text constructor"); -     } +  -      PublicTest3 (String ... text) { +System.out.println ("This is also a constructor"); A     } atThis method has a return value, so it's just a normal method. -      Public voidTest3 () { -System.out.println ("It's just an ordinary way"); -     } -  -      Public Static voidMain (string[] args) { in         NewTest3 (); -     } to}



4.static keywords
The 4.1 static keyword can be defined on member variables, methods, static regions, and inner classes
4.2 Static methods cannot directly invoke properties and methods of non-static members
4.3 static can be used for shared data on all objects
When the class 4.4 is loaded, it is assigned a value.
4.5 Static Memory-"static code block-" Construction method (multiple static code blocks are executed in order)
/**
* Sample Code
* * Created by Niechen on 17/4/18.*/ Public classTest4 { Public Static intAge = 10; Public Static intNumber ; Static{ Age--; System.out.println ("Output in static code block age:" +Age ); } Static{ Age--; System.out.println ("The second static code block outputs Age:" +Age ); } Test4 () { number++; System.out.println ("The constructor outputs Age:" +Age ); } Public Static intTest () {System.out.println (age); returnAge ; } Public Static voidMain (string[] args) {NewTest4 (); NewTest4 (); Test4.test (); System.out.println ("Number of objects created:" +test4.number); } Public Static classTest2 {}
Output : Static code block output age:9 output in the second static code block age:8 constructoroutput Age: 8 constructor output age:  Number of objects created:2



5.this keywords
5.1 can be distinguished from method parameters and member variables
5.2 This refers to the current reference object, which in memory opens up a memory block referencing itself
5.3 is used to call other constructors in the constructor and must be placed in the first line of code
1  PackageOrg.lyrk.accp8.s2.chapter.one;2 3 /**4 * Created by Niechen on 17/4/18.5  */6  Public classTEST5 {7     Private intNumber ;8     PrivateString name;9 Ten      PublicTEST5 (intNumber , String name) { One          This. name = name;//This keyword is one of the functions when the parameter is the same as the property name, the internal or the parameter is distinguished A          This. Number =Number ; -     } -  the      PublicTest5 () { -          This(10, "Hello");//One of the functions of the this keyword invokes other constructors in this class -System.out.println ("This is the default constructor"); -     } +  -      PublicTest5 setName (String name) { +          This. Name =name; A         return  Thisone of the//this keyword functions returns the current reference object at     } -  -      PublicTest5 Setnumber (intNumber ) { -          This. Number =Number ; -         return  This; -     } in  -      Public Static voidMain (string[] args) { toTest5 TEST5 =NewTest5 (). SetName ("Zhang San"). Setnumber (10); + System.out.println (test5.name); - System.out.println (test5.number); the     } *  $}



6. About Constants
6.1 To describe values that do not need to be changed
6.2 It is static immutable, decorated with static final (the book is only final decorated)

7. About Encapsulation
7.1 Internal implementation of specific details, provide methods for external calls
7.2 Internal member variables are decorated with private modifier to provide getter setter method externally
7.3 Public > protected (Chapter II) > Package (without access modifier) > private

8. About overloading
8.1 must be in the same class, the method name is the same, the argument list is different (the number of method parameters or the parameter type is different), and the method return value and method modifiers have no relationship
8.2 Overloaded matching method rules are from precise to fuzzy until a suitable method is found
 PackageOrg.lyrk.accp8.s2.chapter.one;/*** Created by Niechen on 17/4/17.*/ Public classTest8 { Public Static voidTestintT1) {System.out.println (T1+ "int"); }     Public Static voidTestCharT1) {System.out.println (T1+ "Char"); }     Public Static voidTestDoubleT1,DoubleT2) {System.out.println (T1+T2); }     Public Static intTest (String c,string s) {return0;} //public static int test (int t1) {return 0;}     Public Static voidTestintT1,DoubleT2) {System.out.println (T1-T2); }     Public Static voidTest (Integer T1,DoubleT2) {System.out.println ("Integer:" + (T1-T2)); }     Public Static voidMain (string[] args) {Test (10,2.0); Test (' C ', 2); }} Output:8.097.0

ACCP8.0 Java Course second semester-about classes and objects

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.