Java learning 9--Object-oriented

Source: Internet
Author: User

(Focus: Memory analysis)

Definition of Class

1 //define a class with the class keyword2 classPerson {3     //member Variable definition4     Private intID;5     Private intAge = 20;6     //method Definition7      Public intGetage () {8         returnAge ;9     }Ten      Public voidSetage (inti) { OneAge =i; A     } -      Public intgetId () { -         returnID; the     } -}

Member variables

1. member variables can use any of the data types in the Java language (including basic types and reference types);

2, the function of the member variable is the whole class body;

3, the member variable can be initialized when it is defined, and if not initialized, Java uses the default value to initialize it.

Reference

1. Variable types other than basic types in the Java language are referred to as reference types.

2. Manipulating objects by reference in Java

1 // declares a reference variable of type string, but does not point it to an object 2 String S; 3 // use the new statement to create a string object and point it to it to complete its operation with s 4 New String ("Hello world!!");

Construction method

1. Create a new object by using New + construction method

2. A constructor is a function that is defined in a Java class to initialize an object

3. The constructor has the same name as the class and no return value

4. When a constructor is not specified, the compiler automatically adds a default constructor for the class

1  Public classPerson {2     Private intID;3     Private intAge = 20;4     //constructor Function5Person (intNinti) {6ID =N;7Age =i;8     }9      Public Static voidMain (String args[]) {Ten         //use constructors to initialize the object's member variables when creating an object OnePerson Tom =NewPeron (1, 25); A     } -}

Overloading of methods

1. A class can define multiple methods with the same name and return type, but with different parameters (number and type).

2, when called, the corresponding method will be selected according to the different parameter table

3. Overloads that can also be used to construct functions

1  Public classPerson {2     Private intID;3     Private intAge = 20;4     //constructor Function5Person (intNinti) {6ID =N;7Age =i;8     }9 Ten     voidinfo () { OneSYSTEM.OUT.PRINTLN ("My ID is" +ID); A     } -  -     //Overloaded Info Method the     voidinfo (String t) { -System.out.println (t + "ID is" +ID); -     } -  +      Public Static voidMain (String args[]) { -         //use constructors to initialize the object's member variables when creating an object +Person Tom =NewPeron (1, 25); A p.info (); atP.info ("Lily"); -     } -}

Recommended naming rules

1, the first letter of the class name capital

2. The first letter of the variable name and method name is lowercase

3, the use of hump marking

Java learning 9--Object-oriented

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.